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
Software versions
MySqlConnector version: 2.3.7
Server type (MySQL) and version: 8.0.31
.NET version: .NET6
Describe the bug
When I call an Async method from MySqlCommand with a long running query, passing a CancellationToken, it returns -1 after the token is cancelled.
This is possibly caused by some misunderstanding on my part of the documentation and what I'm trying to test.
Code sample
usingMySqlConnector;usingSystem.Diagnostics;varcancellationTokenSource=newCancellationTokenSource();varstopWatch=newStopwatch();try{awaitusingvarconnection=newMySqlConnection("Server=localhost;Port=3306;Database=<db>;Uid=<user>;Pwd=<pass>");awaitconnection.OpenAsync();Console.WriteLine("Executing a long running query...");stopWatch.Start();usingvarcommand=newMySqlCommand("SELECT SLEEP(5);",connection);vartask=command.ExecuteNonQueryAsync(cancellationTokenSource.Token);awaitTask.Delay(500);cancellationTokenSource.Cancel();Console.WriteLine($"Cancelling the query after {stopWatch.Elapsed.TotalMilliseconds}ms.");varresult=awaittask;Console.WriteLine($"Query Completed after {stopWatch.Elapsed.TotalMilliseconds}ms with result: {result}");}catch(OperationCanceledException){stopWatch.Stop();Console.WriteLine($"Operation was cancelled as expected after {stopWatch.Elapsed.TotalMilliseconds}ms.");}catch(Exceptionex){stopWatch.Stop();Console.WriteLine($"An exception occurred after: {stopWatch.Elapsed.TotalMilliseconds}ms. Message: {ex.Message}\nStackTrace: {ex.StackTrace}");}
Expected behavior
Reading the documentation and checking the unit tests examples, I expected that an OperationCanceledException should be throw after I cancel the ct. The query is canceled but it returns the number of rows (-1 in this case).
Executing a long running query...
Cancelling the query after 745.4734ms.
Operation was cancelled as expected after 753.6106ms.
Actual behavior
Executing a long running query...
Cancelling the query after 745.4734ms.
Query Completed after 753.6106ms with result: -1
If I change the code to cancel the ct after a delay, it works as expected, throwing an OperationCanceledException.
- var cancellationTokenSource = new CancellationTokenSource();- cancellationTokenSource .Cancel();+ var cancellationTokenSource = new CancellationTokenSource(1000);
The text was updated successfully, but these errors were encountered:
This is a side-effect of the "long running query" you're using for testing. SLEEP(n) is an interruptible method and the "soft cancellation" that MySqlConnector uses causes it to be gracefully cancelled.
If you use a "real" query, you'll get the cancellation behaviour you expect, e.g.,
usingvarcommand=newMySqlCommand(""" with ints as (select * from (values row(1), row(2), row(3), row(4), row(5), row(6), row(7), row(8), row(9)) temp) select * from ints a join ints b join ints c join ints d join ints e join ints f join ints g join ints h; """,connection);
Software versions
MySqlConnector version: 2.3.7
Server type (MySQL) and version: 8.0.31
.NET version: .NET6
Describe the bug
When I call an Async method from MySqlCommand with a long running query, passing a CancellationToken, it returns -1 after the token is cancelled.
This is possibly caused by some misunderstanding on my part of the documentation and what I'm trying to test.
Code sample
Expected behavior
Reading the documentation and checking the unit tests examples, I expected that an OperationCanceledException should be throw after I cancel the ct. The query is canceled but it returns the number of rows (-1 in this case).
Actual behavior
If I change the code to cancel the ct after a delay, it works as expected, throwing an OperationCanceledException.
The text was updated successfully, but these errors were encountered: