-
Notifications
You must be signed in to change notification settings - Fork 299
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
SQL.OperationCancelled Returns SqlException Instead of OperationCancelledException #51
Comments
See also https://github.com/dotnet/corefx/issues/9245, where apparently SqlException sometimes gets thrown. |
Hi, |
It looks like this exception throwing logic has existed in SqlClient for a long time. |
@karinazhou the issue is that OperationCancelledException is treated in a special way. A Task that aborts with that exception is marked as cancelled (as opposed to fail), and this distinction can be quite important. One thing I'm wondering about, though: this specific exception may be thrown as a result of |
@karinazhou This particular |
@roji @thefringeninja Thank you for the explanation and I can understand the difficulties you mention when handling InvalidOperationException for cancellation scenario. After looking a bit more into code, this SQL.OperationCancelled() is mainly called by SQLCommand and TdsParser. As what @roji suspects, the SQLCommand does derive from DbCommand and probably that is one of the reasons why InvalidOperationException is used here to keep the old way. I do see some cancellation token usage in System.Data.Sqlclient for Async mode though. One thing we are concerning about is the backward compatibility of different exception types. This will definitely impact those who have expected InvalidOperationException when the operation is cancelled by the user. |
As recently announced in the .NET Blog, focus on new SqlClient features an improvements is moving to the new Microsoft.Data.SqlClient package. For this reason, we are moving this issue to the new repo at https://github.com/dotnet/SqlClient. We will still use https://github.com/dotnet/corefx to track issues on other providers like System.Data.Odbc and System.Data.OleDB, and general ADO.NET and .NET data access issues. |
@karinazhou I think it may be acceptable for However, I did a quick test to get a full picture of the situation, and had some surprising results:
In other words, it's not clear in which actual situation InvalidOperationException is thrown - @thefringeninja did you encounter this in an actual runtime scenario, or see this in the codebase? Code used for the above test: 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
}
} |
My mistake @roji, after looking more closely at our build logs it is indeed |
@thefringeninja in that case this issue is a dup of https://github.com/dotnet/corefx/issues/9245 and can be closed. @karinazhou it may still be worth investigating who actually calls |
Closing in favor of #26 |
@roji Thanks for the testing code snippet. We will investigate more. |
https://github.com/dotnet/corefx/blob/master/src/System.Data.SqlClient/src/System/Data/SqlClient/SqlUtil.cs#L391
This is making cooperative cancellation difficult.
The text was updated successfully, but these errors were encountered: