Repository navigation
Description
I have problem with App that is hosted on official asp-dotnet-core-alipne container and SQL Server on other server. After restart server on wich we have MS SQL 2014, conainers gets high CPU and some internal thread hangs. Helps only restart of container. We investigated that there are some tcp connection in state CLOSE_WAIT when this situations occured. Some information about app and server:
Details about app:
- .net core 2.2 (C#)
- based on official alpine container (mcr.net.core.asp:2.2.5-alpine3.9)
- used Dapper as well as ADO.NET
- used Async method with async/await
- used Quartz.NET for job scheaduling
- used System.Data.SqlClient 4.6.1 download by Nuget
Details about Docker
- hosted on Centos 7
- used Docker 19.03
Details about SQL Server:
- MS SQL 2014 Standard x64
- Windows Server 2012R2
- hosted on Virtual Machine
Detalied problem description:
Container with app works 24/7 on linux server (Centos 7) on wich docker is installed. On same server there is virtual machine with Windows Server and MS SQL Server 2014 installed on it. App work fine if there some network issue and other stuff but after restart this server i get error:
[19-08-09 04:15:44.15 ERR -- SSI.Pojazd 0216NPIK] Job SSI.Pojazd.retry_Sms_RetryJob`1 threw an unhandled Exception:
System.Data.SqlClient.SqlException (0x80131904): SHUTDOWN is in progress.
Login failed for user 'XXXX'.
Cannot continue the execution because the session is in the kill state.
A severe error occurred on the current command. The results, if any, should be discarded.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso, String transactionName, Boolean shouldReconnect)
at System.Data.SqlClient.SqlConnection.BeginTransaction(IsolationLevel iso, String transactionName)
at Transport.Core.Abstractions.Database.TransportStoredProcedureWithResult`2.Execute(TInput input)
at Transport.Jobs.RetriesJobs.RetryJob`1.Execute(IJobExecutionContext context)
at Quartz.Core.JobRunShell.Run(CancellationToken cancellationToken)
ClientConnectionId:1d7f1d2d-f80b-42cf-906f-f0ee57b14f59
Error Number:6005,State:1,Class:14
After this error everything start's to behave wierd:
- CPU of container rise from 0.9 CPU to 100 CPU and more
- dotnet process consume CPU (on hosting linux processes ) rise from 1-2 to 12-14 cores (defends of how many apps are working)
- HealthCheck sometimes hangs -> no anwser from server when used simple CURL query
there are CLOSE_WAIT tcp connection to SQL
I think problem is created by this CLOSE_WAIT connection. But I don't have idea how to closed them.
TCP connection log:
tcp 0 0 173.25.0.2:44920 10.6.67.122:5672 ESTABLISHED
tcp 0 0 173.25.0.2:47488 10.12.128.12:1433 ESTABLISHED
tcp 0 0 173.25.0.2:47246 10.12.128.12:1433 ESTABLISHED
tcp 0 0 173.25.0.2:46785 10.6.67.122:5672 ESTABLISHED
tcp 0 0 173.25.0.2:45556 10.12.128.12:1433 CLOSE_WAIT
tcp 0 0 173.25.0.2:45520 10.12.128.12:1433 CLOSE_WAIT
###What i tried to do:
Use CancellationToken as some way of cancel DbOperation in SqlClient.dll
[Dapper code]
public async Task Execute(TInput input, IScope scope,CancellationToken token)
{
await scope.GetConnection().ExecuteAsync(new CommandDefinition(GetStoredProcedureName(), GetParameters(input), scope.GetTransaction(),
commandType: CommandType.StoredProcedure,cancellationToken:token)).ConfigureAwait(false);
}
[ADO.NET CODE]
public async Task CheckAsync(string connectionString, int timeout, CancellationToken cancellationToken)
{
try
{
SqlConnectionStringBuilder connectionStringBuilder =
new SqlConnectionStringBuilder(connectionString) {ConnectTimeout = 2};
using (var conn = new SqlConnection(connectionStringBuilder.ToString()))
{
try
{
if (cancellationToken.IsCancellationRequested)
cancellationToken.ThrowIfCancellationRequested();
await conn.OpenAsync(cancellationToken).ConfigureAwait(false);
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "Select 1";
cmd.CommandTimeout = timeout;
cancellationToken.ThrowIfCancellationRequested();
await cmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
}
}
catch (TaskCanceledException tce)
{
TransportLogger.Log.Debug("Task cancelled " + tce.Message + tce.StackTrace + " " + (tce.InnerException == null ? string.Empty : tce.InnerException.Message));
throw;
}
finally
{
if (conn.State == ConnectionState.Open) conn.Close();
}
}
}
catch (Exception ex)
{
TransportLogger.Log.Error(ex, "Cannot check db assebility");
throw;
}
}
Result -> i get information about cancellation but connection stay in Close_wait and CPU is rising to the roof.
Clear pool using ClearPool and ClearAllPool method
public class CheckDatabaseIsAccessible : ICheckDatabaseIsAccessible
{
public async Task CheckAsync(string connectionString, int timeout, CancellationToken cancellationToken)
{
try
{
SqlConnectionStringBuilder connectionStringBuilder =
new SqlConnectionStringBuilder(connectionString) {ConnectTimeout = 2};
using (var conn = new SqlConnection(connectionStringBuilder.ToString()))
{
try
{
if (cancellationToken.IsCancellationRequested)
cancellationToken.ThrowIfCancellationRequested();
await conn.OpenAsync(cancellationToken).ConfigureAwait(false);
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "Select 1";
cmd.CommandTimeout = timeout;
cancellationToken.ThrowIfCancellationRequested();
await cmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
}
}
catch (TaskCanceledException tce)
{
TransportLogger.Log.Debug("Task cancelled " + tce.Message + tce.StackTrace + " " + (tce.InnerException == null ? string.Empty : tce.InnerException.Message));
throw;
}
finally
{
if (conn.State == ConnectionState.Open) conn.Close();
}
}
}
catch (Exception ex)
{
TransportLogger.Log.Error(ex, "Cannot check db assebility");
throw;
}
}
public void Check(string connectionString, int timeout)
{
try
{
SqlConnectionStringBuilder connectionStringBuilder =
new SqlConnectionStringBuilder(connectionString) {ConnectTimeout = 2};
using (var conn = new SqlConnection(connectionStringBuilder.ToString()))
{
try
{
TransportLogger.Log.Debug("Open connection");
conn.Open();
TransportLogger.Log.Debug("connection open");
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "Select 1";
cmd.CommandTimeout = timeout;
TransportLogger.Log.Debug("Command executing...");
cmd.ExecuteNonQuery();
TransportLogger.Log.Debug("Command executed");
}
}
catch (TaskCanceledException tce)
{
TransportLogger.Log.Debug("Task cancelled " + tce.Message + tce.StackTrace + " " + (tce.InnerException == null ? string.Empty : tce.InnerException.Message));
throw;
}
finally
{
if (conn.State == ConnectionState.Open) conn.Close();
}
}
}
catch (Exception ex)
{
TransportLogger.Log.Error(ex, "Cannot check db assebility");
throw;
}
}
}
Pool is cleared but nothing happend, problem still exists:
[19-08-09 04:16:37.16 DBG -- SSI.Pojazd 0216NPIK] Task cancelled A task was canceled. at Transport.Core.Abstractions.Database.CheckDatabaseIsAccessible.CheckAsync(String connectionString, Int32 timeout, CancellationToken cancellationToken)
[19-08-09 04:16:37.16 INF -- SSI.Pojazd 0216NPIK] Try to clear pool
[19-08-09 04:16:37.16 INF -- SSI.Pojazd 0216NPIK] Pool cleared
I try to reproduce this on test env but i failed. We have aproximatly 400 servers and this situation is quite common (i think is about time of server is shutdown). I observe this situation occured only when
System.Data.SqlClient.SqlException (0x80131904): SHUTDOWN is in progress.
is thrown
Mayby someone had this problem and she/he know solution? Any help will be apprecieated.
Activity
cheenamalhotra commentedon Aug 14, 2019
Hi @szymo0
We're investigating the behavior, will update you soon!
szymo0 commentedon Oct 22, 2019
Hi there, is there any progress in this case?
yukiwongky commentedon Dec 3, 2019
@szymo0 Could you provide the netstat from the server as well?
yukiwongky commentedon Dec 5, 2019
@szymo0 I was doing some more searching and found that your issue looks very similar to this one, which makes me wonder if it's a .net core issue in System.Net.Sockets. Would you mind trying on .net core 2.1 and 3.0 to see if that can solve your issue?
JRahnama commentedon Aug 6, 2020
Hi @szymo0. any update on this issue? Were you able to reproduce the problem after updating to Microsoft.Data.SqlClient v2.0.0 and dotnet 3.1?
karinazhou commentedon Oct 8, 2020
Close this due to no activities on this.
Michriko commentedon Apr 29, 2021
Experiencing a similar issue with .NET Core 3.1 and EF Core.
Application runs inside a Container in a Kubernetes Cluster and connects via EF Core to a MS SQL Server outside the cluster.
Occasionally when there are backups/updates/restarts of the SQL Server, the application is unable to query anything from the SQL Server with the same exception and stacktrace as @szymo0 posted.
CPU usage goes up to 100%, all EF Core Queries fail with the following stacktrace until I restart the whole application:
.NET Core 3.1
EntityFrameworkCore 3.1.9
Docker Base image: mcr.microsoft.com/dotnet/core/aspnet:3.1
vasicvuk commentedon May 19, 2021
@Michriko We are facing the same issue in same situation.
@karinazhou Can you reopen the issue, since the problem still exists?
cheenamalhotra commentedon May 21, 2021
@Michriko @vasicvuk
Could you please try our latest released versions (v2.1.3 and v3.0.0-preview3) to validate this still occurs?
https://www.nuget.org/packages/Microsoft.Data.SqlClient
jain-vivek commentedon Jan 25, 2022
@cheenamalhotra can you confirm if this issue fixes by upgrading to Microsoft.Data.SqlClient? We are facing the same issue and we use an entity framework 3.1.13 which references Microsoft.Data.SqlClient 1.1.2) with .NET Core 3.1. I am not able to simulate in a lower environment to confirm it and would be helpful if someone @Michriko @vasicvuk has already confirmed this fix.