System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.

Matthew 40 Reputation points
2023-10-26T01:56:06.2833333+00:00

I am getting this exception intermittently when trying to do a query on my MySql Database from inside my function apps. It happens approximately about once every 25 times the function app runs.

MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered during command execution.

[2023-10-26T00:19:19.495Z] ---> System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host..

[2023-10-26T00:19:19.495Z] ---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.

[2023-10-26T00:19:19.496Z] at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.CreateException(SocketError error, Boolean forAsyncThrow)

[2023-10-26T00:19:19.497Z] at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.SendAsyncForNetworkStream(Socket socket, CancellationToken cancellationToken)

[2023-10-26T00:19:19.497Z] at System.Net.Sockets.Socket.SendAsyncForNetworkStream(ReadOnlyMemory`1 buffer, SocketFlags socketFlags, CancellationToken cancellationToken)

[2023-10-26T00:19:19.498Z] at System.Net.Sockets.NetworkStream.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken)

[2023-10-26T00:19:19.499Z] at System.Net.Security.SslStream.WriteSingleChunkTIOAdapter

[2023-10-26T00:19:19.499Z] at System.Net.Security.SslStream.WriteAsyncInternalTIOAdapter

[2023-10-26T00:19:19.500Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.500Z] at System.Net.Security.SslStream.WriteAsyncInternalTIOAdapter

[2023-10-26T00:19:19.501Z] at System.Net.Security.SslStream.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken)

[2023-10-26T00:19:19.501Z] at System.Net.Security.SslStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)

[2023-10-26T00:19:19.502Z] at MySql.Data.MySqlClient.TimedStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, Boolean execAsync)

[2023-10-26T00:19:19.503Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.503Z] at MySql.Data.MySqlClient.TimedStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)

[2023-10-26T00:19:19.504Z] at MySql.Data.MySqlClient.MySqlStream.SendPacketAsync(MySqlPacket packet, Boolean execAsync)

[2023-10-26T00:19:19.504Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.505Z] at MySql.Data.MySqlClient.MySqlStream.SendPacketAsync(MySqlPacket packet, Boolean execAsync)

[2023-10-26T00:19:19.506Z] at MySql.Data.MySqlClient.NativeDriver.ExecutePacketAsync(MySqlPacket packetToExecute, Boolean execAsync)

[2023-10-26T00:19:19.506Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.507Z] at MySql.Data.MySqlClient.NativeDriver.ExecutePacketAsync(MySqlPacket packetToExecute, Boolean execAsync)

[2023-10-26T00:19:19.507Z] at MySql.Data.MySqlClient.NativeDriver.SendQueryAsync(MySqlPacket queryPacket, Boolean execAsync, Int32 paramsPosition)

[2023-10-26T00:19:19.508Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.508Z] at MySql.Data.MySqlClient.NativeDriver.SendQueryAsync(MySqlPacket queryPacket, Boolean execAsync, Int32 paramsPosition)

[2023-10-26T00:19:19.509Z] at MySql.Data.MySqlClient.Driver.SendQueryAsync(MySqlPacket p, Int32 paramsPosition, Boolean execAsync)

[2023-10-26T00:19:19.510Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.510Z] at MySql.Data.MySqlClient.Driver.SendQueryAsync(MySqlPacket p, Int32 paramsPosition, Boolean execAsync)

[2023-10-26T00:19:19.511Z] at MySql.Data.MySqlClient.Statement.ExecuteNextAsync(Boolean execAsync)

[2023-10-26T00:19:19.512Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.512Z] at MySql.Data.MySqlClient.Statement.ExecuteNextAsync(Boolean execAsync)

[2023-10-26T00:19:19.513Z] at MySql.Data.MySqlClient.PreparableStatement.ExecuteNextAsync(Boolean execAsync)

[2023-10-26T00:19:19.513Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.514Z] at MySql.Data.MySqlClient.PreparableStatement.ExecuteNextAsync(Boolean execAsync)

[2023-10-26T00:19:19.514Z] at MySql.Data.MySqlClient.Statement.ExecuteAsync(Boolean execAsync)

[2023-10-26T00:19:19.515Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.516Z] at MySql.Data.MySqlClient.Statement.ExecuteAsync(Boolean execAsync)

[2023-10-26T00:19:19.516Z] at MySql.Data.MySqlClient.PreparableStatement.ExecuteAsync(Boolean execAsync)

[2023-10-26T00:19:19.517Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.518Z] at MySql.Data.MySqlClient.PreparableStatement.ExecuteAsync(Boolean execAsync)

[2023-10-26T00:19:19.519Z] at MySql.Data.MySqlClient.MySqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean execAsync, CancellationToken cancellationToken)

[2023-10-26T00:19:19.519Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.520Z] at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)

[2023-10-26T00:19:19.520Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.521Z] at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)

[2023-10-26T00:19:19.522Z] at System.Data.Common.DbCommand.ExecuteReaderAsync(CancellationToken cancellationToken)

[2023-10-26T00:19:19.522Z] at Shared.Helper.MySQLHelper.SelectAsync(String query, String database, String connStr, Dictionary`2 data, ILogger log) in C:\Users\Start Beyond 2\source\repos\000sby-sb-funcapp-shared\sb-funcapp-shared\Helper\MySQLHelper.cs:line 272

[2023-10-26T00:19:19.523Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.524Z] at Shared.Helper.MySQLHelper.SelectAsync(String query, String database, String connStr, Dictionary`2 data, ILogger log)

[2023-10-26T00:19:19.524Z] at Oncio.FunctionsAPI.GraphDataHelper.GetGraphStatusAsync(GetGraphStatusInputData inputData, FunctionResultData result, ILogger log) in C:\Users\Start Beyond 2\source\repos\000sby-sb-funcapp-shared\sb-funcapp-shared\Oncio\GraphDataHelper.cs:line 34

[2023-10-26T00:19:19.525Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.526Z] at Oncio.FunctionsAPI.GraphDataHelper.GetGraphStatusAsync(GetGraphStatusInputData inputData, FunctionResultData result, ILogger log)

[2023-10-26T00:19:19.526Z] at Oncio.FunctionsAPI.Oncio_GetGraphStatus.Run(HttpRequest req, ILogger log) in C:\Users\Start Beyond 2\source\repos\000sby-sb-funcapp-03\SB-FuncApp-03\Oncio_GetGraphStatus.cs:line 31

[2023-10-26T00:19:19.527Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.StartTStateMachine

[2023-10-26T00:19:19.527Z] at Oncio.FunctionsAPI.Oncio_GetGraphStatus.Run(HttpRequest req, ILogger log)

[2023-10-26T00:19:19.528Z] at lambda_method118(Closure , Oncio_GetGraphStatus , Object[] )

[2023-10-26T00:19:19.529Z] at Microsoft.Azure.WebJobs.Host.Executors.TaskMethodInvoker`2.InvokeAsync(TReflected instance, Object[] arguments) in D:\a_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\TaskMethodInvoker.cs:line 21

[2023-10-26T00:19:19.529Z] at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments) in D:\a_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 52

[2023-10-26T00:19:19.530Z] at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

[2023-10-26T00:19:19.530Z] at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1.AsyncStateMachineBox1.MoveNext(Thread threadPoolThread)

[2023-10-26T00:19:19.531Z] at System.Runtime.CompilerServices.YieldAwaitable.YieldAwaiter.<>c.<OutputCorrelationEtwEvent>b__6_0(Action innerContinuation, Task continuationIdTask)

[2023-10-26T00:19:19.531Z] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()

[2023-10-26T00:19:19.532Z] at System.Runtime.CompilerServices.YieldAwaitable.YieldAwaiter.RunAction(Object state)

[2023-10-26T00:19:19.533Z] at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()

[2023-10-26T00:19:19.533Z] at System.Threading.ThreadPoolWorkQueue.Dispatch()

[2023-10-26T00:19:19.534Z] at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

[2023-10-26T00:19:19.534Z] at System.Threading.Thread.StartCallback()

[2023-10-26T00:19:19.535Z] --- End of stack trace from previous location ---

[2023-10-26T00:19:19.536Z]

[2023-10-26T00:19:19.536Z] --- End of inner exception stack trace ---

[2023-10-26T00:19:19.537Z] at System.Net.Security.SslStream.<WriteSingleChunk>g__CompleteWriteAsync|182_1TIOAdapter

[2023-10-26T00:19:19.537Z] at System.Net.Security.SslStream.WriteAsyncInternalTIOAdapter

[2023-10-26T00:19:19.538Z] at MySql.Data.MySqlClient.TimedStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, Boolean execAsync)

[2023-10-26T00:19:19.539Z] at MySql.Data.MySqlClient.MySqlStream.SendPacketAsync(MySqlPacket packet, Boolean execAsync)

[2023-10-26T00:19:19.539Z] at MySql.Data.MySqlClient.NativeDriver.ExecutePacketAsync(MySqlPacket packetToExecute, Boolean execAsync)

[2023-10-26T00:19:19.540Z] at MySql.Data.MySqlClient.NativeDriver.SendQueryAsync(MySqlPacket queryPacket, Boolean execAsync, Int32 paramsPosition)

[2023-10-26T00:19:19.540Z] at MySql.Data.MySqlClient.Driver.SendQueryAsync(MySqlPacket p, Int32 paramsPosition, Boolean execAsync)

[2023-10-26T00:19:19.541Z] at MySql.Data.MySqlClient.Statement.ExecuteNextAsync(Boolean execAsync)

[2023-10-26T00:19:19.541Z] at MySql.Data.MySqlClient.PreparableStatement.ExecuteNextAsync(Boolean execAsync)

[2023-10-26T00:19:19.542Z] at MySql.Data.MySqlClient.Statement.ExecuteAsync(Boolean execAsync)

[2023-10-26T00:19:19.543Z] at MySql.Data.MySqlClient.PreparableStatement.ExecuteAsync(Boolean execAsync)

[2023-10-26T00:19:19.543Z] at MySql.Data.MySqlClient.MySqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean execAsync, CancellationToken cancellationToken)

[2023-10-26T00:19:19.544Z] at MySql.Data.MySqlClient.MySqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean execAsync, CancellationToken cancellationToken)

[2023-10-26T00:19:19.544Z] at MySql.Data.MySqlClient.MySqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean execAsync, CancellationToken cancellationToken)

[2023-10-26T00:19:19.545Z] at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)

[2023-10-26T00:19:19.546Z] at Shared.Helper.MySQLHelper.SelectAsync(String query, String database, String connStr, Dictionary`2 data, ILogger log) in C:\Users\Start Beyond 2\source\repos\000sby-sb-funcapp-shared\sb-funcapp-shared\Helper\MySQLHelper.cs:line 272

[2023-10-26T00:19:19.547Z] at Oncio.FunctionsAPI.GraphDataHelper.GetGraphStatusAsync(GetGraphStatusInputData inputData, FunctionResultData result, ILogger log) in C:\Users\Start Beyond 2\source\repos\000sby-sb-funcapp-shared\sb-funcapp-shared\Oncio\GraphDataHelper.cs:line 34

[2023-10-26T00:19:19.547Z] at Oncio.FunctionsAPI.Oncio_GetGraphStatus.Run(HttpRequest req, ILogger log) in C:\Users\Start Beyond 2\source\repos\000sby-sb-funcapp-03\SB-FuncApp-03\Oncio_GetGraphStatus.cs:line 31

after reading this https://learn.microsoft.com/en-us/troubleshoot/sql/database-engine/connect/tls-exist-connection-closed

I suspected it may be related to TLS settings. I checked my database and it is set to require TLS 1.2 minimum. I had a look on my function app and could not find where to set min TLS, I did find that if i went to overview > json format, there was a record in there under "siteConfig" which was set as

     "minTlsVersion": null,

Does anyone know how i can fix the above or how i can set the min TLS version on my function apps to be 1.2 so it matches the MySql Database?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
3,698 questions
Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
620 questions
{count} votes

Accepted answer
  1. navba-MSFT 9,405 Reputation points Microsoft Employee
    2023-10-27T06:16:43.5466667+00:00

    @Matthew Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    I see that you are experiencing an intermittent issue with their function app when querying their MySQL database. The error message indicates that the connection was forcibly closed by the remote host.

    Please open the Azure portal and navigate to your function app and ensure that you have the below setting to set TLS 1.2:

    User's image

    If you have this set already then within your code you need to add the below line which ensures that the ssl request would be sent to MySQl using TLS 1.2

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    

    See the sample code, to get an idea:

    using System.Net;
    
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            // THIS LINE SHOULD BE ADDED
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    
            // Your other code to perform MySQL operation goes here...
    
            return new OkResult();
        }
    }
    

    In this example, ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; is called at the start of your Azure Function. This ensures that any network requests made by your function will use TLS 1.2.

    Also Please check the Function App detectors by navigating to the Diagnose and Solve Problems section in the Azure Portal for the Functions as shown below: You can rely on the Function App Down or Reporting Errors setting:

    User's image

    Hope this helps.

    **

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful