Metalama.Community.AutoCancellationToken
2026.1.25
Prefix Reserved
dotnet add package Metalama.Community.AutoCancellationToken --version 2026.1.25
NuGet\Install-Package Metalama.Community.AutoCancellationToken -Version 2026.1.25
<PackageReference Include="Metalama.Community.AutoCancellationToken" Version="2026.1.25" />
<PackageVersion Include="Metalama.Community.AutoCancellationToken" Version="2026.1.25" />
<PackageReference Include="Metalama.Community.AutoCancellationToken" />
paket add Metalama.Community.AutoCancellationToken --version 2026.1.25
#r "nuget: Metalama.Community.AutoCancellationToken, 2026.1.25"
#:package Metalama.Community.AutoCancellationToken@2026.1.25
#addin nuget:?package=Metalama.Community.AutoCancellationToken&version=2026.1.25
#tool nuget:?package=Metalama.Community.AutoCancellationToken&version=2026.1.25
Metalama.Community.AutoCancellationToken
Automatically propagates CancellationToken parameter to async methods and method calls within them.
This is a Metalama aspect. It transforms your source code during compilation.
⚠️ Proof of concept
This aspect is a proof of concept, not a production-ready tool. Do not rely on it to make an application correctly cancellable.
Cancellation is a semantic decision, but this aspect works purely syntactically: it passes a token to a call because an overload happens to accept one. It has no way to know which calls should honour cancellation and which must always run to completion — cleanup, disposal, compensating writes, audit logging. Cancelling those causes resource leaks and partially applied state.
Read the limitations below before using it. Several of them fail silently.
Example
Your code:
[AutoCancellationToken]
class C
{
async Task MakeRequests(CancellationToken ct)
{
using var client = new HttpClient();
await MakeRequest(client);
}
private static async Task MakeRequest(HttpClient client) => await client.GetAsync("https://example.org");
}
What gets compiled:
class C
{
async Task MakeRequests(CancellationToken ct)
{
using var client = new HttpClient();
await MakeRequest(client, ct);
}
// The original signature is kept, so existing callers still compile.
private static Task MakeRequest(HttpClient client) => MakeRequest(client, default);
// The body moves to a new overload that takes the token.
private static async Task MakeRequest(HttpClient client, CancellationToken cancellationToken)
=> await client.GetAsync("https://example.org", cancellationToken);
}
Notice that the call to MakeRequest now passes ct, that MakeRequest gained an overload taking a
CancellationToken, and that the call to HttpClient.GetAsync passes it on.
Installation
Install the NuGet package: dotnet add package Metalama.Community.AutoCancellationToken.
How to use
Add [AutoCancellationToken] to the types where you want it to apply.
By annotating a type with [AutoCancellationToken], you add cancellation to its async methods. Specifically:
- For each
asyncmethod that has noCancellationTokenparameter, the original method is kept as a forwarder and an overload taking aCancellationTokenis added. The original signature is never changed, so existing callers keep compiling. - A
CancellationTokenargument is added to calls withinasyncmethods where:CancellationTokencan be added as a last argument and the added argument corresponds to aCancellationTokenparameter (e.g. it's not aparams object[]parameter or a generic parameter). The added argument can result in calling a different overload of the method, or specifying a value for an optional parameter.- The call is not in a
staticlocal function or astaticlambda, which cannot capture the enclosing token. - The containing method doesn't have two or more
CancellationTokenparameters, since it wouldn't be clear which one to use.
Limitations
The aspect decides what to cancel from syntax alone. It cannot tell a cancellable operation from one that must
complete. If a method in an annotated type performs cleanup, disposal or logging through a call that offers a
CancellationToken overload, the aspect will pass the token and that work becomes cancellable. Review the
generated code before depending on it.
Callers of the original signature never cancel. The forwarder passes default, i.e. CancellationToken.None.
Code outside the aspect's reach keeps compiling, but gains no cancellation at all — silently. Only callers within
annotated types have the token propagated to them.
Members involved in virtual dispatch are not transformed. virtual, abstract and override methods, and
explicit interface implementations, are skipped entirely and receive no cancellation support. A method's signature
is its dispatch contract: adding an overload would let a derived class the aspect cannot see override only the
original signature, so calling the token-taking overload would run the base body and silently ignore the override.
Making only the overload virtual would instead break existing override declarations.
This one is not silent. When such a member calls something that would have accepted a token, the aspect reports
the warning ACT001, so you can see where cancellation was dropped and add a CancellationToken parameter by hand:
warning ACT001: 'RunAsync' is not given a CancellationToken parameter because it is virtual, abstract, an override
or an explicit interface implementation, and changing such a signature would break the dispatch contract. Its call
to 'GetAsync' therefore runs without cancellation. Declare a CancellationToken parameter on 'RunAsync' explicitly
to propagate cancellation.
Declaring the parameter yourself resolves it: the aspect then propagates that token through the body as usual.
Propagation is not transitive, so a chain breaks at the first method the aspect does not own. The aspect adds a
token to the async methods of annotated types and then passes it to calls that already accept one. It never adds a
parameter to a method merely because doing so is what would let the token travel further:
[AutoCancellationToken]
class Caller
{
// WorkAsync is in a type the aspect was not applied to, so it never gains a CancellationToken parameter and
// there is no overload to pass the token to. The chain stops here, even though the cancellable call is one
// hop away.
async Task RunAsync() => await Helper.WorkAsync();
}
static class Helper
{
public static async Task WorkAsync() => await Cancellable();
}
Closing that gap means deciding, for every method, whether it transitively reaches a call that can be cancelled — and adding a parameter to one method changes the answer for all of its callers, so the analysis has to be iterated to a fix point. That is possible, but generally out of scope for a build-time transformation because of its cost, and impossible across an assembly boundary. Annotating every type involved in a call chain is the practical workaround.
Conversely, within an annotated type the aspect over-approximates. Because it does not analyse which methods
need a token, every async method gets one, including methods that never use it:
public Task DoNothingAsync() => DoNothingAsync(default);
public async Task DoNothingAsync(CancellationToken cancellationToken) => await Task.Yield(); // unused
The aspect only sees the current compilation. It cannot add tokens to methods in referenced assemblies, and it cannot know whether a type deriving from yours will be transformed.
Several constructs are still skipped without being reported. A method whose last parameter is a params array,
a method that already declares two or more CancellationToken parameters, and a call whose candidate overload takes
a generic final parameter are all left untouched with no diagnostic. Only the virtual-dispatch case above reports
ACT001; for these, nothing tells you the aspect did nothing.
Async iterators are not given [EnumeratorCancellation]. The token added to an async IAsyncEnumerable<T>
method is not wired up to the enumerator, so a token passed to WithCancellation is unconsumed. This is the one
limitation the compiler does warn about, as CS8425.
If you need cancellation you can rely on, thread CancellationToken through explicitly.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Metalama.Framework (>= 2026.1.25)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Metalama.Community.AutoCancellationToken:
| Package | Downloads |
|---|---|
|
Shinya.Core
Shinya.Framework |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2026.1.25 | 604 | 8/21/2026 |
| 2026.1.24 | 444 | 8/13/2026 |
| 2026.1.23 | 96 | 8/10/2026 |
| 2026.1.22 | 356 | 8/5/2026 |
| 2026.1.21 | 2,579 | 7/21/2026 |
| 2026.1.20 | 1,347 | 7/15/2026 |
| 2026.1.19 | 3,194 | 6/27/2026 |
| 2026.1.18 | 1,028 | 6/10/2026 |
| 2026.1.17 | 120 | 6/5/2026 |
| 2026.1.16 | 273 | 6/3/2026 |
| 2026.1.15-rc | 2,016 | 5/15/2026 |
| 2026.1.14-rc | 408 | 5/13/2026 |
| 2026.1.13-preview | 107 | 5/13/2026 |
| 2026.1.12-preview | 407 | 5/12/2026 |
| 2026.1.11-preview | 544 | 5/6/2026 |
| 2026.1.10-preview | 898 | 4/29/2026 |
| 2026.0.24 | 118 | 6/30/2026 |
| 2026.0.23 | 109 | 6/27/2026 |
| 2026.0.22 | 115 | 5/15/2026 |
| 2025.1.19 | 114 | 6/30/2026 |