From 8a7946972bec15202c1e9ce1588e639eafb9dc8e Mon Sep 17 00:00:00 2001 From: Strycher Date: Thu, 24 Sep 2026 03:37:43 -0400 Subject: [PATCH] fix(#696): close leaked GATT handles in the vendored Android plugin Three changes to FlutterBluePlusPlugin.java, per the #689 diagnosis: 1. onConnectionStateChange(CONNECTED): close the handle a map overwrite replaces. A replaced handle is unreachable forever and holds the link and a GATT client slot until process death, which is exactly what the owner's force-stop evidence showed. 2. disconnect: 5 s backstop. If Android never delivers DISCONNECTED for the handle we asked to disconnect, force gatt.close(), untrack it, and synthesize the disconnect event (mirrors the existing cancel-while-connecting path, reason code 23789259). Also caps the Dart layer's 35 s wait at ~5 s. 3. disconnect with no tracked handle now logs at WARN so the orphan fingerprint is visible in exported logs. Vendored version marked 8.2.2+offband.1. Co-Authored-By: Claude Fable 5 --- .../FlutterBluePlusPlugin.java | 58 ++++++++++++++++++- .../flutter_blue_plus_android/pubspec.yaml | 2 +- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/packages/flutter_blue_plus_android/android/src/main/java/com/lib/flutter_blue_plus/FlutterBluePlusPlugin.java b/packages/flutter_blue_plus_android/android/src/main/java/com/lib/flutter_blue_plus/FlutterBluePlusPlugin.java index 8f4b0954..903b636f 100644 --- a/packages/flutter_blue_plus_android/android/src/main/java/com/lib/flutter_blue_plus/FlutterBluePlusPlugin.java +++ b/packages/flutter_blue_plus_android/android/src/main/java/com/lib/flutter_blue_plus/FlutterBluePlusPlugin.java @@ -790,7 +790,11 @@ public class FlutterBluePlusPlugin implements } } if (gatt == null) { - log(LogLevel.DEBUG, "already disconnected"); + // Offband #694: WARN, not DEBUG. This no-op means the Dart + // layer's disconnect resolves instantly with nothing done, + // which is invisible in exported logs at DEBUG level and is + // exactly the fingerprint of an orphaned-handle latch. + log(LogLevel.WARNING, "disconnect: no tracked gatt handle for " + remoteId + " (no work done)"); result.success(false); // no work to do return; } @@ -802,6 +806,47 @@ public class FlutterBluePlusPlugin implements // disconnect gatt.disconnect(); + // Offband #694: if Android never delivers the DISCONNECTED + // callback, gatt.close() (which normally runs in + // onConnectionStateChange) never happens and this process holds + // the link until it dies. Backstop: if this exact handle is + // still tracked as connected after 5s, force-close it and + // synthesize the disconnect event, mirroring what the + // cancel-while-connecting path below already does. + if (mConnectedDevices.get(remoteId) == gatt) { + final BluetoothGatt disconnectingGatt = gatt; + final String backstopRemoteId = remoteId; + new Handler(Looper.getMainLooper()).postDelayed(() -> { + boolean stillTracked; + try { + acquireMutex(mMethodCallMutex); + stillTracked = mConnectedDevices.get(backstopRemoteId) == disconnectingGatt; + if (stillTracked) { + log(LogLevel.WARNING, "disconnect not confirmed after 5s, force-closing: " + backstopRemoteId); + mConnectedDevices.remove(backstopRemoteId); + mCurrentlyConnectingDevices.remove(backstopRemoteId); + try { disconnectingGatt.close(); } catch (Exception e) { /* best effort */ } + } + } finally { + mMethodCallMutex.release(); + } + if (stillTracked) { + // random number defined by this fork, alongside + // flutter blue plus's own 23789258 cancel code. + int bmForceClosedErrorCode = 23789259; + + // see: BmConnectionStateResponse + HashMap backstopResponse = new HashMap<>(); + backstopResponse.put("remote_id", backstopRemoteId); + backstopResponse.put("connection_state", bmConnectionStateEnum(BluetoothProfile.STATE_DISCONNECTED)); + backstopResponse.put("disconnect_reason_code", bmForceClosedErrorCode); + backstopResponse.put("disconnect_reason_string", "disconnect not confirmed, force-closed"); + + invokeMethodUIThread("OnConnectionStateChanged", backstopResponse); + } + }, 5000); + } + // was connecting? if (mCurrentlyConnectingDevices.get(remoteId) != null) { @@ -2200,7 +2245,16 @@ public class FlutterBluePlusPlugin implements // connected? if(newState == BluetoothProfile.STATE_CONNECTED) { // add to connected devices - mConnectedDevices.put(remoteId, gatt); + BluetoothGatt replaced = mConnectedDevices.put(remoteId, gatt); + + // Offband #694: an overwrite here orphans the replaced handle. + // Nothing else holds a reference, so it can never be closed and + // it keeps the link (and a GATT client slot) until the process + // dies. Close it before it is lost. + if (replaced != null && replaced != gatt) { + log(LogLevel.WARNING, "closing replaced gatt handle: " + remoteId); + try { replaced.close(); } catch (Exception e) { /* best effort */ } + } // remove from currently connecting devices mCurrentlyConnectingDevices.remove(remoteId); diff --git a/packages/flutter_blue_plus_android/pubspec.yaml b/packages/flutter_blue_plus_android/pubspec.yaml index 0be97859..570615b4 100644 --- a/packages/flutter_blue_plus_android/pubspec.yaml +++ b/packages/flutter_blue_plus_android/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_blue_plus_android description: Android implementation of the flutter_blue_plus plugin. -version: 8.2.2 +version: 8.2.2+offband.1 homepage: https://github.com/chipweinberger/flutter_blue_plus environment: