SqlCommand.Cancel() does not work on Linux
See original GitHub issueSqlCommand.Cancel()
doesn’t appear to work using System.Data.SqlClient on Linux (Ubuntu 19.04, .NET Core SDK 3.0.100-preview5-011568) - the command runs to completion, and only then throws SqlException. The issue does not seem to repro on Windows (with .NET Core).
Repro:
using (var cmd = new SqlCommand("WAITFOR DELAY '00:00:05'", conn))
{
// sync cancellation
Task.Delay(1000).ContinueWith(t => cmd.Cancel());
var sw = Stopwatch.StartNew();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine($"Sync cancellation: caught {e.GetType().Name} after {sw.ElapsedMilliseconds}ms");
// Does not interrupt the call (completes after 5 seconds) but still generates SqlException at the end
}
// async cancellation
sw.Restart();
try
{
await cmd.ExecuteNonQueryAsync(new CancellationTokenSource(1000).Token);
}
catch (Exception e)
{
Console.WriteLine($"Async cancellation: caught {e.GetType().Name} after {sw.ElapsedMilliseconds}ms");
// Interrupts and generates SqlException immediately
}
}
/cc @divega @karinazhou
Issue Analytics
- State:
- Created 4 years ago
- Comments:15 (11 by maintainers)
Top Results From Across the Web
How i can stop sql query execution from .net code?
You are using the SqlCommand.Cancel method to cancel any async operation in progress. Then you can use it like this:
Read more >SqlCommand.Cancel() method does not always work
Cancel() to be executed. If the user presses the cancel button shortly after the query is launched, the command object cancels the query....
Read more >Thread: Cancelling a long running query
It doesnt appear that the query is cancelled, because when I click the first button again to re-run the query I get a...
Read more >SqlCommand.Cancel Method (System.Data.SqlClient)
If there is nothing to cancel, nothing occurs. However, if there is a command in process, and the attempt to cancel fails, no...
Read more >Working with the SQL Server command line (sqlcmd)
In this new chapter, we are going to show the following examples in a local SQL Server using SQL Server command line (sqlcmd)....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve tried the idea above. It works as far as I can tell. The full manual test suite runs through without unusual problems (TVPMain really need fixing at some point).
It’s a bit annoying fix because of the current packet cloning which I’ve fixed in the managed networking rework PR. It will also be blocked by the same issue as that PR https://github.com/dotnet/corefx/pull/35363 where an unrelated test that fails anyway will continue to fail so it won’t get merged. So once things get unblocked I’ll be able to fix this.
It’s a managed implementation issue. I’ve reproduced it on windows using your test script above and have added a new test.
There are
lock(this)
statements in the managed send and receive methods on SniTCPHandle. So what happens is you start a query which ultimately tracks down into Receive which takes the lock and then from another thread you call Cancel which tracks down into SendAttention and that tries to Send and blocks on the lock. When the receive times out the lock is released the send works, the attention is processed and everything unravels in the correct order just a lot later that you want it to.I think send and receive aren’t mutually exclusive but that each one is not concurrent with itself. Changing the lock to be operation specific consumes a bit of object space but there shouldn’t be too many tcp handles around since they’re reused. It will need some careful testing because locking in this library is definitely “here be dragons” territory.