diff --git a/src/textual/app.py b/src/textual/app.py index 07b5e4f1a3..c22875427a 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -2752,23 +2752,24 @@ def is_mounted(self, widget: Widget) -> bool: async def _close_all(self) -> None: """Close all message pumps.""" - # Close all screens on all stacks: - for stack in self._screen_stacks.values(): - for stack_screen in reversed(stack): - if stack_screen._running: - await self._prune_node(stack_screen) - stack.clear() - - # Close pre-defined screens. - for screen in self.SCREENS.values(): - if isinstance(screen, Screen) and screen._running: - await self._prune_node(screen) - - # Close any remaining nodes - # Should be empty by now - remaining_nodes = list(self._registry) - for child in remaining_nodes: - await child._close_messages() + async with self._dom_lock: + # Close all screens on all stacks: + for stack in self._screen_stacks.values(): + for stack_screen in reversed(stack): + if stack_screen._running: + await self._prune_node(stack_screen) + stack.clear() + + # Close pre-defined screens. + for screen in self.SCREENS.values(): + if isinstance(screen, Screen) and screen._running: + await self._prune_node(screen) + + # Close any remaining nodes + # Should be empty by now + remaining_nodes = list(self._registry) + for child in remaining_nodes: + await child._close_messages() async def _shutdown(self) -> None: self._begin_batch() # Prevents any layout / repaint while shutting down @@ -3342,7 +3343,10 @@ async def prune_widgets_task( await self._prune_nodes(widgets) finally: finished_event.set() - self._update_mouse_over(self.screen) + try: + self._update_mouse_over(self.screen) + except ScreenStackError: + pass if parent is not None: parent.refresh(layout=True)