dotnet / dotnet/SqlClient

The CancellationToken in a TaskCanceledException on ToListAsync does not match

Open
#2,037 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
989
Forks
340
Avg merge
4d 19h
Merged PRs (30d)
72

Description

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

```cs
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.