What this thread does
Do first: enter an await using scope immediately after each successful resource acquisition, before constructing the next resource.
Short answer
As of November 2026, current C#/.NET docs distinguish cooperative cancellation from resource disposal. CancelAfter requests cancellation; the nested disposal pattern awaits both IAsyncDisposable instances’ DisposeAsync methods. Set the operation deadline from the service budget and leave room for cleanup; DisposeAsync() has no cancellation-token parameter (cancellation, async disposal).
Choose / don't choose
- Async-only cleanup: choose
IAsyncDisposablewhen the resource contract requires asynchronous release and all owners await it; don't assume a synchronous consumer calls it (disposal pattern). - Both interfaces: choose
IDisposableplusIAsyncDisposablewhen supported consumers use both cleanup paths and synchronous cleanup is meaningful; don't implement sync disposal as a hidden unbounded sync-over-async wait (pattern, Toub FAQ). - Context-free cleanup: choose
ConfigureAwait(false)for reusable cleanup that does not require the caller's context; don't use it when cleanup itself must touch UI-thread-owned objects (FAQ).
Not for: HTTP retry policy; see polly-v8-or-microsoft-http-resilience-for-dotnet-retries.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
CancellationTokenSource.CancelAfter(int) | Delay in milliseconds; values below -1 throw ArgumentOutOfRangeException; choose positive delay from the operation budget | Current .NET API | CancelAfter, timer implementation |
IAsyncDisposable.ConfigureAwait | Configure continuation marshalling for the disposal await | Current async-disposal docs | pattern, FAQ |
await using | Enter after each acquisition; the documented nested pattern awaits both IAsyncDisposable instances’ DisposeAsync methods | Current C# reference | using |
CancellationTokenSource.Dispose() | After users and callbacks are finished; it does not request cancellation | Current .NET API | Dispose |
IDisposable / IAsyncDisposable | Implement both only when both supported ownership paths need cleanup; either path must release what it owns | Current disposal guidance | pattern |
Order of operations
1. Create an operation-owned source, capture its token and pass it to cooperating operations. Arm CancelAfter once against the intended deadline; subsequent calls reset the delay until cancellation occurs (CancelAfter). 2. Acquire the first resource and immediately establish its disposal scope. Acquire the second inside that scope, then establish its own scope; dispose in reverse acquisition order. (using, nested patterns). 3. Await the operation inside these scopes; let cancellation unwind them. Use await using (resource.ConfigureAwait(false)) for the disposal await if context capture is undesirable. (FAQ). 4. If abandoning concurrent child operations, request cancellation and await their completion before closing shared resources. Cancel() is a signal, not a join. Finish callbacks and all use of source members before disposing the source (cooperative contract, JamesNK's August 1, 2024 report). 5. Dispose the source after work and owned cleanup complete. After disposal, the Token property throws ObjectDisposedException; capturing a token is not permission to race source disposal with active work (report, Dispose).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
ObjectDisposedException | Accessing CancellationTokenSource.Token after disposal throws ObjectDisposedException | Await users and callbacks before disposal; capture tokens before starting work | JamesNK report |
ArgumentOutOfRangeException | Integer CancelAfter delay is less than -1 | Reject invalid budgets; use documented finite delays or intentional infinite timeout | CancelAfter, timer implementation |
| Earlier resource leaks when later construction throws (symptom) | An exception from the later constructor prevents proper disposal in the stacked pattern | Nest acquisition inside the first await-using scope or use immediate using declarations | nested pattern |
Missing
- Numeric
CancelAftertimeout: the operation duration, service deadline and cleanup allowance are unspecified. There is no universal safe timeout, and cooperative cancellation cannot force termination or bound arbitrary disposal.
Checked
2026-11-12; current C# using and .NET cancellation/disposal docs, Stephen Toub's December 11, 2019 FAQ and dated JamesNK report read. No C# operation or disposal program executed.
Started from a post by @Microsoft, packaged by the editor. Original post