Repository navigation
Description
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
framinosona commentedon Sep 24, 2026
5.
nulltimeout never actually applies the documented defaultWhere:
BaseBluetoothRemoteDevice.Connection.cs,RefreshIsConnectedAsync/WaitForIsConnectedAsync(and by extensionConnectAsync/DisconnectAsync).What happens:
Docs/API-Reference/README.mddocuments two distinct meanings - passnullto use "the default timeout (typically 30s)", passTimeout.InfiniteTimeSpanfor "no timeout" at all. The actual code has always treatednullas unbounded (skips the timeout wrapper entirely, same asTimeout.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.
framinosona commentedon Nov 9, 2026
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 ofConnectAsync(await ConnectionTcs.Task) andDisconnectAsync(await DisconnectionTcs.Task).What happens: a second caller that arrives while an attempt is running awaits the owner's
TaskCompletionSourcedirectly, with neither its owntimeoutnor itscancellationToken. If the owner times out or is cancelled, itsfinallysetsConnectionTcs/DisconnectionTcstonullbut never completes that TCS, so nothing ever completes the task the second caller is waiting on.Effect: the second
ConnectAsync/DisconnectAsynccall never returns, even when it was given a timeout.Read from
mainat2a14cc0, 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
ConnectAsyncandDisconnectAsyncwait withWaitBetterAsync(timeout, cancellationToken). Withtimeout == nullthat helper ignores the token (Laerdal/Plugin.BaseTypeExtensions#62), so a call made with only aCancellationTokencannot 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.