Skip to content

CancellationTokenSource.CancelAsync leaves WaitHandle undisposed when no callbacks are registered #136748

Description

@ReubenBond

Description

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:

using System;
using System.Threading;

var source = new CancellationTokenSource();
var handle = source.Token.WaitHandle;
await source.CancelAsync();
source.Dispose();
Console.WriteLine(handle.SafeWaitHandle.IsClosed); // False
handle.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

  1. added
    untriaged
    on Oct 31, 2026
  2. dotnet-policy-service commented on Oct 31, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
    See info in area-owners.md if you want to be subscribed.

  3. added and removed
    untriaged
    on Oct 31, 2026
  4. added this to the 12.0.0 milestone on Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions