Skip to content

Disconnect a BLE client whose link dropped - #136

Merged
fdlamotte merged 1 commit into
meshcore-dev:mainfrom
hamsolodev:fix/135-dropped-client-disconnect
Nov 25, 2026
Merged

fdlamotte merged 1 commit into
meshcore-dev:mainfrom
hamsolodev:fix/135-dropped-client-disconnect

Conversation

@hamsolodev

Copy link
Copy Markdown
Contributor

What was wrong (#135)
When a BLE link dropped on its own, BLEConnection.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 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 then self.client has been reset, and a dropped client isn't connected.

What changed

  • handle_disconnect disconnects the client bleak passes it (not self.client), in a background task through _spawn_background, which keeps a reference to it. With the link already down, bleak's disconnect() leaves the device alone and only releases the bus.
  • The release is bounded by DISCONNECT_TIMEOUT (10 s, like WRITE_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_connected is False for every dropped client. In bleak 3.1.0 a client whose own D-Bus connection never came up makes disconnect() wait for ever (_bus is set before bus.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.
  • The other paths are unchanged. auto_reconnect reuses the same BLEConnection, but the task holds only the dropped client, so it never touches the new one. After an explicit disconnect(), or _cleanup_stale_client, bleak calls back too, and the second disconnect() returns at once.

Verified

  • Unit tests in tests/unit/test_ble_dropped_client.py, with a stub client:
    • a dropped client gets exactly one disconnect();
    • a client whose disconnect() never returns holds up neither handle_disconnect nor a later connect(), and the release gives up at its bound;
    • an auto-reconnect through ConnectionManager never disconnects the new client.
  • All three fail on current main. The whole suite passes on 3.11 and 3.14.
  • Checked with bleak 3.1.0 on Linux/BlueZ: a real BleakClient with a stand-in D-Bus connection releases its bus once after a drop (never on main), and once on an explicit disconnect(), as before.

Fixes #135.

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.
@hamsolodev
hamsolodev marked this pull request as ready for review November 25, 2026 08:38
@fdlamotte
fdlamotte merged commit a0e0b5a into meshcore-dev:main Nov 25, 2026
2 checks passed
@hamsolodev
hamsolodev deleted the fix/135-dropped-client-disconnect branch November 25, 2026 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BLE: after a dropped link, the BleakClient is never disconnected, and every notification arrives twice after reconnecting

2 participants