Repository navigation
Description
Bug Description
Three design-level gaps in AndroidBluetoothPairingManager (added in #127) and in the BaseBluetoothPairingManager it builds on. Line numbers are at 53f067a. They were reported in the review of #127; each item links its thread.
1. A result is not bound to the operation that produced it
Nothing identifies which operation a poller or a late callback belongs to, so a stale one can resolve a replacement operation.
- Stale poller (thread,
AndroidBluetoothPairingManager.cs:159-194).PollBondStateAsynckeeps its ownseenBondingand runs for up to 60 s. An operation whose caller gave up is not cleared; it is replaced by the nextSetPendingfor the same address. The old poller then matches the new operation (same kind, same address, lines 173-176) and still holdsseenBonding = truefrom the first attempt. If the retry has just calledCreateBond()and the state still readsBond.None, the old poller reports it as a failed bond (line 183-185) and the retry fails without ever having been inBond.Bonding. - Late callback (thread,
AndroidBluetoothPairingManager.cs:271-320,BaseBluetoothPairingManager.cs:291-330). After the wait for device A has timed out, aPairAsyncfor device B setsIsPairingand creates a new completion source beforeNativePairAsyncreachesSetPending. A bond result for A that arrives in that window still matches A's pending operation, passes thewhen IsPairingcheck, andOnPairSucceeded(A)completes B's completion source: the call for B returnstrue. The unpair and failure branches have the same shape. The checks are made on kind-wide flags (IsPairing,IsUnpairing), andOnPairSucceeded(device)/OnUnpairSucceeded(device)do not compare the device with the operation in progress. A result that arrives between the flag check and the handler, once the wait has ended, is reported as an unexpected result ([BUG] A native result that arrives after a cancelled or timed-out wait is reported as unexpected #126).
2. The fast paths skip the opposite-kind collision check
- Already bonded (thread,
NativePairAsync, lines 93-95):PairAsyncreports success without callingSetPending, so it also does so while an awaitedUnpairAsyncfor the same device has calledremoveBond()and the state still readsBond.Bonded. The caller is told the device is paired while the bond is about to go. - Already unpaired (same thread,
NativeUnpairAsync, lines 123-126): the inverse.UnpairAsyncreports success while an awaitedPairAsynchas calledCreateBond()and the state still readsBond.None.
3. The paired device list never holds the bond
(Thread.) After PairAsync succeeds, GetPairedDevices() stays empty and WaitForDeviceToBePairedAsync() never completes. The collection is private (BaseBluetoothPairingManager.cs:63), nothing in the base class adds to or removes from it (OnPairSucceeded and OnUnpairSucceeded only raise Paired, Unpaired and PairingStateChanged), and its comment mentions protected methods to maintain it that do not exist. The Android manager overrides IsPaired(string) to read the native bond state, so IsPaired is right, but the list, PairedDevicesAdded, PairedDevicesRemoved and the wait methods are not.
Related: #126 (late native result after an abandoned wait), #128 (concurrent calls merged across devices).
Code to Reproduce
Derived from reading the code; not run. Items 1 and 2 need a bonding peripheral, or a fake that raises the bond state changes in a chosen order. Item 3 shows with any device: call PairAsync, then GetPairedDevices().
await pairingManager.PairAsync(device); // returns true, the device is bonded
var paired = pairingManager.GetPairedDevices(); // emptyExpected Behavior
Open design questions, no fix proposed here.
- Bind each result to its operation, for example with a generation number checked again when a sampled result is delivered, or by handing each operation its own completion source that its poller and callbacks complete, and make "abandoned" and "replaced" transitions atomic with completion.
- Run the opposite-kind check in the fast paths too, without replacing the operation that is active.
- Give the base class a way to add and remove paired devices (a protected method or a hook on bond state changes), and have the Android manager use it, so the list, the events and the wait methods follow the bond state.
Library Version
main at 53f067a
Additional Context
- Hardening that does not need a design decision (already-cancelled tokens, one manager instance, the unpair poll past a
Bondedreading) is in fix(android): harden the pairing manager entry points #132.
Activity