diff --git a/electrum/gui/qt/__init__.py b/electrum/gui/qt/__init__.py index 61c80c9fb..cdfe9d538 100644 --- a/electrum/gui/qt/__init__.py +++ b/electrum/gui/qt/__init__.py @@ -329,7 +329,7 @@ class ElectrumGui(Logger): def close_window(self, window: ElectrumWindow): if window in self.windows: - self.windows.remove(window) + self.windows.remove(window) self.build_tray_menu() # save wallet path of last open window if not self.windows: @@ -375,6 +375,9 @@ class ElectrumGui(Logger): self.app.lastWindowClosed.connect(quit_after_last_window) def clean_up(): + # If there are still some open windows, try to clean them up. + for window in list(self.windows): + window.clean_up() # Shut down the timer cleanly self.timer.stop() # clipboard persistence. see http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg17328.html diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py index 3e9d75eb4..acae82c38 100644 --- a/electrum/gui/qt/main_window.py +++ b/electrum/gui/qt/main_window.py @@ -190,7 +190,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger): self.contacts = wallet.contacts self.tray = gui_object.tray self.app = gui_object.app - self.cleaned_up = False + self._cleaned_up = False self.payment_request = None # type: Optional[paymentrequest.PaymentRequest] self.payto_URI = None self.checking_accounts = False @@ -300,6 +300,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger): _("Would you like to be notified when there is a newer version of Electrum available?")) config.set_key('check_updates', bool(choice), save=True) + self._update_check_thread = None if config.get('check_updates', False): # The references to both the thread and the window need to be stored somewhere # to prevent GC from getting in our way. @@ -496,7 +497,6 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger): def close_wallet(self): if self.wallet: self.logger.info(f'close_wallet {self.wallet.storage.path}') - self.wallet.thread = None run_hook('close_wallet', self.wallet) @profiler @@ -3158,14 +3158,17 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger): self.show_warning(_('Please restart Electrum to activate the new GUI settings'), title=_('Success')) def closeEvent(self, event): - # It seems in some rare cases this closeEvent() is called twice - if not self.cleaned_up: - self.cleaned_up = True - self.clean_up() + # note that closeEvent is NOT called if the user quits with Ctrl-C + self.clean_up() event.accept() def clean_up(self): - self.wallet.thread.stop() + if self._cleaned_up: + return + self._cleaned_up = True + if self.wallet.thread: + self.wallet.thread.stop() + self.wallet.thread = None util.unregister_callback(self.on_network) self.config.set_key("is_maximized", self.isMaximized()) if not self.isMaximized(): @@ -3177,6 +3180,9 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger): self.qr_window.close() self.close_wallet() + if self._update_check_thread: + self._update_check_thread.exit() + self._update_check_thread.wait() self.gui_object.timer.timeout.disconnect(self.timer_actions) self.gui_object.close_window(self) diff --git a/electrum/gui/qt/network_dialog.py b/electrum/gui/qt/network_dialog.py index cce54683c..5085e246b 100644 --- a/electrum/gui/qt/network_dialog.py +++ b/electrum/gui/qt/network_dialog.py @@ -65,6 +65,7 @@ class NetworkDialog(QDialog): self.network_updated_signal_obj.network_updated_signal.connect( self.on_update) util.register_callback(self.on_network, ['network_updated']) + self._cleaned_up = False def on_network(self, event, *args): self.network_updated_signal_obj.network_updated_signal.emit(event, args) @@ -72,6 +73,14 @@ class NetworkDialog(QDialog): def on_update(self): self.nlayout.update() + def closeEvent(self, event): + if not self._cleaned_up: + self._cleaned_up = True + self.clean_up() + event.accept() + + def clean_up(self): + self.nlayout.clean_up() class NodesListWidget(QTreeWidget): @@ -308,6 +317,13 @@ class NetworkChoiceLayout(object): self.fill_in_proxy_settings() self.update() + def clean_up(self): + if self.td: + self.td.found_proxy.disconnect() + self.td.exit() + self.td.wait() + self.td = None + def check_disable_proxy(self, b): if not self.config.is_modifiable('proxy'): b = False diff --git a/electrum/gui/qt/util.py b/electrum/gui/qt/util.py index 6330ab1d0..473865b46 100644 --- a/electrum/gui/qt/util.py +++ b/electrum/gui/qt/util.py @@ -914,6 +914,8 @@ class TaskThread(QThread): def stop(self): self.tasks.put(None) + self.exit() + self.wait() class ColorSchemeItem: