Repository navigation
Disconnect a BLE client whose link dropped - #136
Merged
fdlamotte merged 1 commit intoNov 25, 2026
Merged
Conversation
When a link dropped on its own, handle_disconnect let go of the BleakClient without calling its disconnect(). On Linux, bleak releases the D-Bus connection it opened for a link only in disconnect(), so the connection outlived the link, BlueZ kept its notify session and turned it back on at the next connect, and every notification arrived once more for each link that had dropped before. handle_disconnect now disconnects the client bleak passes it, in a tracked background task. With the link down, bleak's disconnect() only releases the bus. It is bounded by DISCONNECT_TIMEOUT (10 s): a client whose own D-Bus connection never came up makes bleak's release wait for ever, and is_connected is False for every dropped client, so that can't be told from here. Fixes meshcore-dev#135.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong (#135)
When a BLE link dropped on its own,
BLEConnection.handle_disconnectlet go of theBleakClientwithout calling itsdisconnect(). On Linux, bleak releases the D-Bus connection it opened for a link only indisconnect(), so that connection outlived the link. BlueZ kept its notify session and turned it back on at the next connect, and every notification then arrived once more for each link that had dropped before._cleanup_stale_client(#100) can't reach this client: by thenself.clienthas been reset, and a dropped client isn't connected.What changed
handle_disconnectdisconnects the client bleak passes it (notself.client), in a background task through_spawn_background, which keeps a reference to it. With the link already down, bleak'sdisconnect()leaves the device alone and only releases the bus.DISCONNECT_TIMEOUT(10 s, likeWRITE_TIMEOUT), and nothing waits on it. I chose a timeout over disconnecting only a client that really connected, because that can't be told from here:is_connectedis False for every dropped client. In bleak 3.1.0 a client whose own D-Bus connection never came up makesdisconnect()wait for ever (_busis set beforebus.connect()). bleak only calls back for a client that was connected, so in practice the release returns at once, but the bound means it never depends on that.auto_reconnectreuses the sameBLEConnection, but the task holds only the dropped client, so it never touches the new one. After an explicitdisconnect(), or_cleanup_stale_client, bleak calls back too, and the seconddisconnect()returns at once.Verified
tests/unit/test_ble_dropped_client.py, with a stub client:disconnect();disconnect()never returns holds up neitherhandle_disconnectnor a laterconnect(), and the release gives up at its bound;ConnectionManagernever disconnects the new client.main. The whole suite passes on 3.11 and 3.14.BleakClientwith a stand-in D-Bus connection releases its bus once after a drop (never onmain), and once on an explicitdisconnect(), as before.Fixes #135.