Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JakobFerdinand opened this issue May 11, 2023 · 4 comments
Labels
✔️ Triage Done Issues that are triaged by dev team and are in investigation. ℹ️ Needs more Info Issues that have insufficient information to pursue investigations

Comments

@JakobFerdinand
Copy link

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

@roji roji transferred this issue from dotnet/efcore May 12, 2023
@roji
Copy link
Member

roji commented May 12, 2023

This is a bug in the SqlClient low-level driver; the same code works when using PostgreSQL.

@JRahnama JRahnama added the 🆕 Triage Needed For new issues, not triaged yet. label May 12, 2023
@JRahnama
Copy link
Contributor

@JakobFerdinand Can you include a simple repro that does not use EFCore and reproduce the issue?

@JRahnama JRahnama added the ℹ️ Needs more Info Issues that have insufficient information to pursue investigations label May 17, 2023
@roji
Copy link
Member

roji commented May 18, 2023

@JRahnama here you go; the following code prints False with SqlClient, but True with e.g. Npgsql.

Note that this specifically tests for passing an already-cancelled token to a SqlClient call. There are other paths where the token is cancelled only after the operation already started - these also need to throw an exception containing the token. Basically wherever you're creating an OperationCanceledException/TaskCanceledException, the triggering token should be passed in.

await using var connection = new SqlConnection("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false");
await connection.OpenAsync();

var cancelledToken = new CancellationToken(canceled: true);
await using var command = new SqlCommand("SELECT 1", connection);

try
{
    await command.ExecuteReaderAsync(cancelledToken);
}
catch (OperationCanceledException e)
{
    Console.WriteLine($"Token on exception matches: {e.CancellationToken == cancelledToken}");
}

@samsharma2700
Copy link
Contributor

This issue is on our radar and related to this #26 . We will evaluate internally and update.

@cheenamalhotra cheenamalhotra added ✔️ Triage Done Issues that are triaged by dev team and are in investigation. and removed 🆕 Triage Needed For new issues, not triaged yet. labels Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✔️ Triage Done Issues that are triaged by dev team and are in investigation. ℹ️ Needs more Info Issues that have insufficient information to pursue investigations
Projects
None yet
Development

No branches or pull requests

5 participants