diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0b0e10d0..a95c77f8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,7 @@ Added * Added optional hack to use Bluetooth address instead of UUID on macOS. * Added ``BleakScanner.find_device_by_name()`` class method. * Added optional command line argument to use debug log level to all applicable examples. +* Make sure the disconnect monitor task is properly cancelled on the BlueZ client. Changed ------- diff --git a/bleak/backends/bluezdbus/client.py b/bleak/backends/bluezdbus/client.py index 8bf88ff0..da2f9f7a 100644 --- a/bleak/backends/bluezdbus/client.py +++ b/bleak/backends/bluezdbus/client.py @@ -161,6 +161,8 @@ def on_value_changed(char_path: str, value: bytes) -> None: ) self._remove_device_watcher = lambda: manager.remove_device_watcher(watcher) + local_disconnect_monitor_event = asyncio.Event() + try: try: # @@ -197,10 +199,10 @@ def on_value_changed(char_path: str, value: bytes) -> None: self._is_connected = True # Create a task that runs until the device is disconnected. - self._disconnect_monitor_event = asyncio.Event() + self._disconnect_monitor_event = local_disconnect_monitor_event asyncio.ensure_future( self._disconnect_monitor( - self._bus, self._device_path, self._disconnect_monitor_event + self._bus, self._device_path, local_disconnect_monitor_event ) ) @@ -246,6 +248,9 @@ def on_value_changed(char_path: str, value: bytes) -> None: raise except BaseException: + # this effectively cancels the disconnect monitor in case the event + # was not triggered by a D-Bus callback + local_disconnect_monitor_event.set() self._cleanup_all() raise