You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] WaitBetterAsync ignores the cancellation token when there is no timeout #62
TaskExtensions.WaitBetterAsync(this Task, TimeSpan?, CancellationToken) and the Task<T> overload ignore the cancellation token when there is no timeout. With timeout null, zero or negative they return the original task unchanged, so a token that is cancelled while the task is pending has no effect. The cancellationToken parameter is documented as "A cancellation token to observe while waiting for the task to complete".
Found with a small console while working on Plugin.Bluetooth: a pending task, WaitBetterAsync(null, token), then token cancelled; the returned task kept pending until the task itself completed. With a timeout, Task.WaitAsync(timeout, token) is used and the token works. Plugin.Bluetooth's own waits go through this method (for example the scanner's wait for the platform's stop signal), so StopScanningAsync(cancellationToken: token) without a timeout cannot be cancelled either.
Only the Task and Task<T> overloads were read; the SemaphoreSlim overloads were not checked.
Code to Reproduce
varpending=newTaskCompletionSource().Task;usingvarcts=newCancellationTokenSource();varwait=pending.WaitBetterAsync(null,cts.Token);cts.Cancel();awaitTask.Delay(200);Console.WriteLine(wait.IsCompleted);// False; with a timeout it is True (cancelled)
Expected Behavior
Return the original task only when the token cannot be cancelled (!cancellationToken.CanBeCanceled); otherwise use task.WaitAsync(cancellationToken) so the token is observed with and without a timeout. Document the behaviour, and add a test for "null timeout, token cancelled".
Activity