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
CancellationTokenSource.CancelAsync leaves WaitHandle undisposed when no callbacks are registered #136748
After await CancellationTokenSource.CancelAsync() completes with no registered callbacks, disposing the source leaves an already-created CancellationToken.WaitHandle open. The equivalent synchronous Cancel() path closes it.
Reproduction
A standalone console application with no Orleans or third-party dependencies:
usingSystem;usingSystem.Threading;varsource=newCancellationTokenSource();varhandle=source.Token.WaitHandle;awaitsource.CancelAsync();source.Dispose();Console.WriteLine(handle.SafeWaitHandle.IsClosed);// Falsehandle.Dispose();// Clean up the reproduction's retained handle.
Expected behavior
Disposing the source after cancellation has completed closes its wait handle, including when there are no registered callbacks.
Actual behavior
Verified on Windows x64 with actual .NET 8.0.33 and .NET 10.0.14 runtimes, using SDK 11.0.100-rc.3.26522.148. The standalone reproduction targets net8.0 and net10.0 explicitly.
Cancellation method
Registered no-op callback
Handle closed after source disposal
Cancel()
No
true
Cancel()
Yes
true
await CancelAsync()
No
false
await CancelAsync()
Yes
true
The source token's WaitHandle getter does throw after source disposal, but the previously obtained handle remains usable/open in the failing case.
Investigation
The CancelAsync no-callback fast path transitions cancellation to the notifying state and returns Task.CompletedTask without running the callback-completion path. CancellationTokenSource.Dispose exchanges out the kernel event but intentionally skips disposing it while the source is in the notifying state. This combination appears to explain the retained handle.
Discovered while adding cancellation-source lifetime regressions for dotnet/orleans#11655. The behavior above is reproduced independently of Orleans. Searches for existing CancelAsync/WaitHandle, completion-state, and handle-disposal issues and PRs did not find a matching report.
Activity
dotnet-policy-service commentedon Oct 31, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.