Canceling SQL Server query with while loop hangs forever
See original GitHub issueSee StackOverflow post (https://stackoverflow.com/questions/48461567/canceling-query-with-while-loop-hangs-forever?noredirect=1#comment83955305_48461567) here for a full description of the issue.
Essentially, the issue is that for a certain query I am finding that calling CancellationTokenSource.Cancel()
hangs indefinitely instead of canceling the query. The same query cancels instantly in SQL Server Management Studio. Here is code the reproduces the issue:
void Main()
{
var cancellationTokenSource = new CancellationTokenSource();
var blocked = RunSqlAsync(cancellationTokenSource.Token);
Console.WriteLine(blocked.Wait(TimeSpan.FromSeconds(1))); // false (blocked in SQL as expected)
cancellationTokenSource.Cancel(); // hangs forever?!
Console.WriteLine("Finished calling Cancel()");
blocked.Wait();
}
public async Task RunSqlAsync(CancellationToken cancellationToken)
{
var connectionString = new SqlConnectionStringBuilder { DataSource = @".\sqlexpress", IntegratedSecurity = true, Pooling = false }.ConnectionString;
using (var connection = new SqlConnection(connectionString))
{
await connection.OpenAsync().ConfigureAwait(false);
using (var command = connection.CreateCommand())
{
command.CommandText = @"
WHILE 1 = 1
BEGIN
DECLARE @x INT = 1
END
";
command.CommandTimeout = 0;
Console.WriteLine("Running query");
await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
}
}
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:35 (26 by maintainers)
Top Results From Across the Web
Canceling query with while loop hangs forever
ADO.NET should send a query cancellation signal to SQL Server. I can see from the CPU usage, that SQL Server continues executing the...
Read more >Cancelling Query taking a long time - SQL Server Forums
Why can it take so long to cancel a query? I ran a select statement through Management Studio and it hangs saying 'Cancelling...
Read more >SSMS hangs for an extended period when closing a query ...
SSMS hangs for an extended period when closing a query tab or object explorer when a connection cannot be established to the SQL...
Read more >[Solved]-Canceling query with while loop hangs forever-C#
I'm going to make this an answer and say that this is a bug in ADO.NET. ADO.NET should send a query cancellation signal...
Read more >Loop inside stored procedure doesn't stop after query ...
I have a few nested stored procedures, which have a while loop at the end. When I stop the query, the loop keeps...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
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
Reopening issue as the change was reverted in 4.0.0-preview3 (https://github.com/dotnet/SqlClient/pull/1352)
It isn’t really a problem of how expensive the locks are because as you say they’re pretty cheap most of the time. The problem is that the EndExecute process inside the library is taking a lock very early and holding it for a long time and because that same lock is required for cancellation it prevents cancel, it effectively serializes it to occur after the wait so you’ll get a cancellation but it won’t happen until the query is finished which isn’t the intention.
the compareexchange isn’t for perf it’s just for making the state transition atomic and preventing both EndExecuteInternal (which is Close from the data reader confusingly) and Cancel can’t both be executing in a way that they think they’ve succeeded. Instead of locking to serialize we use the successful change of state to identify the “winner” and let them execute.