-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
How can I abort a running EF Core Query using GetAsyncEnumerator? #23652
Comments
Just to be sure... You say you get a TaskCanceledException from MoveNextAsync, but then you get a second TaskCanceledException on that same line? In your code above, the first exception should make you quit the loop and dispose the enumerator immediately, so it's not clear where the second TaskCanceledException would come from... Can you please clarify and possibly provide the specific code sample you're using? Aside from that, EF Core passes the given cancellation token to underlying operations performed on the ADO.NET provider, and it's their responsibility to cancel the query at the server - it's possible that something isn't working properly there. Will wait for an answer on the above before investigating. |
Hi Shay, Here's the exception messages. Line 99 is first exception (stopping enumerator, works as expected)
Second exception (is thrown once I see the query completed in SQL Profiler)
In case it helps, here's my connection string for ef core:
(ATM I would also guess that the issue lies within the ADO.net provider) |
@janschreier I'm still unclear on how the code you posted above could produce the two exceptions below... Can you please post a full, runnable code sample so that I can investigate? FYI your original method can be simplified by using WithCancellation and not interacting with the enumerator directly: public async IAsyncEnumerable<Blog> GetRueckrufe([EnumeratorCancellation] CancellationToken cancellationToken)
{
await using var ctx = new BlogContext();
await foreach (var blog in ctx.Blogs.AsAsyncEnumerable().WithCancellation(cancellationToken))
{
yield return blog;
}
} |
Seems like first chance exceptions case. When underlying will throw an exception (like above), the enclosing querying enumerable catch it, log a message and re-throw it. |
Thanks to both of you, I'll get back to you, once I have the example ready. |
Ehm, how should I start.
and I removed the important bit when trying to make a short example. Ivan Stoev guessed correctly on SO that the behavior I see is only shown, when using |
I am not able to get repro for this. Seeing only 1 exception for non-buffering, mars enabled split query scenario. I found few bugs in how cancellation token was being used in query. (probably few more were fixed also from the time issue was filed). Created PR #25918 Closing this issue as no repro. We can re-open if a stand-alone runnable repro is provided which shows the indicated behavior on latest packages. |
I am using EF Core 5.0 and have the following code:
It works as expected allowing me to populate a datagrid while more data is still loaded. If I cancel the provided CancelationToken, first I get a TaskCanceledException on the line MoveNextAsync() which is expected.
BUT: I can see in SQL Profiler that the SQL query itself is not aborted but always runs until all data is loaded and only then I get a second TaskCanceledException on that same line.
How do I abort the query itself?
provider and version information
EF Core version: 5.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 5.0.1
Operating system: Win 10 1809
IDE: Visual Studio 2019 16.8.3
The text was updated successfully, but these errors were encountered: