Skip to content

The CancellationToken in a TaskCanceledException on ToListAsync does not match #2037

Open
@JakobFerdinand

Description

@JakobFerdinand

Why does the TaskCanceledException to be the given one but it isn´t

using (MyDbContext dbContext = new())
{
    CancellationTokenSource cts = new();
    cts.Cancel();

    try
    {
        await dbContext.People.ToListAsync(cts.Token);
    }
    catch (TaskCanceledException ex) when (ex.CancellationToken == cts.Token) // that is what I´m expecting
    {
        Console.WriteLine("No problem"); 
    }
    catch (TaskCanceledException ex) when (ex.CancellationToken == CancellationToken.None) // that is what is happening
    {
        Console.WriteLine("Why?");
    }
}

Can you explain the given cts.Token is not passed to the exception?

provider and version information

EF Core version: 7.0.5
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More Info ℹ️Issues that have insufficient information to pursue investigationsTriage Done ✔️Issues that are triaged by dev team and are in investigation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions