Repository navigation
Description
Describe the bug
CA2007 reports an await foreach over an IAsyncEnumerable<T> that has no ConfigureAwait call, but not one over source.WithCancellation(cancellationToken). WithCancellation creates its ConfiguredCancelableAsyncEnumerable<T> with continueOnCapturedContext: true, so that loop resumes on the captured context just like the unconfigured one.
The analyzer skips any await foreach whose collection is a ConfiguredCancelableAsyncEnumerable<T>. ConfigureAwait(bool) and WithCancellation(CancellationToken) both return that type, so the type alone does not show whether the context was configured.
Steps to reproduce
Create a class library with dotnet new classlib --name Repro, delete Class1.cs, and add this .editorconfig next to the project file:
root = true
[*.cs]
dotnet_diagnostic.CA2007.severity = warningAdd Sample.cs:
namespace Repro;
public static class Sample
{
public static async Task RunAsync(IAsyncEnumerable<int> source, CancellationToken cancellationToken)
{
await foreach (var item in source) { }
await foreach (var item in source.WithCancellation(cancellationToken)) { }
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false)) { }
}
}Run dotnet build.
Expected behavior
CA2007 reports the first and second loops. The third loop calls ConfigureAwait(false), so it is not reported.
Actual behavior
CA2007 reports only the first loop:
<repro>/Sample.cs(7,36): warning CA2007: Consider calling ConfigureAwait on the awaited task (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007)
Is this a regression?
No. The await foreach check has skipped ConfiguredCancelableAsyncEnumerable<T> collections since dotnet/roslyn-analyzers#6683 added it.
Are there any workarounds?
Write .ConfigureAwait(false) after WithCancellation(...) by hand. Nothing reports a loop where it is missing.
dotnet --info output
Trimmed to the SDK, runtime environment, and host sections:
.NET SDK:
Version: 11.0.100
Commit: 297acf930f
Workload version: 11.0.100-manifests.08e30c08
MSBuild version: 18.12.2-1.26529.118+297acf930
Runtime Environment:
OS Name: ubuntu
OS Version: 26.10
OS Platform: Linux
RID: linux-x64
Base Path: <dotnet-root>/sdk/11.0.100
Host:
Version: 11.0.0
Architecture: x64
Commit: 297acf930fIDE version
None; reproduced with dotnet build.
Other details
Telling the two calls apart means looking at the invocation chain that produced the collection, for example whether it contains a ConfigureAwait call, rather than at the collection's type.
dotnet/roslyn-analyzers#3703 is related: it asks for CA2007 to cover any type that has a ConfigureAwait(bool) method. This report is about a type the rule already handles.
Drafted by Claude (Anthropic AI assistant).
Activity
github-actions commentedon Nov 12, 2026
🎯 Agentic Issue Triage
Triage Assessment:
untriagedremoved)@krwq| @dotnet/dotnet-analyzershigh- The issue names the CA2007 analyzer and its source file, which maps directly to the CODEOWNERS section.➡️ Summary: CA2007 skips
await foreachoverWithCancellation(...)because it only checks for theConfiguredCancelableAsyncEnumerable<T>type, missing the unconfigured context. Inspect the invocation chain forConfigureAwaitinstead of the type.github-actions commentedon Nov 13, 2026
🍪 Issue Monster selected this for Copilot
I have identified this issue as a good candidate for automated resolution and requested assignment to the Copilot coding agent.
If assignment succeeds, the Copilot coding agent will analyze the issue and create a pull request with the fix.
Om nom nom! 🍪