From 740016e0d5b77d68d4006e12235ead3c043a6b6a Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sun, 17 Dec 2023 05:22:40 +0000 Subject: [PATCH] hw wallets: fix crashes on macOS related https://github.com/trezor/cython-hidapi/pull/150#issuecomment-1542391087 --- electrum/plugin.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/electrum/plugin.py b/electrum/plugin.py index e24a8effb..3ea17c3c1 100644 --- a/electrum/plugin.py +++ b/electrum/plugin.py @@ -140,7 +140,7 @@ class Plugins(DaemonThread): % (self.gui_name, name)) try: module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) + spec.loader.exec_module(module) # note: imports the plugin code in a *different* thread plugin = module.Plugin(self, self.config, name) except Exception as e: raise Exception(f"Error loading {name} plugin: {repr(e)}") from e @@ -374,6 +374,16 @@ _hwd_comms_executor = concurrent.futures.ThreadPoolExecutor( thread_name_prefix='hwd_comms_thread' ) +# hidapi needs to be imported from the main thread. Otherwise, at least on macOS, +# segfaults will follow. (see https://github.com/trezor/cython-hidapi/pull/150#issuecomment-1542391087) +# To keep it simple, let's just import it now, as we are likely in the main thread here. +if threading.current_thread() is not threading.main_thread(): + _logger.warning("expected to be in main thread... hidapi will not be safe to use now!") +try: + import hid +except ImportError: + pass + T = TypeVar('T')