Skip to content

Connect/disconnect robustness: pre-existing timeout, double-reporting, and hang issues #59

Open

Description

@framinosona

Summary

While reviewing PR #55 (async connection-state refresh + attempt-correlation redesign), several issues were found in ConnectAsync/DisconnectAsync and their platform implementations that predate PR #55 and are unrelated to its own fix. Filing here to track separately rather than expanding that PR's scope further.

1. Android/Windows double-report a single connect/disconnect failure

Where: AndroidBluetoothRemoteDevice.NativeConnectAsync/NativeDisconnectAsync, WindowsBluetoothRemoteDevice.NativeConnectAsync/NativeDisconnectAsync (Bluetooth.Maui.Platforms.Droid / Bluetooth.Maui.Platforms.Win).

What happens: each of these methods calls OnConnectFailedAsync/OnDisconnectAsync inside its own catch, then rethrows. The exception then propagates to ConnectAsync/DisconnectAsync's own outer catch in BaseBluetoothRemoteDevice.Connection.cs, which calls the same completion method again with the same exception.

Effect: the caller correctly receives the exception once, but the second call's TrySetException/TrySetResultOrException fails silently (already completed) and falls through to OnUnexpectedDisconnection/ReportBestEffortFailure - firing the public UnexpectedDisconnection event and a WARNING-level log for a failure that was already properly reported and handled.

Confirmed this exact call shape already exists on main - predates any of the recent connection-refresh work.

2. ConnectAsync/DisconnectAsync can exceed their documented timeout

Where: BaseBluetoothRemoteDevice.Connection.cs, ConnectAsync/DisconnectAsync.

What happens: each method awaits multiple sequential steps (an initial refresh, optionally waiting for advertisement, waiting for the native TCS, a post-operation refresh) and passes the same, full caller-supplied timeout to each independently, instead of tracking a shared remaining budget.

Effect: under slow conditions (e.g. a stalled native callback near the deadline), the overall call can take roughly 2x (or more, depending on how many stages run) the documented timeout.

Note: WaitForIsConnectedAsync was already fixed (PR #55) to track a shared remaining budget across its own two steps - this issue is about applying the same pattern to ConnectAsync/DisconnectAsync.

3. Windows abandon-cleanup can hang indefinitely

Where: WindowsBluetoothRemoteDevice.NativeDisconnectAsync.

What happens: ConnectAsync's "give up and retract the native request" cleanup path calls NativeDisconnectAsync(timeout: null, cancellationToken: CancellationToken.None). On Windows this awaits WaitForPropertyToBeOfValue(GattSessionStatus, Closed, timeout: null, ...) with no timeout at all.

Effect: if the native GATT session status never transitions to Closed (e.g. a stuck WinRT session), the cleanup call - whose entire purpose is to give up promptly - never returns.

4. Apple's cancellation/fault reporting can double-report in a narrow race

Where: AppleBluetoothRemoteDevice.NativeRefreshIsConnectedAsync.

What happens: the cancellation registration and the awaited WaitAsync don't coordinate ownership of a fault. If the dispatched task faults right as cancellation fires, both the registration's StartAndForget and the normal awaited-propagation path can report the same exception.

Effect: a single fault can reach BluetoothUnhandledExceptionListener twice in this specific timing window.


None of these were introduced by PR #55 - found while reviewing its diff, tracked here separately so they don't block or expand that PR.

Activity

  1. added
    bug
    on Sep 24, 2026
  2. framinosona commented on Sep 24, 2026

    @framinosona
    MemberAuthor

    5. null timeout never actually applies the documented default

    Where: BaseBluetoothRemoteDevice.Connection.cs, RefreshIsConnectedAsync/WaitForIsConnectedAsync (and by extension ConnectAsync/DisconnectAsync).

    What happens: Docs/API-Reference/README.md documents two distinct meanings - pass null to use "the default timeout (typically 30s)", pass Timeout.InfiniteTimeSpan for "no timeout" at all. The actual code has always treated null as unbounded (skips the timeout wrapper entirely, same as Timeout.InfiniteTimeSpan) - no default timeout is substituted anywhere in this call chain. This predates PR #55.

    Effect: ConnectAsync()/DisconnectAsync()/WaitForIsConnectedAsync() called with their documented defaults can wait forever on a stuck native call instead of honoring the ~30s default the docs promise.

    Note: deciding what the actual per-operation default values should be is a design call, not something to guess at while fixing this - tracked here rather than decided unilaterally in PR #55.

  3. framinosona commented on Nov 9, 2026

    @framinosona
    MemberAuthor

    6. Merged callers wait without a timeout or token, and hang if the owner gives up

    Where: BaseBluetoothRemoteDevice.Connection.cs, the "already in progress" branches of ConnectAsync (await ConnectionTcs.Task) and DisconnectAsync (await DisconnectionTcs.Task).

    What happens: a second caller that arrives while an attempt is running awaits the owner's TaskCompletionSource directly, with neither its own timeout nor its cancellationToken. If the owner times out or is cancelled, its finally sets ConnectionTcs/DisconnectionTcs to null but never completes that TCS, so nothing ever completes the task the second caller is waiting on.

    Effect: the second ConnectAsync/DisconnectAsync call never returns, even when it was given a timeout.

    Read from main at 2a14cc0, not reproduced. The redesign in #55 has a fix for it (9079266, "terminate owned TCS on abandonment"), which is the part I would bring back first.

    Addition to 5: cancellation without a timeout

    ConnectAsync and DisconnectAsync wait with WaitBetterAsync(timeout, cancellationToken). With timeout == null that helper ignores the token (Laerdal/Plugin.BaseTypeExtensions#62), so a call made with only a CancellationToken cannot be cancelled while it waits for the native callback.


    #55 is closed in favour of this issue: the part of it that fixed the iOS refresh shipped as #63, and the redesign stays on its branch as a reference. If a concrete failure appears, I'll bring items back as small PRs.

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