Repository navigation
Android: refuse requests on a disconnected autoConnect client (#394) - #395
Conversation
An autoConnect client stays open after a link loss, so writeValue could still find the cached characteristic and the stack could accept the write and drop it without ever calling onCharacteristicWrite. The orphaned pending entry was then completed by the first callback of the reconnected link, so every later write lagged by one and timed out after 10 s - about one write per timeout with the per-device queue. Register a request only while its client's link is up, under the same lock the disconnect cleanup sweeps, so a concurrent submit is either swept or refused with deviceDisconnected. Apply that to reads, writes, descriptor reads/writes and CCCD subscriptions. As a backstop, a client kept open by autoConnect that reports STATE_CONNECTED again fails anything still pending from its previous link. readDescriptorValue now registers its pending result before issuing the read, as readValue already does.
…ct (#394) Follow-up to the autoConnect link-loss fix. - requestMtu, discoverServices and readRssi registered their pending result without the link check, so the same accept-and-drop gap could strand an entry for them. They now register under the disconnect cleanup's lock only while the link is up, and before issuing the request, as readValue does. - A subscription to a characteristic without a CCCD was never gated: there is no CCCD write to refuse, but setCharacteristicNotification() succeeds locally on a linkless client and reported a subscription the stack cannot deliver. It is now refused like any other request. - On STATE_CONNECTED the backstop now sweeps before connectedGatts marks the link up. Otherwise a request that passed the submission gate in that window was failed with deviceDisconnected after the stack had accepted it. - LinkLossOperationTest: theBackstopTouchesOnlyTheReconnectingClient registered its write before the disconnect sweep, which cleanUpConnection already failed, so the test passed with the backstop removed. Register it after the sweep, and add coverage for the sweep ordering, the CCCD-less subscription and the MTU/discovery/RSSI refusal. - MtuCompletionTest attaches its clients as connected, matching the new gate. - README/CHANGELOG: document the wider scope and the dependency on the platform reporting the disconnection. 122 Android unit tests, 0 failures. flutter analyze clean.
|
Tested the PR head (2d9d342) on the device from #394: Samsung SC-02M, Android 11 (API 30), Magic Motion Flamingo T. My app-side workaround (close + fresh connect on link loss) was removed for this test, so the plugin's in-place autoConnect reconnect is what's being exercised. Setup: Results:
Not tested: API 33+, iOS, and the read / descriptor / subscription / MTU / RSSI paths beyond initial setup. Looks good from my side. Thanks for the quick fix! |
|
Thanks for running this with the workaround removed — that is the setup that actually exercises the plugin's in-place autoConnect reconnect, and the numbers match what the fix is supposed to do: a 6–29 ms submission now fails in 3–28 ms with For the record on what this does not cover: MTU and RSSI are unchanged by this PR — a reconnect only sweeps anything still pending for the old link — so the device paths without evidence here are reads, descriptor reads/writes and CCCD subscriptions. They register under the same lock as the write and are covered by unit tests, but that is not the same as your four-cycle result. API 33+ is also unverified; there the write returns a status, so the link gate is a second line of defence rather than the only one. Much appreciated. |
#395's follow-up (the wider live-link gate) landed after this branch was cut, so its semantics are re-applied on top of the refactored structure: readRssi and discoverServices register under the gate before issuing, requestMtu refuses a linkless client before serving or registering, a subscription without a CCCD is refused, and the reconnect backstop sweeps before the link is marked up. The two device-less tests #395 added are ported to the fixture and the pending collections, and the backstop-ordering test observes the client state from the failed future's inline callback instead of the removed test hook.
Fixes #394.
Problem
With
connect(autoConnect: true), theBluetoothGattclient stays open after a link loss. A request submitted in the gap before Android reconnects it still finds the client in the registry and the characteristic in the cached service tree. Below API 33 the stack accepts the request (e.g.writeCharacteristicreturnstrue) and then drops it without a callback:The pending entry outlived the disconnect: cleanup had already run, and
STATE_CONNECTEDswept nothing. After the reconnect the stack reused the same client, soonCharacteristicWritecompleted that stale entry (oldest match) — every later write was completed by the next one's callback and the Dart call timed out after 10 s. With the per-device queue the peripheral got about one write per timeout. The same mechanism applies to reads, descriptor reads/writes and CCCD subscriptions.Fix
MutableList.addIfLinkedUp). Either the check sees the link gone and fails withdeviceDisconnected, or the entry is registered in time for cleanup to sweep it. Applied towriteValue,readValue,writeDescriptorValue,readDescriptorValueand the CCCD write insetNotifiable, after the existing lookups sodeviceNotFound,characteristicNotFoundand the write-support errors keep their precedence.autoConnectafterSTATE_DISCONNECTEDis tracked until it reportsSTATE_CONNECTEDagain; at that point anything still pending for that client is failed withdeviceDisconnected. Only that client's operations are touched, and a duplicateSTATE_CONNECTEDwithout an intervening link loss sweeps nothing. The existing cleanup sweep was extracted intofailPendingOperationsfor this.readDescriptorValuenow registers its pending result before issuing the read, asreadValuealready does, so an early response cannot be lost.connectedGattswhen noSTATE_DISCONNECTEDever arrives.This matches Apple, which fails a write with
deviceDisconnectedwhen its peripheral is not connected (#378).Tests
LinkLossOperationTest: a write after link loss fails immediately withdeviceDisconnectedand never reaches the stack; a write after the auto-reconnect completes on its own callback; a request left from the previous link is failed on reconnect; the backstop touches only the reconnecting client; a duplicateSTATE_CONNECTEDdoes not fail in-flight requests; reads/descriptor operations/subscriptions are refused the same way.PluginTestSupportgainsattachConnectedGatt; existing read/write/subscription tests now simulateSTATE_CONNECTEDso they keep exercising the stack-refusal path rather than the new gate../gradlew :universal_ble:testDebugUnitTest: 119 tests, 0 failures.flutter analyze: clean.Also documents the behaviour in
README.md(auto-connect) and bumps to 3.4.1 with a CHANGELOG entry.