Skip to content
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

SqlCommand async Task may never complete because of unhandled exception #3067

Open
SergeyBokhantsev opened this issue Dec 9, 2024 · 1 comment
Assignees
Labels
🐛 Bug! Issues that are bugs in the drivers we maintain. ✔️ Triage Done Issues that are triaged by dev team and are in investigation.
Milestone

Comments

@SergeyBokhantsev
Copy link

Describe the bug

Async Task of SqlCommand sometimes could never go to Completed state (if exception occurs during command execution).
Thus, if you await the SqlCommand then your code may stuck forever at waiting the task completion.

Observed case:
There is a trivial code to interact with Azure SQL DB via EntityFramework. We noticed that sometimes the module that is working with DB freezes forever. Analyzing memory dumps we saw that it is always stuck awaiting EntityFramework operations like Select, Find, SaveChangesAsync etc.
In every case there is an unobserved exception logged also with the following details about the root exception:
Invalid operation. The connection is closed.
System.AggregateException: System.InvalidOperationException:
at Microsoft.Data.SqlClient.SqlConnection.GetOpenTdsConnection
at Microsoft.Data.SqlClient.SqlCommand+<>c__DisplayClass186_0.b__0
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop
at System.Threading.Tasks.Task.ExecuteWithThreadLocal

To reproduce

Unfortunately I don't have code to reproduce the issue because in our case the issue was caused by a sporadic network connection problem.
But taking into account the above exception call stack, the issue in source code can be seen:

I tested the described case by compiling custom SqlClient.dll where an artificial exceptions was thrown in lines 2407 and 2448, and can confirm it causes same behavior and Entity Framework operation stuck forever awaiting SqlCommand.

Expected behavior

Exceptions during SqlCommand execution must not prevent root Task from completion.

Further technical details

Microsoft.Data.SqlClient version: 5.2.0
EntityFramework 6.4.4
.Net 8

@cheenamalhotra cheenamalhotra added 🆕 Triage Needed For new issues, not triaged yet. 🐛 Bug! Issues that are bugs in the drivers we maintain. 🙌 Up-for-Grabs Issues that are ready to be picked up for anyone interested. Please self-assign and remove the label ✔️ Triage Done Issues that are triaged by dev team and are in investigation. and removed 🆕 Triage Needed For new issues, not triaged yet. labels Dec 9, 2024
@cheenamalhotra cheenamalhotra added this to the 7.0-preview1 milestone Dec 10, 2024
@paulmedynski paulmedynski self-assigned this Dec 11, 2024
@paulmedynski paulmedynski removed the 🙌 Up-for-Grabs Issues that are ready to be picked up for anyone interested. Please self-assign and remove the label label Dec 11, 2024
@cvdsoftware
Copy link

I experienced this problem myself a few times, with a locally hosted SQL Server, also caused by a network hiccup.

We have a service monitoring tool, if the service seems to be stuck we restart it.

Would be nice when this is fixed to backport the fix to older versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug! Issues that are bugs in the drivers we maintain. ✔️ Triage Done Issues that are triaged by dev team and are in investigation.
Projects
None yet
Development

No branches or pull requests

4 participants