-
Notifications
You must be signed in to change notification settings - Fork 300
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
InvalidOperationException when writing a lot of packets asynchronously and using managed SNI implementation #786
Comments
Hey @cheenamalhotra The fix works but as it is mentioned in the PR by @roji it might lead to thread pool starvation with that Is there any way to get the fix for it into 2.1 version? Looking at work arrounds in other issues:
Is there any other work arround that could help us? I just feel like the asynchronous API is too much unreliable in such cases beacuse of it but from the other hand using sync API does not scale :( |
@cheenamalhotra Works great in 2.1. Thanks for your help! |
Describe the bug
Hey guys.
We are executing stored procedure that sometimes is taking as parameter a data table with a little bit of data (in the prepared repro case it is arround 0.5mb). When we try to do that we sometimes get following exception:
When I digged into the code I saw that connection is being closed here:
SqlClient/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs
Line 1802 in d14c0b1
And the exception received in that method is:
That led me to following methods:
SqlClient/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs
Line 707 in d14c0b1
SqlClient/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPacket.cs
Line 304 in d14c0b1
So
SendAsync
returns justuint
and inside that method we have following call ofWriteToStreamAsync
which is aasync void
:SqlClient/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs
Line 712 in d14c0b1
Since it's a
async void
it means it's fire and forget and there might be another call ofWriteToStreamAsync
before previous one completes which lead to writing into the same stream by multiple threads concurrently at the same time that is causing theNotSupportedException
(there is also a lock inSendAsync
which I don't know what is a purpose of it but it does not defend against that case).If I use sync version of
ExecuteReader
or non-managed SNI on Windows it works fine. I also tried to play withpacketSize
property in connection string to increase/decrease but it caused the exception anyway.To reproduce
Here is a solution project with reproduction.
Expected behavior
Execute without error like in non-managed SNI implementation or sync version.
Further technical details
Microsoft.Data.SqlClient version: 1.1.3/2.0.1
.NET target: Net Core 3.1/.net5
SQL Server version: Azure Sql Server
Operating system: Windows(with managed SNI)/ Alpine docker container
The text was updated successfully, but these errors were encountered: