You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When an already-cancelled CancellationToken is specified in a query, EF core seems to log the OperationCanceledException that is thrown when EF tries to open the connection.
It's caught with catch (Exception e), logged as "Opening connection to database '...' on server '...'", and rethrown,
I think cancellations should be rethrown without being logged 🤔.
Your code
staticasyncTaskMain(){varsp=newServiceCollection().AddDbContext<Db>().AddLogging((options)=>options.SetMinimumLevel(LogLevel.Debug).AddSimpleConsole().AddFilter("Microsoft.EntityFrameworkCore.Query",LogLevel.None).AddFilter("Microsoft.EntityFrameworkCore.Infrastructure",LogLevel.None)).BuildServiceProvider();usingvarscope=sp.CreateScope();vardb=scope.ServiceProvider.GetRequiredService<Db>();vars=newCancellationTokenSource();s.Cancel();try{Console.WriteLine("query");awaitdb.Entities.FromSqlRaw("SELECT 1 AS id").ToListAsync(s.Token);}catch(OperationCanceledException){Console.WriteLine("cancelled; ignored");}}[Table("entity")]classEntity{[Column("id")]publicrequiredintId{get;set;}}partialclassDb(DbContextOptions<Db>options):DbContext(options){publicrequiredDbSet<Entity>Entities{get;init;}}
Output below. The highlighted lines are caused by the OperationCanceledException being logged by EF.
query
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20005]
Creating DbConnection.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20006]
Created DbConnection. (10ms).
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
Opening connection to database 'main' on server 'owo.db'.
-fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]- An error occurred using the connection to database 'main' on server 'owo.db'.
cancelled; ignored
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20007]
Disposing connection to database 'main' on server 'owo.db'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20008]
Disposed connection to database 'main' on server 'owo.db' (0ms).
Stack traces
Verbose output
EF Core version
9.0.2
Database provider
No response
Target framework
.NET 9.0.200
Operating system
No response
IDE
No response
The text was updated successfully, but these errors were encountered:
We are seeing the same issue on a Postgres instance in prod, but I can't reproduce it locally with your example code.
In production we are logging canceled user requests and whenever this db error occurs, we have an info cancel log behind from our side.
Error message from the diagnostic logger: "An error occurred using the connection to database '"xxxx"' on server '"/xxxxxxx.s.PGSQL.5432"'.". Name: Microsoft.EntityFrameworkCore.Database.Connection.ConnectionError and Type: 20004
There is nothing inside NpgsqlConnection that is calling cancellationToken.ThrowIfCancellationRequested() which I can see.
EF Core version
Microsoft.EntityFrameworkCore 9.0.1
Database provider
Postgres 17.2 and Npgsql.EntityFrameworkCore.PostgreSQL 9.0.3
Bug description
When an already-cancelled
CancellationToken
is specified in a query, EF core seems to log theOperationCanceledException
that is thrown when EF tries to open the connection.It's caught with
catch (Exception e)
, logged as "Opening connection to database '...' on server '...'", and rethrown,efcore/src/EFCore.Relational/Storage/RelationalConnection.cs
Lines 748 to 786 in a1541cb
I think cancellations should be rethrown without being logged 🤔.
Your code
Output below. The highlighted lines are caused by the
OperationCanceledException
being logged by EF.Stack traces
Verbose output
EF Core version
9.0.2
Database provider
No response
Target framework
.NET 9.0.200
Operating system
No response
IDE
No response
The text was updated successfully, but these errors were encountered: