Skip to content

Fix/notify leak availability - #3

Merged
viggfred merged 2 commits into
mainfrom
fix/notify-leak-availability
Jul 8, 2026
Merged

Fix/notify leak availability#3
viggfred merged 2 commits into
mainfrom
fix/notify-leak-availability

Conversation

@viggfred

@viggfred viggfred commented Jul 8, 2026

Copy link
Copy Markdown
Owner

No description provided.

Viggo Fredriksen 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.
@viggfred
viggfred merged commit ab560a5 into main Jul 8, 2026
2 checks passed
@viggfred
viggfred deleted the fix/notify-leak-availability branch July 8, 2026 23:51
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.

1 participant