Skip to content

CA2007 does not report await foreach over WithCancellation(...) without ConfigureAwait #57090

Open

Description

@glen-84

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 = warning

Add 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:       297acf930f

IDE 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

  1. added
    untriaged
    on Nov 12, 2026
  2. locked and limited conversation to collaborators on Nov 12, 2026
  3. unlocked this conversation on Nov 12, 2026
  4. removed
    untriaged
    on Nov 12, 2026
  5. github-actions commented on Nov 12, 2026

    @github-actions
    Contributor

    🎯 Agentic Issue Triage

    Triage Assessment:

    ➡️ Summary: CA2007 skips await foreach over WithCancellation(...) because it only checks for the ConfiguredCancelableAsyncEnumerable<T> type, missing the unconfigured context. Inspect the invocation chain for ConfigureAwait instead of the type.

    Generated by 🏷️ Issue Triage for #57090 · copilot · auto · ~16 AIC · ⌖ ~1 AIC · ⊞ 14.6K · ◷

  6. github-actions commented on Nov 13, 2026

    @github-actions
    Contributor

    🍪 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! 🍪

    🍪 Om nom nom by Issue Monster Orchestrator · copilot · auto · ~13 AIC · ⌖ ~15 AIC · ⊞ 13.2K · ◷

  7. removed their assignment
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions