From a0ccd9f9e52dcdc81638bb84986444f910d22abc Mon Sep 17 00:00:00 2001 From: alex-poor Date: Fri, 20 Nov 2026 17:20:12 +1300 Subject: [PATCH] fix(libre3): recover from transient handshake errors instead of wedging Two BLE error paths called disconnect(), which sets `stopping` and permanently disables reconnection (the onCharacteristicWrite path already documents this trap). A transient out-of-order/dropped frame during the security re-handshake therefore killed the CGM feed until AAPS was force-stopped. Observed live 2026-11-20: a routine GATT_CONNECTION_TIMEOUT(147) reconnect re-ran the handshake, hit `reassembly: sequence gap`, called disconnect(), and left the loop blind on profile basal for 3 h (BG 5.3 -> 11.8 mmol). Route both the reassembler Error and the security-session Failed step through scheduleReconnect() (which already tears down the GATT and retries on capped backoff) so the scanner self-heals like any normal drop. Co-Authored-By: Claude Opus 4.8 --- .../main/kotlin/app/aaps/libre3/Libre3BleClient.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libre3/src/main/kotlin/app/aaps/libre3/Libre3BleClient.kt b/libre3/src/main/kotlin/app/aaps/libre3/Libre3BleClient.kt index bacf41c1fa4c..9c6f619376f7 100644 --- a/libre3/src/main/kotlin/app/aaps/libre3/Libre3BleClient.kt +++ b/libre3/src/main/kotlin/app/aaps/libre3/Libre3BleClient.kt @@ -301,7 +301,10 @@ class Libre3BleClient( } is Libre3SecuritySession.Step.Failed -> { listener.onError(step.reason) - disconnect() + // A failed handshake step is recoverable: the next clean connect re-runs it from + // scratch. Do NOT call disconnect() — that sets `stopping` and permanently kills + // reconnection (same trap the onCharacteristicWrite path warns about). Retry instead. + scheduleReconnect() } } } @@ -425,7 +428,11 @@ class Libre3BleClient( when (val result = r.offer(value)) { is Libre3Framing.Reassembler.Result.NeedMore -> Unit is Libre3Framing.Reassembler.Result.Error -> { - listener.onError("reassembly: ${result.reason}"); disconnect() + // An out-of-order / dropped BLE frame mid-handshake is a transient radio glitch, + // not a reason to stop forever. disconnect() sets `stopping` and permanently + // disables reconnection (observed live 2026-11-20: 3 h with no BG). scheduleReconnect() + // tears down the GATT and re-handshakes cleanly on backoff. + listener.onError("reassembly: ${result.reason}"); scheduleReconnect() } is Libre3Framing.Reassembler.Result.Complete -> { reassembler = null