Skip to content

fix(libre3): recover from transient handshake errors instead of wedging - #2

Merged
alex-poor merged 1 commit into
mainfrom
fix/libre3-reassembly-permanent-wedge
Nov 20, 2026
Merged

alex-poor merged 1 commit into
mainfrom
fix/libre3-reassembly-permanent-wedge

Conversation

@alex-poor

Copy link
Copy Markdown
Owner

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 stopping flag is the kill-switch for reconnection — set only by disconnect(), and once set every recovery path bails (scheduleReconnect, the connect watchdog, and the posted retry all early-return on it). Normal drops recover because onConnectionStateChange(DISCONNECTED) calls scheduleReconnect().

But two error branches called disconnect() on transient, recoverable conditions:

  • the reassembler Result.Error (an out-of-order / dropped notification frame mid-handshake — reassembly: sequence gap)
  • the security-session Step.Failed step

So 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). The onCharacteristicWrite path already carries a comment warning about exactly this trap; these two siblings were missed.

Fix

Route both error branches through scheduleReconnect() instead of disconnect(). scheduleReconnect() already tears down the GATT (disconnect() + close()) and retries on capped exponential backoff — it just doesn't set stopping. The scanner now self-heals like any normal drop.

Testing

  • :libre3:compileFullDebugKotlin + :libre3:testFullDebugUnitTest pass.
  • No unit test added: Libre3BleClient extends BluetoothGattCallback and 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).
  • Built as a loop APK and installed on the live phone for on-device verification.

🤖 Generated with Claude Code

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>
@alex-poor
alex-poor merged commit db6e5fe into main Nov 20, 2026
1 check failed
@alex-poor
alex-poor deleted the fix/libre3-reassembly-permanent-wedge branch November 20, 2026 04:30
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