Skip to content

feat: add net11.0 target framework and modernize WithTimeoutAsync - #959

Draft
samtrion wants to merge 9 commits into
mainfrom
feat/net11-support
Draft

samtrion wants to merge 9 commits into
mainfrom
feat/net11-support

Conversation

@samtrion

@samtrion samtrion commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds net11.0 to the library and the tests, and modernizes the internals of WithTimeoutAsync on net8.0 and newer. All existing target frameworks are kept. The way the library works is not changed, on any target framework.

Refs #958

Changes

Target framework

  • net11.0 added to the library and the unit test project; the newest target framework keeps the apphost.
  • README, dev container, testEnvironments.json, new-project.ps1 (default net10.0) and the solution README template updated.

WithTimeoutAsync on net8.0+

  • The pending path uses Task.WaitAsync with ConfigureAwaitOptions.SuppressThrowing instead of a linked CancellationTokenSource, Task.Delay and Task.WhenAny, awaited directly in each public overload (one async frame instead of two).
  • netstandard2.0, net6.0 and net7.0 keep the previous implementation unchanged.
  • Measured against main on .NET 8, 10 and 11, task completing before the timeout: 4.5x to 9x faster (about 2000 to 3100 ns down to 300 to 490 ns) and 60 to 65 percent fewer bytes (about 1130 to 1220 B down to 400 to 490 B). Timeout elapses: time unchanged, 111 B more on .NET 8, 50 B less on .NET 10 and 11. Already completed tasks and WhenAll: unchanged.

New API (net8.0+)

  • WithTimeoutAsync(TimeSpan, TimeProvider, CancellationToken) for Task, Task<T>, ValueTask and ValueTask<T>, for deterministic timeout tests.

Behavior

Kept identical on every target framework and pinned by tests that also pass on the previous implementation:

  • no exception on timeout; Task<T> / ValueTask<T> still deliver the late result
  • exact OperationCanceledException (not TaskCanceledException) carrying the caller's token; a task that is canceled itself surfaces its own TaskCanceledException
  • faults rethrown unchanged, including a source that faults with its own TimeoutException
  • an already faulted Task / ValueTask still reports true
  • zero timeout waits for completion; token ignored after the timeout elapsed for the generic overloads
  • ArgumentOutOfRangeException with parameter name delay for timeouts beyond the timer limit
  • whatever happens first decides: the outcome is read from the result of WaitAsync, which records the first of completion, timeout and cancellation, like Task.WhenAny did
  • resuming thread when the task completes in time: inline on the completing thread on every target framework, as in the last release (2.1.515). The thread pool hop that net8.0+ had picked up through CancellationTokenSource.CancelAsync in feat(ValueTask): add WhenAll extension members and fix WithTimeoutAsync cancellation #956 is gone again.

Known remainder: if the timeout elapses and the task faults in the same instant, the result is still false, but the exception of the task counts as observed, so TaskScheduler.UnobservedTaskException is not raised for it.

Not changed, on purpose

  • No #if !NET11_0_OR_GREATER gating: the .NET 11 BCL has no ValueTask.WhenAll, ValueTask.WhenAny or ValueTask.WaitAsync.
  • Runtime Async (<Features>runtime-async=on</Features>) was tried for the net11.0 asset and removed again: every suspension is more expensive than with a compiler generated state machine, measured 1.3x to 1.5x for WhenAll over pending tasks and 104 to 152 B more per pending WithTimeoutAsync call.
  • WhenAll: untouched.
  • global.json, Directory.Build.props, Directory.Packages.props, .editorconfig: untouched.
  • NE_DOTNET_TARGETFRAMEWORKS already contains 11.x.

Verification

  • dotnet build -c Release: 0 warnings, 0 errors on all 7 target frameworks.
  • dotnet test: 1106 tests passed on net6.0 to net11.0, three consecutive runs.
  • dotnet pack: package contains lib/net11.0 next to all previous target framework folders.

Add net11.0 to the library and the unit test project. All existing
target frameworks (netstandard2.0, net6.0-net10.0) are kept. The newest
target framework keeps the apphost in the test project.

Tooling and docs follow: dev container installs SDK 11 plus the older
SDKs, test environments use the released .NET 10 and .NET 11 images,
the project script defaults to net10.0, and the README lists .NET 11.0.

Refs #958
…der overloads

On net8.0 and newer the pending path now waits with Task.WaitAsync and
ConfigureAwaitOptions.SuppressThrowing instead of a linked
CancellationTokenSource, Task.Delay and Task.WhenAny. This removes about
half of the allocations and a thread pool hop when the task completes in
time. netstandard2.0, net6.0 and net7.0 keep the previous implementation.

Observable behavior is the same on every target framework: no exception
on timeout, the exact OperationCanceledException with the caller's token,
faults rethrown unchanged, and the same ArgumentOutOfRangeException for
timeouts beyond the timer limit. New tests pin these cases and pass on the
previous implementation as well.

Add WithTimeoutAsync(TimeSpan, TimeProvider, CancellationToken) overloads
for Task, Task<T>, ValueTask and ValueTask<T> on net8.0 and newer, so
timeouts can be tested deterministically.

Refs #958
@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.04348% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.16%. Comparing base (86a768d) to head (611673b).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
.../TaskExtensions.WithTimeoutAsync(Task,Timespan).cs 88.37% 4 Missing and 1 partial ⚠️
...Extensions.WithTimeoutAsync(ValueTask,Timespan).cs 86.36% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #959      +/-   ##
==========================================
- Coverage   99.16%   97.16%   -2.00%     
==========================================
  Files          10       10              
  Lines         239      353     +114     
  Branches       40       61      +21     
==========================================
+ Hits          237      343     +106     
- Misses          0        6       +6     
- Partials        2        4       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Compile the net11.0 asset with the runtime-async feature, so the runtime
suspends and resumes async methods instead of compiler generated state
machines. Older target frameworks are compiled as before.

Refs #958
…sync

WhenAll awaits once per supplied task inside one method. With Runtime
Async every suspension and resumption costs more than with a compiler
generated state machine, which made WhenAll over pending tasks about 30
to 50 percent slower on net11.0. Opt the WhenAll methods out per method;
WithTimeoutAsync stays on Runtime Async.

The attribute is recognized by the compiler by name and has no effect on
target frameworks that are compiled without the runtime-async feature.

Refs #958
The pending path went through a second async method. Each public
overload now awaits Task.WaitAsync in its own frame, with the shared
logic in two synchronous helpers. This saves one async frame per pending
call: 152 to 168 B less and about 20 percent faster on net11.0.

Behavior is unchanged; netstandard2.0, net6.0 and net7.0 keep the
previous code path.

Refs #958
Measured on the final code, Runtime Async costs this library on every
path: 104 to 152 B more per pending WithTimeoutAsync call and about 20
percent more time, and 1.3x to 1.5x for WhenAll over pending tasks. With
every async method opted out the feature switch has no effect left, so
the switch and the per-method opt-out are removed again.

Refs #958
… on net8+

The Task.WaitAsync based path differed from the previous implementation
in three ways. All three are restored:

- When the task completes in time, the caller resumes on the thread pool
  again instead of inline on the thread that completed the task.
- The outcome is read from the result of WaitAsync, which records what
  happened first, instead of from the state of the task afterwards. A
  task that completes right after the timeout elapsed reports false
  again, and right after the caller's cancellation throws again.

Tests pin the resuming thread per target framework and a task that is
canceled itself while waiting.

Refs #958
…work

The released implementation resumed the caller inline on the thread that
completed the task. Since the cancellation fix, net8.0 and newer went
through CancellationTokenSource.CancelAsync, which moved the caller to
the thread pool as a side effect. Restore the initial behavior: no
thread pool hop on any target framework.

The first-wins outcome and the exact OperationCanceledException of the
cancellation fix are kept. A test pins the resuming thread.

Refs #958

This branch has not been deployed

No deployments
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.

1 participant