From bcdf730a72cd23805ec6ec53e7b5b07a1d38781d Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Sun, 14 Jul 2019 18:47:10 +1000 Subject: [PATCH] base: make sure we only connect once to the bluez connect/disconnect signals This was getting stacked - once on init and once whenever we actually made a connecting, resulting in the signal never getting disconnected properly and some races because we tried to sync twice. The whole "listen()" vs "listening" vs "connected" etc. is a bit of a mess and should be reworked for a proper fix to this. But for now this will do. This may fix #124 --- tuhi/base.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tuhi/base.py b/tuhi/base.py index 488d63f..cbaa940 100644 --- a/tuhi/base.py +++ b/tuhi/base.py @@ -72,7 +72,8 @@ class TuhiDevice(GObject.Object): self._battery_percent = 0 self._last_battery_update_time = 0 self._battery_timer_source = None - self._signals = {} + self._signals = {'connected': None, + 'disconnected': None} self._bluez_device = bluez_device @@ -151,8 +152,10 @@ class TuhiDevice(GObject.Object): return self._sync_state def _connect_device(self, mode): - self._signals['connected'] = self._bluez_device.connect('connected', self._on_bluez_device_connected, mode) - self._signals['disconnected'] = self._bluez_device.connect('disconnected', self._on_bluez_device_disconnected) + if self._signals['connected'] is None: + self._signals['connected'] = self._bluez_device.connect('connected', self._on_bluez_device_connected, mode) + if self._signals['disconnected'] is None: + self._signals['disconnected'] = self._bluez_device.connect('disconnected', self._on_bluez_device_disconnected) self._bluez_device.connect_device() def register(self): @@ -181,7 +184,7 @@ class TuhiDevice(GObject.Object): try: bluez_device.disconnect(self._signals['connected']) - del self._signals['connected'] + self._signals['connected'] = None except KeyError: pass @@ -193,7 +196,7 @@ class TuhiDevice(GObject.Object): logger.debug(f'{bluez_device.address}: disconnected') try: bluez_device.disconnect(self._signals['disconnected']) - del self._signals['disconnected'] + self._signals['disconnected'] = None except KeyError: pass