From c62c1b85ad70c2d191753dc53c9d6d92c2c572d6 Mon Sep 17 00:00:00 2001 From: alex-poor Date: Fri, 20 Nov 2026 21:04:15 +1300 Subject: [PATCH] fix(libre3): make scheduleReconnect idempotent to stop a reconnect storm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../kotlin/app/aaps/libre3/Libre3BleClient.kt | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/libre3/src/main/kotlin/app/aaps/libre3/Libre3BleClient.kt b/libre3/src/main/kotlin/app/aaps/libre3/Libre3BleClient.kt index 9c6f619376f7..32b22225ea5a 100644 --- a/libre3/src/main/kotlin/app/aaps/libre3/Libre3BleClient.kt +++ b/libre3/src/main/kotlin/app/aaps/libre3/Libre3BleClient.kt @@ -54,6 +54,16 @@ class Libre3BleClient( /** Set by [disconnect] so a deliberate teardown is not treated as a dropout to recover from. */ private var stopping = false private var reconnectAttempt = 0 + + /** + * True while a reconnect is already queued. [scheduleReconnect] tears the GATT down, and that + * teardown's own `onConnectionStateChange(DISCONNECTED)` re-enters [scheduleReconnect]; repeated + * error frames do the same. Without this guard each call spawned another `connectGatt`, so a + * single glitch became a storm of hundreds of overlapping connections (observed live 2026-11-20: + * 312 `status=147` disconnects over ~15 min before backoff throttled it back). The guard + * collapses all of that into one orderly retry. + */ + private var reconnectScheduled = false private val handler = android.os.Handler(android.os.Looper.getMainLooper()) private val opQueue = ConcurrentLinkedQueue<() -> Unit>() @@ -81,11 +91,16 @@ class Libre3BleClient( this.device = device stopping = false reconnectAttempt = 0 + reconnectScheduled = false openGatt() } private fun openGatt() { val d = device ?: return + // Defensive: never leave a previous client open when starting a new one. With the + // reconnectScheduled guard this should already be null, but a leaked gatt is exactly what + // turned one glitch into a connection storm, so close before replacing. + gatt?.let { it.disconnect(); it.close() } // autoConnect=false: the direct connection is far quicker, and we drive our own retry // schedule rather than leaving it to the platform's opaque background attempts. gatt = d.connectGatt(context, false, this, BluetoothDevice.TRANSPORT_LE) @@ -123,6 +138,7 @@ class Libre3BleClient( fun disconnect() { stopping = true authorized = false + reconnectScheduled = false handler.removeCallbacksAndMessages(null) gatt?.disconnect() gatt?.close() @@ -140,6 +156,11 @@ class Libre3BleClient( */ private fun scheduleReconnect() { if (stopping) return + // Coalesce: one reconnect in flight at a time. The gatt.disconnect() below fires a + // DISCONNECTED callback that re-enters here, and error frames can call in repeatedly — any + // of which, unguarded, would start an overlapping connectGatt and storm the radio. + if (reconnectScheduled) return + reconnectScheduled = true handler.removeCallbacks(watchdog) authorized = false gatt?.disconnect() @@ -151,7 +172,10 @@ class Libre3BleClient( RECONNECT_MAX_DELAY_MS ) reconnectAttempt++ - handler.postDelayed({ if (!stopping) openGatt() }, delay) + handler.postDelayed({ + reconnectScheduled = false + if (!stopping) openGatt() + }, delay) } // --- operation queue ----------------------------------------------------