Repository navigation
fix(tasks): observe the cancellation token when there is no timeout - #71
Merged
Merged
Conversation
WaitBetterAsync on Task and Task<T> returned the original task whenever the timeout was null, zero or negative, so a token cancelled while the task was pending had no effect. Use Task.WaitAsync(token) in that case when the token can be canceled, and keep returning the original task otherwise. The SemaphoreSlim overloads already pass the token through. Fixes #62
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block the no-timeout cancellation-handling changes.
No open issues 🎉
What changed in this PR
Fixes #62 by making both WaitBetterAsync overloads observe cancellation when no timeout is applied, while preserving the original task for non-cancelable tokens.
Changes:
- Uses
Task.WaitAsync(cancellationToken)for cancelable waits with null, zero, or negative timeouts. - Clarifies XML documentation that cancellation abandons the wait without canceling the underlying task.
- Adds eight test cases covering cancellation and successful completion for both overloads.
| File | Description |
|---|---|
| Plugin.BaseTypeExtensions/TaskExtensions.cs | Updates no-timeout cancellation handling and XML documentation for both overloads. |
| Plugin.BaseTypeExtensions.Tests/TaskExtensionsTests.cs | Adds regression tests for cancellation and completion with cancelable tokens. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TaskExtensions.WaitBetterAsync(TaskandTask<T>overloads) now observes the cancellation token when there is no timeout. With a null, zero or negative timeout and a token that can be canceled, it returnstask.WaitAsync(cancellationToken). When the token cannot be canceled it still returns the original task unchanged.Fixes #62
Why
With no timeout the methods returned the original task straight away, so a token cancelled while the task was pending had no effect and the returned task stayed pending until the awaited task completed. With a timeout the token already worked, because
Task.WaitAsync(timeout, token)was used. Callers that wait through this method without a timeout, such as a scanner stop that takes a token, could not be cancelled.The
SemaphoreSlimoverloads were checked too: their no-timeout path already callssemaphore.WaitAsync(cancellationToken), so they are not affected.Change Type
Affected Areas
(Task extensions: not in the list above.)
Behavior And Compatibility
Signatures are unchanged. With a null, zero or negative timeout and a cancelable token, a token cancelled while the task is pending now makes the returned task complete as canceled (
OperationCanceledException) where it used to be ignored. Cancelling only abandons the wait, the awaited task keeps running. With a non-cancelable token (the default) nothing changes, including returning the same task instance.Tests
8 tests added for both overloads: no timeout (null, zero, negative) with a cancelled token completes as canceled while the awaited task stays pending, and a cancelable but uncancelled token still returns the result. The waits are bounded at 5 seconds so a regression fails instead of hanging. The no-timeout cases failed before the change (6 failures) and pass now. 490 tests pass locally.
Documentation
Checklist
type(scope): subject)