G4: F16 — re-register disconnect callback after BLE client reset

Why: handle_disconnect resets self.client to self._user_provided_client.
That client's disconnected_callback was not re-registered, so subsequent
BLE disconnects after a successful reconnect cycle were missed —
ConnectionManager never learned the link dropped again. Now re-registers
via set_disconnected_callback with a hasattr guard and try/except for
bleak version compatibility. The next connect() call would also re-create
the client with the callback, but this closes the gap between disconnect
and reconnect.
Refs: Forensics report finding F16
This commit is contained in:
Matthew Wolter 2026-04-11 20:24:24 -07:00
parent 9150a49c6f
commit fe0dcac90f

View file

@ -154,6 +154,17 @@ class BLEConnection:
self.client = self._user_provided_client
self.device = self._user_provided_device
# Re-register disconnect callback on the reset client so subsequent
# disconnects after a reconnect cycle are still detected.
if self.client is not None and hasattr(self.client, 'set_disconnected_callback'):
try:
self.client.set_disconnected_callback(self.handle_disconnect)
except Exception:
# set_disconnected_callback may not be available on all bleak
# versions; the next connect() call will re-create the client
# with the callback anyway.
pass
if self._disconnect_callback:
asyncio.create_task(self._disconnect_callback("ble_disconnect"))