Skip to content

Commit 371f55a

Browse files
committed
hww: fix some threading issues in wizard
fixes spesmilo#3377 related: spesmilo#6064 (passphrase dialog not rendered correctly)
1 parent 81fc3fc commit 371f55a

8 files changed

Lines changed: 40 additions & 5 deletions

File tree

electrum/base_wizard.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ def do_upgrade():
158158
exc = e
159159
self.waiting_dialog(do_upgrade, _('Upgrading wallet format...'), on_finished=on_finished)
160160

161+
def run_task_without_blocking_gui(self, task, *, msg: str = None) -> Any:
162+
"""Perform a task in a thread without blocking the GUI.
163+
Returns the result of 'task', or raises the same exception.
164+
This method blocks until 'task' is finished.
165+
"""
166+
raise NotImplementedError()
167+
161168
def load_2fa(self):
162169
self.data['wallet_type'] = '2fa'
163170
self.data['use_trustedcoin'] = True
@@ -421,6 +428,7 @@ def derivation_and_script_type_dialog(self, f):
421428
def on_hw_derivation(self, name, device_info: 'DeviceInfo', derivation, xtype):
422429
from .keystore import hardware_keystore
423430
devmgr = self.plugins.device_manager
431+
assert isinstance(self.plugin, HW_PluginBase)
424432
try:
425433
xpub = self.plugin.get_xpub(device_info.device.id_, derivation, xtype, self)
426434
client = devmgr.client_by_id(device_info.device.id_)

electrum/gui/qt/installwizard.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,26 @@ def waiting_dialog(self, task, msg, on_finished=None):
529529
if on_finished:
530530
on_finished()
531531

532+
def run_task_without_blocking_gui(self, task, *, msg=None):
533+
assert self.gui_thread == threading.current_thread(), 'must be called from GUI thread'
534+
if msg is None:
535+
msg = _("Please wait...")
536+
537+
exc = None # type: Optional[Exception]
538+
res = None
539+
def task_wrapper():
540+
nonlocal exc
541+
nonlocal res
542+
try:
543+
task()
544+
except Exception as e:
545+
exc = e
546+
self.waiting_dialog(task_wrapper, msg=msg)
547+
if exc is None:
548+
return res
549+
else:
550+
raise exc
551+
532552
@wizard_dialog
533553
def choice_dialog(self, title, message, choices, run_next):
534554
c_values = [x[0] for x in choices]

electrum/plugins/digitalbitbox/digitalbitbox.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,8 @@ def setup_device(self, device_info, wizard, purpose):
707707
client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
708708
if purpose == HWD_SETUP_NEW_WALLET:
709709
client.setupRunning = True
710-
client.get_xpub("m/44'/0'", 'standard')
710+
wizard.run_task_without_blocking_gui(
711+
task=lambda: client.get_xpub("m/44'/0'", 'standard'))
711712

712713

713714
def is_mobile_paired(self):

electrum/plugins/hw_wallet/plugin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def setup_device(self, device_info: DeviceInfo, wizard: 'BaseWizard', purpose):
7878
"""Called when creating a new wallet or when using the device to decrypt
7979
an existing wallet. Select the device to use. If the device is
8080
uninitialized, go through the initialization process.
81+
82+
Runs in GUI thread.
8183
"""
8284
raise NotImplementedError()
8385

electrum/plugins/keepkey/keepkey.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ def setup_device(self, device_info, wizard, purpose):
279279
client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
280280
if not device_info.initialized:
281281
self.initialize_device(device_id, wizard, client.handler)
282-
client.get_xpub('m', 'standard')
282+
wizard.run_task_without_blocking_gui(
283+
task=lambda: client.get_xpub("m", 'standard'))
283284
client.used()
284285

285286
def get_xpub(self, device_id, derivation, xtype, wizard):

electrum/plugins/ledger/ledger.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ def create_client(self, device, handler):
591591
def setup_device(self, device_info, wizard, purpose):
592592
device_id = device_info.device.id_
593593
client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
594-
client.get_xpub("m/44'/0'", 'standard') # TODO replace by direct derivation once Nano S > 1.1
594+
wizard.run_task_without_blocking_gui(
595+
task=lambda: client.get_xpub("m/44'/0'", 'standard')) # TODO replace by direct derivation once Nano S > 1.1
595596

596597
def get_xpub(self, device_id, derivation, xtype, wizard):
597598
if xtype not in self.SUPPORTED_XTYPES:

electrum/plugins/safe_t/safe_t.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ def setup_device(self, device_info, wizard, purpose):
253253
client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
254254
if not device_info.initialized:
255255
self.initialize_device(device_id, wizard, client.handler)
256-
client.get_xpub('m', 'standard')
256+
wizard.run_task_without_blocking_gui(
257+
task=lambda: client.get_xpub("m", 'standard'))
257258
client.used()
258259

259260
def get_xpub(self, device_id, derivation, xtype, wizard):

electrum/plugins/trezor/trezor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ def setup_device(self, device_info, wizard, purpose):
280280
if not device_info.initialized:
281281
self.initialize_device(device_id, wizard, client.handler)
282282
is_creating_wallet = purpose == HWD_SETUP_NEW_WALLET
283-
client.get_xpub('m', 'standard', creating=is_creating_wallet)
283+
wizard.run_task_without_blocking_gui(
284+
task=lambda: client.get_xpub('m', 'standard', creating=is_creating_wallet))
284285
client.used()
285286

286287
def get_xpub(self, device_id, derivation, xtype, wizard):

0 commit comments

Comments
 (0)