Skip to content

Commit 740016e

Browse files
committed
hw wallets: fix crashes on macOS
related trezor/cython-hidapi#150 (comment)
1 parent bf4934b commit 740016e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

electrum/plugin.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def load_plugin(self, name) -> 'BasePlugin':
140140
% (self.gui_name, name))
141141
try:
142142
module = importlib.util.module_from_spec(spec)
143-
spec.loader.exec_module(module)
143+
spec.loader.exec_module(module) # note: imports the plugin code in a *different* thread
144144
plugin = module.Plugin(self, self.config, name)
145145
except Exception as e:
146146
raise Exception(f"Error loading {name} plugin: {repr(e)}") from e
@@ -374,6 +374,16 @@ class HardwarePluginToScan(NamedTuple):
374374
thread_name_prefix='hwd_comms_thread'
375375
)
376376

377+
# hidapi needs to be imported from the main thread. Otherwise, at least on macOS,
378+
# segfaults will follow. (see https://github.com/trezor/cython-hidapi/pull/150#issuecomment-1542391087)
379+
# To keep it simple, let's just import it now, as we are likely in the main thread here.
380+
if threading.current_thread() is not threading.main_thread():
381+
_logger.warning("expected to be in main thread... hidapi will not be safe to use now!")
382+
try:
383+
import hid
384+
except ImportError:
385+
pass
386+
377387

378388
T = TypeVar('T')
379389

0 commit comments

Comments
 (0)