SqlClient: Long running connections to Azure SQL randomly fail after some amount of time
Describe the bug
My guess is that SQL Client is pooling connections to aggressively and not properly detecting connection issues. And or reconnecting to the gateway to be handed a new worker.
I’m assuming workers get restarted\shifted to different machines, and the client is holding on to firmly to the old worker instance.
System.ComponentModel.Win32Exception
Exception message: Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Stack trace: Microsoft.Data.SqlClient.SqlException:
at Microsoft.Data.SqlClient.SqlCommand.EndExecuteReaderAsync (Microsoft.Data.SqlClient, Version=6.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic (System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at HealthChecks.SqlServer.SqlServerHealthCheck+<CheckHealthAsync>d__2.MoveNext (HealthChecks.SqlServer, Version=9.0.0.0, Culture=neutral, PublicKeyToken=2783471aaf72b3d9: /home/runner/work/AspNetCore.Diagnostics.HealthChecks/AspNetCore.Diagnostics.HealthChecks/src/HealthChecks.SqlServer/SqlServerHealthCheck.cs:32)
Inner exception System.ComponentModel.Win32Exception handled at Microsoft.Data.SqlClient.SqlCommand.EndExecuteReaderAsync:
Microsoft.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
---> System.ComponentModel.Win32Exception (258): No error information
at Microsoft.Data.SqlClient.SqlCommand.EndExecuteReaderAsync(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
ClientConnectionId:6bb62fc0-60bc-4668-b601-3fb24463b0dc
Error Number:-2,State:0,Class:11
ClientConnectionId before routing:11b3f3ff-05e0-4a26-919d-341135a5e7b5
Routing Destination:dxxxxxxxx3.trxxxxx.westeurope1-a.worker.database.windows.net,11057
To reproduce
No repo this is a pool of databases under various active loads (never “to high”)
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks()
.AddSqlServer(Configuration["Data:ConnectionStrings:Sql"])
Expected behavior
Properly handled reconnects to the worker \ go back to the gateway to get a new worker assigned
Further technical details
Microsoft.Data.SqlClient version: 6.0.2 (cannot upgrade because of #3336 .NET target: .NET 9.0.4 SQL Server version: Azure SQL Database (Microsoft.Sql/servers/databases) Operating system: AKS container mcr.microsoft.com/dotnet/runtime:9.0.4-alpine3.21
Additional context Azure SQL Maintenace script has been run for all databases. i have a support ticket open with Azure SQL and they cannot find anything wrong on their end.
Authentication is with a SQL user & password so no weird tokens Connection Policy is set to Redirect SQL Elastic Pool Business Critical (Gen5) 4 vCores Average load is around 20%~30%
Our code is running on an AKS cluster
With EF CORE AddDbContextPool and .EnableRetryOnFailure(3)
I checked for SNAT exhaustion errors (i could only find one single connection for the past month)
It fails for SELECT 1; health checks but also for SELECT TOP(1) [x] FROM [y] WHERE [Id]=z and for even more complicated queries.
We have 6.56 million logged samples (rate is 25%) for the past 30 days of SQL queries in app insights all working properly. Except for the couple that have that weird connectivity issue
our connection timeout is bumped up from 30 to 60 as per azure sql support
Server=tcp:xxxxx.database.windows.net,1433;Database=yyyyy;User ID=zzzzz;Password=yyyyy;MultipleActiveResultSets=False;Encrypt=Strict;Connection Timeout=60;
p.s. sorry for being yet another one of those complainers\users that don’t contribute anything 😉😘 p.s.s. love the work wraith is doing for varbinary(max) and sorry for long post🥔
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 15 (6 by maintainers)
@mdaigle not really because there’s raw packet captures in there
Hi @KLuuKer - I’m handing this issue off to my colleague @mdaigle . He will be in touch within the next few days.