From 6c3c294b196239e86eeca4ca91faefd46f1de1e7 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 11 Sep 2025 20:41:04 -0500 Subject: [PATCH] backends/bluezdbus/manager: Release resources on bus restart Call disconnect() on the old bus object if we have to reconnect to the D-Bus system bus. This ensures that file handles are released and we don't leak them. --- CHANGELOG.rst | 1 + bleak/backends/bluezdbus/manager.py | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b9b16623..34529262 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -21,6 +21,7 @@ Changed Fixed ----- +* Fixed file handle leak in BlueZ backend when D-Bus connection is lost and re-established. * Fixed crash in CoreBluetooth backend if an ObjC delegate callback is called after the asyncio run loop stops. * Fixed possible deadlock when starting scanning on Windows when Bluetooth is turned off. * Fixed "Bluetooth device is turned off" Exception on macOS, when a Bluetooth permission request popup is shown to the user by the OS. diff --git a/bleak/backends/bluezdbus/manager.py b/bleak/backends/bluezdbus/manager.py index ca2c5451..ff87b151 100644 --- a/bleak/backends/bluezdbus/manager.py +++ b/bleak/backends/bluezdbus/manager.py @@ -361,6 +361,11 @@ async def async_init(self) -> None: bus.disconnect() raise + if self._bus: + # Even if we are disconnected, still need to call this to + # release file handles. + self._bus.disconnect() + # Everything is setup, so save the bus self._bus = bus