Skip to content

fix(tasks): observe the cancellation token when there is no timeout - #71

Merged
framinosona merged 1 commit into
mainfrom
fix/wait-better-async-token
Nov 22, 2026
Merged

framinosona merged 1 commit into
mainfrom
fix/wait-better-async-token

Conversation

@framinosona

Copy link
Copy Markdown
Member

Summary

TaskExtensions.WaitBetterAsync (Task and Task<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 returns task.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 SemaphoreSlim overloads were checked too: their no-timeout path already calls semaphore.WaitAsync(cancellationToken), so they are not affected.

Change Type

  • Feature
  • Bug fix
  • Refactor
  • Docs only
  • CI/build/tooling

Affected Areas

  • String / Numeric / Enum extensions
  • Collection extensions (Dictionary / List / Enumerable / ConcurrentDictionary)
  • Temporal extensions (DateTime / DateOnly / TimeOnly / TimeSpan)
  • Reflection / Assembly extensions
  • Comparison extensions
  • Tests
  • Docs

(Task extensions: not in the list above.)

Behavior And Compatibility

  • Public API changed
  • Exception behavior changed
  • No externally visible behavior change

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

  • Unit tests added/updated
  • Not applicable (explain)

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

  • README feature list updated
  • XML docs added/updated
  • Not applicable (explain)

Checklist

  • Commit messages follow Conventional Commits (type(scope): subject)
  • CI passes
  • Change is scoped to one logical unit of work

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
Copilot AI balanced review requested due to automatic review settings November 22, 2026 21:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@github-code-quality

Copy link
Copy Markdown

Code coverage overview

Languages: C#

C# / code-coverage-agent

main bbb6344 fix/wait-better-asyn... c0b71e7 +/-
90% 90% 0%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.

@github-actions

Copy link
Copy Markdown

Test Results

490 tests  +8   490 ✅ +8   2s ⏱️ ±0s
  1 suites ±0     0 💤 ±0 
  1 files   ±0     0 ❌ ±0 

Results for commit c0b71e7. ± Comparison against base commit bbb6344.

@framinosona
framinosona merged commit 17bdfcf into main Nov 22, 2026
14 checks passed
@framinosona
framinosona deleted the fix/wait-better-async-token branch November 22, 2026 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] WaitBetterAsync ignores the cancellation token when there is no timeout

2 participants