While cancelling a query SqlClient might throw an exception even though the query successfully executed
- Dominant language
- C#
- Stars
- 989
- Forks
- 340
- Avg merge
- 4d 19h
- Merged PRs (30d)
- 72
Description
### Describe the bug
While executing an 'update' query via `ExecuteNonQueryAsync` we've encountered an issue where SqlClient reports an error (Operation cancelled by user) but the actual query has completed successfully. I've managed to reproduce this error by cancelling the query via `CancellationTokenSource` just after the query is sent.
### To reproduce
https://github.com/vonzshik/SqlClientLostUpdateBug
### Expected behavior
Whenever there is an attempt to cancel a query, SqlClient should throw an exception only if the cancellation is successful.
### Further technical details
Microsoft.Data.SqlClient version: 5.0.1
.NET target: .NET 6
SQL Server version: SQL Server 2017
Operating system: Windows 10 21H2
**Additional context**
I believe the issue here is how SqlClient determines whenever it has to throw an "Operation cancelled by user" error.
https://github.com/dotnet/SqlClient/blob/03f305389139eff2dfc2c67ad3b564a9f1dd3325/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs#L2597-L2618
If I'm understanding this correctly, SqlClient sends an "Attention" request to cancel the current query and after it receives an "Attention" response it just adds an error to the list. The problem with this is that SqlClient doesn't even look whether the query was actually cancelled (which can happen if the cancellation request is sent before or after the query).
Contributor guide
Assessment
This issue has not been assessed yet.