Skip to content

[BUG] Android pairing manager: operation identity, fast-path collisions and the paired devices list #129

Open

Description

@framinosona

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). PollBondStateAsync keeps its own seenBonding and runs for up to 60 s. An operation whose caller gave up is not cleared; it is replaced by the next SetPending for the same address. The old poller then matches the new operation (same kind, same address, lines 173-176) and still holds seenBonding = true from the first attempt. If the retry has just called CreateBond() and the state still reads Bond.None, the old poller reports it as a failed bond (line 183-185) and the retry fails without ever having been in Bond.Bonding.
  • Late callback (thread, AndroidBluetoothPairingManager.cs:271-320, BaseBluetoothPairingManager.cs:291-330). After the wait for device A has timed out, a PairAsync for device B sets IsPairing and creates a new completion source before NativePairAsync reaches SetPending. A bond result for A that arrives in that window still matches A's pending operation, passes the when IsPairing check, and OnPairSucceeded(A) completes B's completion source: the call for B returns true. The unpair and failure branches have the same shape. The checks are made on kind-wide flags (IsPairing, IsUnpairing), and OnPairSucceeded(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): PairAsync reports success without calling SetPending, so it also does so while an awaited UnpairAsync for the same device has called removeBond() and the state still reads Bond.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. UnpairAsync reports success while an awaited PairAsync has called CreateBond() and the state still reads Bond.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();    // empty

Expected Behavior

Open design questions, no fix proposed here.

  1. 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.
  2. Run the opposite-kind check in the fast paths too, without replacing the operation that is active.
  3. 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

Activity

  1. added
    bug
    on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions