SqlClient: A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 35 - An internal exception was caught)
Describe the bug
We are using Entity Framework 7 with the Microsoft.Data.SqlClient package version 5.1.1. The issue we are facing is the following stack trace appears in groups in our logs every few hours from all our services that use a common routine for loading a database connection. From the logs these issues are typically cleared up within 30seconds, but during that time there is significant errors (10-15).
These errors are all happening from different services (hosted in a AKS cluster) within the same time framework.
I will add that all the services connect to the Azure SQL Database using Private endpoints, and will stop happening after 15-30 seconds. The interval between the errors is random, anywhere from 30minutes to 4 hours so I am at a complete loss where to look.
Microsoft.Data.SqlClient.SqlException (0x80131904): A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 35 - An internal exception was caught)
---> System.IO.IOException: Unable to read data from the transport connection: Connection reset by peer.
---> System.Net.Sockets.SocketException (104): Connection reset by peer
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.Stream.Read(Span`1 buffer)
at System.Net.Sockets.NetworkStream.Read(Span`1 buffer)
at Microsoft.Data.SqlClient.SNI.SslOverTdsStream.Read(Span`1 buffer)
at System.Net.Security.SslStream.EnsureFullTlsFrameAsync[TIOAdapter](CancellationToken cancellationToken)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder`1.StateMachineBox`1.System.Threading.Tasks.Sources.IValueTaskSource<TResult>.GetResult(Int16 token)
at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](Memory`1 buffer, CancellationToken cancellationToken)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder`1.StateMachineBox`1.System.Threading.Tasks.Sources.IValueTaskSource<TResult>.GetResult(Int16 token)
at System.Net.Security.SslStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at Microsoft.Data.SqlClient.SNI.SNITCPHandle.Receive(SNIPacket& packet, Int32 timeoutInMilliseconds)
at ....
ClientConnectionId:4d28a01d-2de1-4540-b1ca-9e9ef9cd9501
Further technical details
Microsoft.Data.SqlClient version: 5.1.1 .NET target: dotnet core 7 SQL Server version: Azure Sql Operating system: Debian Linux - Docker
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 11
- Comments: 27 (7 by maintainers)
It’s really hard to find the place where
SocketExceptionshould turn intoSqlError.Number.In the meantime we applied this workaround:
And registration:
We also have a massive number of socket exceptions using Azure SQL from .Net clients that connect over private endpoints.
I think there are some well-known causes for these socket exceptions. The challenge has been to try to get Microsoft to provide support. These problems occur in their data center, using their SQL client, and their Azure SQL database. You would think that CSS would want to look at a repro for this problem. But I have opened numerous cases with several of the teams that have experience with private endpoints (eg. Power BI team, ADF team, Synapse team, and Private Endpoint team itself). But nobody wants to share my repro! It is very frustrating. The private endpoint team at CSS appeared to be just a VPN and DNS troubleshooting team, so I gave up on trying to get them to help.
My next plan is to open an AKS support case. Has anyone tried to give that team a repro for this yet? I know it is a lot of work, but I don’t think we can fix private endpoints without forcing Mindtree/Microsoft to review a full repro.
I can share what I have learned, if it helps… Sorry in advance for the wall of text.
The most drastic reduction in our socket exceptions happened when we increased the threads in the threadpool, specifically the min threads, which are only (8,8) by default on Linux (at least on .Net core 3.1). See issue #647 and the related comment https://github.com/dotnet/SqlClient/issues/647#issuecomment-1816458510
ThreadPool.SetMinThreads(1024, 512);That fixes the majority of our socket exceptions. Note that this is a fix which is unrelated to the private endpoints. It just resolves the problems with threadpool starvation on Linux (prior to introducing the fix, you will notice that your callstack waits for async responses from members like “System.Net.Security.SslStream.ReadAsyncInternal”)
The remaining socket exceptions are almost certainly due to bugs in the so-called Private Endpoints (aka Managed Private Endpoints). These bugs are well-known by the responsible Microsoft team, but not so much by anyone else at Microsoft (even a PG like the ADF team that use private endpoints extensively doesn’t seem to be aware, despite the fact that their product suffers from a massive number of what they call “transient communication failures”. We have also had recurring monthly outages that last ~30 mins and refer to “transient failures” as the underlying cause. In one recent incident, they even made up a story that blamed Cosmos DB for a database crash that lasted thirty minutes. In reality, I think they had to change their outage notification a little bit since the normal notification was getting a little repetitive…)
Thankfully there was one team that shared some of the details about the underlying private endpoint bugs. It was only because and I have had a support case open with them for over two years. The Power BI team gave me a tiny bit of information, but they won’t go so far as to say that the bugs the found are the same ones that impact the other platforms which use private endpoints. It is pretty clear that these network bugs have delayed the GA … You will see that they announced their component (azure managed vnet gateway) about three years ago and it still hasn’t gone GA - I’m assuming that is primarily because of these unresolved networking bugs (you can find the public preview announcement here: https://powerbi.microsoft.com/en-us/blog/announcing-vnet-connectivity-for-pbi-datasets-public-preview/ )
As per information from the Power BI team, here are some changes they expect to receive to improve the “Azure Network stack”:
Feature ending with x509 : Support map space deletion in VFP without breaking flows. (virtual filtering platform, ie switch?) Estimated ETA - late 2024
Bug ending with x810 : PE-NC Flow mix up Estimated ETA - Network team promised to roll it out early 2024.
I also want to point out that these issues in private endpoints are NOT specific to SqlClient, from what I can tell. They seem to affect other types of TCP traffic - particularly long-running operations that hold a connection open for an extended period of time (longer a second).
I wish Microsoft would share more openly and transparently about these networking bugs. Instead what they typically do is to brush off the problem as a “transient” issue, even though there are underlying bugs that are responsible. They will proceed to ask customers to implement cumbersome workarounds (numerous retries with two minute delays in between them). This seems excessive considering the fact that the TCP protocol is intended to be fault-tolerant in the first place, and also because my connections are between resources that are direct proximity to each other within the same datacenter!
One of the reasons it is hard to hold Microsoft accountable is because these private endpoint bugs are related to virtual network (software-defined networking components). It is not related to the physical infrastructure. And only a small subset of customers is typically impacted at any time. The impacted customers typically have to be running workloads on the same physical and possibly even the same virtual machine (if containers are involved). Interestingly the Power BI team was never able to recreate the PE bugs if they were running my repro in their “Canary” region. Eventually they had to run the repro on the East US region in order to see the buggy behavior.
I have kept my Power BI support ticket open for now (going on three years). I think I will try to avoid closing it until they have a permanent fix for the private endpoints. They will finally go GA in February, despite the presence of these bugs. Their plan is to mask over the problem with implicit retries (it is not something I look forward to, considering the long-running queries that pull large amounts of data into Power BI).
I hope this is helpful, if long-winded. I will remember to circle back if/when the AKS support team is willing to take a support case and a repro for these “private endpoint” bugs. I think it is pretty clear that these problems don’t happen for me on (1) peered VNETs or on (2) public service endpoints. It is just the funky private endpoints that seem to be buggy. It would be helpful if someone could confirm whether these observations are consistent with their own.
In talking with the ODBC driver team, for their Linux implementation, they actually have translations from Linux into the equivalent Windows error codes to try and maintain prior Windows-only behavior under error conditions. So it sounds like this is an enhancement we need to make in our managed SNI layer that’s used by non-Windows clients. I bet this would be helped by implementing #649…
Looks related to #1773 and #1902
guys if your connections are on AKS and connecting to a “public” azure SQL using the default AKS outbound load balancer
try changing the default outbound nat rule
aksOutboundRuleidle timeout from the stock 30 minutes down to 4 minutesYes your idle connections won’t last as long, but this helped me reduce me weird connection errors to zero No errors now for the past 3 days
p.s. if you manually change it and do ANY KIND OF UPDATE to the aks cluster, it will look at the current settings and “correct” them back to 30min, so if you want to test this make sure you modify the setting in AKS itself or don’t touch anything in AKS! so use
--nat-gateway-idle-timeoutDO NOT USE the service label! thats for INbound connections only NOT OUTboundWe have been struggling with this issue, or very similar, for the past months and I wanted to provide some recent details for the benefit of finding the root cause and a potential fix:
We’re seeing intermittent SQL Exception / SocketException when connecting from a WEU AKS pod towards an Azure SQL database running in NEU. We connect to the SQL using Private Endpoint.
The exceptions vary between “Connection reset by peer” and “Connection timed out”, but common to both is that there is no sign of the query ever reaching the database.
They seem to occur in bursts for a period of time and then disappear for anything from 1 - 6 hours.
We see the issue happening much more frequently when connection across regions but the exceptions also happen from AKS in NEU connecting to Azure SQL in NEU.
Example stack trace:
[{"severityLevel":"Error","outerId":"0","message":"A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 35 - An internal exception was caught)","type":"Microsoft.Data.SqlClient.SqlException","id":"60822972","parsedStack":[{"assembly":"Microsoft.Data.SqlClient, Version=6.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5","method":"Microsoft.Data.SqlClient.SqlCommand.EndExecuteReaderAsync","level":0,"line":0},{"assembly":"System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic",“level”:1,“line”:0},{“assembly”:“System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e”,“method”:“System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw”,“level”:2,“line”:0},{“assembly”:“System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e”,“method”:“System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess”,“level”:3,“line”:0},{“assembly”:“System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e”,“method”:“System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification”,“level”:4,“line”:0},{“assembly”:“HealthChecks.SqlServer, Version=8.0.2.0, Culture=neutral, PublicKeyToken=2783471aaf72b3d9”,“method”:“HealthChecks.SqlServer.SqlServerHealthCheck+<CheckHealthAsync>d__2.MoveNext”,“level”:5,“line”:32,“fileName”:“/_/src/HealthChecks.SqlServer/SqlServerHealthCheck.cs”}]}`Details Microsoft.Data.SqlClient version: 6.0.0 .NET target: .NET 8 and 9 SQL Server version: Azure Sql Image: Azure Linux 3 image
Here is an update about the PE bugs…
The bug ending with x810 (PE-NC Flow mix up) was fixed in early February. As I understand, it was a general-purpose PE bug that impacted normal scenarios where a client on a VM is connecting to Azure SQL.
Despite the fix, I still see lots of socket exceptions in PE’s, so I’m guessing they are related to the other bug: (ending with x509 : Support map space deletion in VFP without breaking flows)
… although I’m not certain. The way this bug ( x509) is documented seems obscure and isn’t likely to apply to all my scenarios. This one supposedly applies to only the scenarios where there are multiple containers on a VM. … So I’m still trying to get a team at Microsoft to take a repro for my Spark problems but haven’t found one that is eager to do so yet. I’m guessing that there are multiple bugs; but nobody seems to be willing to dive into them. I’ve learned that it is pretty impossible to get bugs fixed when working with Microsoft’s “professional” support. The back-end PG do not care very much about these customers, will handle their cases in sort of a “best effort” way, and sometimes that means no effort.
@ErikEJ You are right. And both of those issues are closed. So, the logical question comes: how do we detect transient errors in an OS-independent manner? Is it even possible with the current codebase?