Fix BLE reconnect leaving stale notification registration (duplicate notifications) - #100
Open
joeyleake wants to merge 1 commit into
Open
Conversation
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.
Fixes #97
Problem
After a BLE reconnect, every GATT notification from the device was
delivered twice.
BLEConnection.connect()always overwritesself.clientwith a fresh
BleakClienton every call, but never tore down the previousclient first — so its GATT notification subscription (
start_notifyonthe UART TX characteristic) stayed registered at the BlueZ D-Bus level
alongside the new one, and every notification fired for both.
Fix
Added
BLEConnection._cleanup_stale_client(), called once at the top ofconnect()before any of the three client-acquisition branches(pre-configured client / direct device / scan-by-address) replace
self.client. It best-effort disconnects an existing, connected client soits notify subscription is torn down before a new one is created. Failures
during this best-effort cleanup are logged at debug and swallowed, since
the goal is to clear stale state, not to block a reconnect attempt on a
client that's already in a bad state.
One incidental behavior note: the previous "client is already connected!!
weird" early-return branch is now unreachable within the same
connect()call, since cleanup disconnects first. That branch previously returned
early without re-establishing the notify subscription in that edge case;
now it goes through a normal reconnect. This seems like a correctness
improvement rather than a regression, but flagging it since it's a
behavior change either way.
Testing
Added
tests/unit/test_ble_reconnect_cleanup.py, which mocksBleakClientand asserts that calling
connect()twice in a row disconnects the firstclient before the second one is used.
Full suite: 180 passed, 2 failed (
test_send_cmd,test_advert_path_preserves_embedded_zero_bytes) — both failuresreproduce identically on a clean
maincheckout and are unrelated to thischange.