Fix/notify leak availability - #3
Merged
Merged
Conversation
added 2 commits
July 9, 2026 01:44
Every (re)connect subscribed to the response characteristic via EnableNotifications but never unsubscribed, and the characteristic wasn't even retained so it couldn't be. The tinygo/x/bluetooth Linux backend registers a D-Bus signal channel, match rule and goroutine per subscription; since BlueZ reuses the same characteristic object path when reconnecting to the same device, every stale goroutine's path check keeps matching. So after N reconnects a single BLE notification was delivered N times — the logs showed one login/status reply fanning out ~50× (and each duplicate re-published to MQTT). Retain the response characteristic and call EnableNotifications(nil) in Disconnect to tear the watcher down (it also cleans up if the link already dropped). Also unsubscribe when EnableNotifications fails part-way (StartNotify error), which otherwise leaks a half-registered watcher.
On-demand refresh could leave a blind stuck "unavailable" in HA even though it was talking fine: refreshState calls ensureConnected once (which publishes "offline" if its own connect fails), then RequestStatus reconnects inside Send and succeeds — but nothing on that path put availability back "online" (the earlier reconcile only covered ensureConnected and the command path). Publish "online" from onBLEEvent: receiving any frame proves the link is up, so this reconciles availability regardless of which path (re)established it. publishAvailability is change-tracked and mutex-guarded, so firing it from the BLE-callback goroutine is cheap and race-free.
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.
No description provided.