When a CancellationToken is available but not forwarded to a method that has an overload accepting one, the cancellation request is
silently ignored.
The caller’s intent to cancel long-running operations is not propagated, which means cooperative cancellation is broken:
The rule detects any accessible token source:
CancellationToken parameter at any positionCancellationToken field or propertyCancellationToken? parameter, field, or property; forwarded as .GetValueOrDefault()AsyncLocal<CancellationToken> or ThreadLocal<CancellationToken> parameter, field, or property;
forwarded as .ValueCancellationToken obtained via a property of an accessible object, e.g. _cts.Token,
HttpContext.RequestAbortedThe rule does not flag calls where CancellationToken.None is explicitly passed as an argument. Pass
CancellationToken.None to suppress the rule when cancellation must not apply, such as during disposal or post-cancellation cleanup.
This follows the "point of no cancellation" pattern: once a method has started work it cannot safely revert, cancellation should no longer be
honored, and any further token should be forwarded as CancellationToken.None instead of the original token:
public async ValueTask DisposeAsync()
{
await _stream.FlushAsync(CancellationToken.None); // Compliant: disposal must complete regardless of cancellation
}
Forward the accessible CancellationToken to the overload that accepts it.
public async Task SaveAsync(string path, string content, CancellationToken token = default)
{
await File.WriteAllTextAsync(path, content, Encoding.UTF8); // Noncompliant - pass 'token' to allow cancellation
}
public async Task SaveAsync(string path, string content, CancellationToken token = default)
{
await File.WriteAllTextAsync(path, content, Encoding.UTF8, token);
}
[TestClass]
public class MyTests
{
public TestContext TestContext { get; set; }
[TestMethod]
public async Task MyTest()
{
await httpClient.GetStringAsync(url); // Noncompliant
}
}
[TestClass]
public class MyTests
{
public TestContext TestContext { get; set; }
[TestMethod]
public async Task MyTest()
{
await httpClient.GetStringAsync(url, TestContext.CancellationToken);
}
}
When the noncompliant call is inside a static local function or static lambda, the token cannot be captured from the
enclosing scope. Instead, extend the parameter list to accept a CancellationToken and pass it from the call site.
public async Task ProcessAsync(CancellationToken token = default)
{
await ProcessItemAsync("item");
static async Task ProcessItemAsync(string item)
{
await File.WriteAllTextAsync("log.txt", item, Encoding.UTF8); // Noncompliant
}
}
public async Task ProcessAsync(CancellationToken token = default)
{
await ProcessItemAsync("item", token);
static async Task ProcessItemAsync(string item, CancellationToken token)
{
await File.WriteAllTextAsync("log.txt", item, Encoding.UTF8, token);
}
}
private readonly SemaphoreSlim _semaphore = new(1, 1);
public void Acquire(CancellationToken? ct = null)
{
if (ct.HasValue)
_semaphore.Wait(ct.Value);
else
_semaphore.Wait(); // Noncompliant - pass 'ct.GetValueOrDefault()' to allow cancellation
}
private readonly SemaphoreSlim _semaphore = new(1, 1);
public void Acquire(CancellationToken? ct = null)
{
_semaphore.Wait(ct.GetValueOrDefault());
}