From ea2c831b01152a3b401cab3ef1d31d6fa6489ba7 Mon Sep 17 00:00:00 2001 From: David Foster Date: Mon, 8 Jan 2024 10:21:05 -0500 Subject: [PATCH] Shell: Fix race condition where shell might start accepting commands before foreground thread created --- src/crystal/main.py | 14 +++++++------- src/crystal/shell.py | 3 +++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/crystal/main.py b/src/crystal/main.py index 1753dba1..430afa6e 100644 --- a/src/crystal/main.py +++ b/src/crystal/main.py @@ -189,13 +189,6 @@ def _main(args: List[str]) -> None: install_to_linux_desktop_environment() sys.exit() - # Start shell if requested - if parsed_args.shell: - from crystal.shell import Shell - shell = Shell() - else: - shell = None - # Start GUI subsystem import wx import wx.xml # required by wx.richtext; use explicit import as hint to py2app @@ -293,6 +286,13 @@ def _finish_launch(self, filepath: Optional[str]=None) -> None: from crystal.util.xthreading import is_quitting, set_foreground_thread set_foreground_thread(threading.current_thread()) try: + # Start shell if requested + if parsed_args.shell: + from crystal.shell import Shell + shell = Shell() + else: + shell = None + app = MyApp(redirect=False) # Starts tests if requested diff --git a/src/crystal/shell.py b/src/crystal/shell.py index 174e34cc..ce131ee8 100644 --- a/src/crystal/shell.py +++ b/src/crystal/shell.py @@ -17,6 +17,9 @@ class Shell: def __init__(self) -> None: + if not has_foreground_thread(): + raise ValueError('Expected there to be a foreground thread when starting a Shell') + # Setup proxy variables for shell _Proxy._patch_help() self._project_proxy = _Proxy(f'')