Skip to content

Commit

Permalink
disconnect signals when closing wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasgriffin committed Jan 25, 2025
1 parent fce9f22 commit 1d977b6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
43 changes: 33 additions & 10 deletions bitcoin_safe/gui/qt/qt_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

import bdkpython as bdk
from bitcoin_qr_tools.data import Data
from PyQt6.QtCore import Qt, QTimer, pyqtSignal
from PyQt6.QtCore import Qt, QTimer, pyqtBoundSignal, pyqtSignal
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import (
QApplication,
Expand Down Expand Up @@ -287,15 +287,17 @@ def __init__(
self.quick_receive.update_content(UpdateFilter(refresh_all=True))

#### connect signals
self.signal_on_change_sync_status.connect(self.update_status_visualization)
self.signals.language_switch.connect(self.updateUi)
self.wallet_signals.updated.connect(self.signals.any_wallet_updated.emit)
self.wallet_signals.export_labels.connect(self.export_labels)
self.wallet_signals.export_bip329_labels.connect(self.export_bip329_labels)
self.wallet_signals.import_labels.connect(self.import_labels)
self.wallet_signals.import_bip329_labels.connect(self.import_bip329_labels)
self.wallet_signals.import_electrum_wallet_labels.connect(self.import_electrum_wallet_labels)
self.signal_on_change_sync_status.connect(self.update_display_balance)
self.connect_signal(self.signal_on_change_sync_status, self.update_status_visualization)
self.connect_signal(self.signals.language_switch, self.updateUi)
self.connect_signal(self.wallet_signals.updated, self.signals.any_wallet_updated.emit)
self.connect_signal(self.wallet_signals.export_labels, self.export_labels)
self.connect_signal(self.wallet_signals.export_bip329_labels, self.export_bip329_labels)
self.connect_signal(self.wallet_signals.import_labels, self.import_labels)
self.connect_signal(self.wallet_signals.import_bip329_labels, self.import_bip329_labels)
self.connect_signal(
self.wallet_signals.import_electrum_wallet_labels, self.import_electrum_wallet_labels
)
self.connect_signal(self.signal_on_change_sync_status, self.update_display_balance)

self._start_sync_retry_timer()
self._start_sync_regularly_timer()
Expand Down Expand Up @@ -427,6 +429,27 @@ def stop_sync_timer(self) -> None:
self.timer_sync_retry.stop()
self.timer_sync_regularly.stop()

def disconnect_signals(self) -> None:
def discon_sig(signal: pyqtBoundSignal):
"""
Disconnect only breaks one connection at a time,
so loop to be safe.
"""
while True:
try:
signal.disconnect()
except TypeError:
break
return

super().disconnect_signals()
signals = self.wallet_signals
for signal_name in dir(signals):
signal = getattr(signals, signal_name)
if not isinstance(signal, pyqtBoundSignal):
continue
discon_sig(signal)

def close(self) -> None:
self.disconnect_signals()
self.label_syncer.send_all_labels_to_myself()
Expand Down
17 changes: 13 additions & 4 deletions bitcoin_safe/gui/qt/signal_carrying_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import logging
from typing import Callable, List, Tuple

from PyQt6.QtCore import QObject
from PyQt6.QtCore import QObject, pyqtBoundSignal

from bitcoin_safe.typestubs import TypedPyQtSignal, TypedPyQtSignalNo

from ...signals import SignalFunction

Expand All @@ -40,9 +42,16 @@
class SignalCarryingObject(QObject):
def __init__(self, parent=None, **kwargs) -> None:
super().__init__(parent, **kwargs)
self._connected_signals: List[Tuple[SignalFunction, Callable]] = []

def connect_signal(self, signal, f, **kwargs) -> None:
self._connected_signals: List[
Tuple[SignalFunction | pyqtBoundSignal | TypedPyQtSignalNo | TypedPyQtSignal, Callable]
] = []

def connect_signal(
self,
signal: SignalFunction | pyqtBoundSignal | TypedPyQtSignalNo | TypedPyQtSignal,
f: Callable,
**kwargs
) -> None:
signal.connect(f, **kwargs)
self._connected_signals.append((signal, f))

Expand Down

0 comments on commit 1d977b6

Please sign in to comment.