EF Core async query continues after CancellationTokenSource cancellation
0 reputation · 10 Feb 2025, 06:08 UTC
EF Core async query continues after CancellationTokenSource cancellation
In supported EF Core versions, a DbContext runs an asynchronous query and passes a CancellationToken to ToListAsync. After the token source is cancelled, the application receives an exception, but it is unclear whether the database server stops executing the command or only the client stops waiting.
The goal is to determine whether EF Core reliably propagates cancellation to the underlying provider command and how CommandTimeout interacts with token-triggered cancellation. Behavior may differ between providers such as SQL Server and PostgreSQL, and the surrounding transaction scope may determine whether partial work rolls back.
Does EF Core always signal the database provider to cancel the command when a CancellationToken is triggered? Which exception type should be expected, OperationCanceledException or a provider-specific DbException? Does CommandTimeout override or combine with cancellation-token timing?