Repository navigation
fix(libre3): recover from transient handshake errors instead of wedging - #2
Merged
Merged
Conversation
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 <noreply@anthropic.com>
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.
Problem
The native Libre 3 source (
Libre3BleClient) can go permanently silent after a transient BLE glitch, ending the CGM feed until AAPS is force-stopped. Observed live 2026-11-20: 3 h with no BG, loop running blind on profile basal (BG drifted 5.3 → 11.8 mmol).Root cause
A
stoppingflag is the kill-switch for reconnection — set only bydisconnect(), and once set every recovery path bails (scheduleReconnect, the connect watchdog, and the posted retry all early-return on it). Normal drops recover becauseonConnectionStateChange(DISCONNECTED)callsscheduleReconnect().But two error branches called
disconnect()on transient, recoverable conditions:Result.Error(an out-of-order / dropped notification frame mid-handshake —reassembly: sequence gap)Step.FailedstepSo a routine
GATT_CONNECTION_TIMEOUT(147)reconnect that re-ran the handshake and hit a sequence gap tore the whole thing down for good (status=257, then nothing). TheonCharacteristicWritepath already carries a comment warning about exactly this trap; these two siblings were missed.Fix
Route both error branches through
scheduleReconnect()instead ofdisconnect().scheduleReconnect()already tears down the GATT (disconnect()+close()) and retries on capped exponential backoff — it just doesn't setstopping. The scanner now self-heals like any normal drop.Testing
:libre3:compileFullDebugKotlin+:libre3:testFullDebugUnitTestpass.Libre3BleClientextendsBluetoothGattCallbackand the module has no Robolectric, so the reconnect path can't be exercised in the pure-JVM harness without extracting the stop-vs-retry decision into a pure helper (left as a possible follow-up).loopAPK and installed on the live phone for on-device verification.🤖 Generated with Claude Code