Skip to content

fix(libre3): make scheduleReconnect idempotent to stop a reconnect storm - #3

Merged
alex-poor merged 1 commit into
mainfrom
fix/libre3-reconnect-storm
Nov 20, 2026
Merged

alex-poor merged 1 commit into
mainfrom
fix/libre3-reconnect-storm

Conversation

@alex-poor

Copy link
Copy Markdown
Owner

Problem

The permanent-wedge fix (db6e5fe) routed transient handshake errors to scheduleReconnect() instead of disconnect(). That is re-entrant: scheduleReconnect() calls gatt.disconnect(), whose async onConnectionStateChange(DISCONNECTED) callback also calls scheduleReconnect(), and repeated error frames do the same. With no guard, each call started another connectGatt.

Observed live 2026-11-20: a single glitch produced 312 status=147 disconnects across overlapping GATT connections over ~15 min with no BG, until exponential backoff throttled it. It self-recovered (unlike the old wedge) but hammered the shared BLE radio and dragged recovery out to ~15 min instead of seconds. Pump link was unaffected.

Fix

  • Add a reconnectScheduled guard so only one reconnect is ever in flight; re-entrant and repeated calls become no-ops. The storm collapses to a single orderly retry on backoff.
  • Close any existing gatt at the top of openGatt() (belt-and-suspenders against a leaked client — exactly what turned one glitch into a storm).
  • Reset the flag on connect() / disconnect().

Testing

  • :libre3:compileFullDebugKotlin + :libre3:testFullDebugUnitTest pass.
  • Built as a loop APK and installed live (3.4.2.3-hovorka-reconnstorm): loop looping, BG flowing, normal reconnect cycle clean, 0× status=147 bursts.
  • No JVM unit test: Libre3BleClient extends BluetoothGattCallback, no Robolectric in the module. The storm-path single-retry is structurally guaranteed but will only be observed on the next real glitch.

🤖 Generated with Claude Code

The permanent-wedge fix routed transient handshake errors to
scheduleReconnect(), but that re-entered itself: scheduleReconnect()'s own
gatt.disconnect() fires onConnectionStateChange(DISCONNECTED), which calls
scheduleReconnect() again, and repeated error frames do the same. With no
guard, each call started another connectGatt.

Observed live 2026-11-20: a glitch produced 312 status=147 disconnects
across overlapping GATT connections over ~15 min (no BG the whole time)
before exponential backoff throttled it. It self-recovered — unlike the old
wedge — but hammered the radio and dragged recovery out.

Add a `reconnectScheduled` guard so only one reconnect is ever in flight,
collapsing the storm to a single orderly retry. Also close any existing
gatt at the top of openGatt() as a belt-and-suspenders against leaks, and
reset the flag on connect()/disconnect().

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@alex-poor
alex-poor merged commit d604840 into main Nov 20, 2026
1 check failed
@alex-poor
alex-poor deleted the fix/libre3-reconnect-storm branch November 20, 2026 08:09
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