Closed
Description
When I upgraded my project from .net core 2.0 to 2.1 I (sometimes) get this
System.NotSupportedException:
System.NotSupportedException: The WriteAsync method cannot be called when another write operation is pending.
at System.Net.Security.SslStreamInternal.WriteAsyncInternal[TWriteAdapter](TWriteAdapter writeAdapter, ReadOnlyMemory`1 buffer)
at System.Net.Security.SslStreamInternal.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Security.SslStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at MySqlConnector.Protocol.Serialization.StreamByteHandler.WriteBytesAsync(ArraySegment`1 data, IOBehavior ioBehavior) in C:\projects\mysqlconnector\src\MySqlConnector\Protocol\Serialization\StreamByteHandler.cs:line 87
--- End of stack trace from previous location where exception was thrown ---
at MySqlConnector.Utilities.ValueTaskExtensions.ContinueWith[T,TResult](ValueTask`1 valueTask, Func`2 continuation) in C:\projects\mysqlconnector\src\MySqlConnector\Utilities\ValueTaskExtensions.cs:line 8
at MySqlConnector.Core.ServerSession.DisposeAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 200
at MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ConnectionPool.cs:line 89
at MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ConnectionPool.cs:line 141
at MySql.Data.MySqlClient.MySqlConnection.CreateSessionAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 399
at MySql.Data.MySqlClient.MySqlConnection.OpenAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 170
at MySql.Data.MySqlClient.MySqlConnection.Open() in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlConnection.cs:line 156
at BackOffice.Controllers.ApiController.GetCustomerList(Nullable`1 id) in C:\Dev\New Line Mobile\NewLineBilling\BackOffice\Controllers\ApiController.cs:line 43
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)
Metadata
Metadata
Assignees
Labels
No labels
Activity
bgrainger commentedon Jun 6, 2018
What version of MySqlConnector are you using?
cschotte commentedon Jun 6, 2018
v0.40.4 the exception is happening (sometimes) in the Connection.Open(); (i'm using SSL)
MySQL Server version 5.7.21 (Azure)
.NET Core 2.1.0
bgrainger commentedon Jun 6, 2018
I wonder if
SslStreamin an earlier .NET Core version was throwing a different exception and it just happened to be caught here: https://github.com/mysql-net/MySqlConnector/blob/0.40.4/src/MySqlConnector/Core/ServerSession.cs#L196-L210It seems like a previous
Writeoperation must have failed somehow, which is why the session is being cleaned up (after being retrieved from the pool), but I don't yet know exactly how this error is being triggered.bgrainger commentedon Jun 7, 2018
Are you able to collect logs (with
ConsoleLoggerProvideror one of these options) of the failure and post them here?cschotte commentedon Jun 8, 2018
cschotte commentedon Jun 8, 2018
The MySQL Server version is 5.7.21 (Azure), only the Logger is reporting ServerVersion=5.6.39.0 (if this is a MySQL version number)
bgrainger commentedon Jun 8, 2018
This is a known issue with Azure Database for MySQL (due to the proxy that's used): https://social.msdn.microsoft.com/Forums/security/en-US/795cb2be-dce7-4805-b324-a67e83697580/azure-database-for-mysql-reports-incorrect-version?forum=AzureDatabaseforMySQL
bgrainger commentedon Jun 8, 2018
Just to confirm: the
WriteAsyncexception did happen during the period of time that log was collected? (Unfortunately, I don't think I see anything relevant yet.)bgrainger commentedon Jul 6, 2018
Just got this same error when targeting Azure:
Since the exception is in
GetSessionAsync, it may be related to a previous unit test.This occurred in the
netcoreapp2.0tests, so it may not strictly be related tonetcoreapp2.1.cschotte commentedon Jul 6, 2018
Next week I try to create a small test project, so we can easier trigger this event. Now I it looks random.
cschotte commentedon Jul 10, 2018
https://stackoverflow.com/questions/12611861/sslstream-writeasync-the-beginwrite-method-cannot-be-called-when-another-write
cschotte commentedon Jul 10, 2018
The following solution is (for now) working for me, needs more testing.
bgrainger commentedon Jul 10, 2018
Were you previously calling
OpenAsync()withoutawaiting it? What was your previous code?cschotte commentedon Jul 10, 2018
First my code was
db.Open();
hkurma commentedon Aug 5, 2018
I'm facing the same issue as discussed above. My project is using .Net Core 2.1 and Azure MySql Database. I'm using Entity Framework for database communications and I added Pomelo.EntityFrameworkCore.MySql nuGet package as mentioned in this https://docs.microsoft.com/en-us/ef/core/providers/
This issue is random as @cschotte said. But it is happening often for me. Any suggestions on how to solve it ? Below is stack trace of the Exception.
cschotte commentedon Aug 5, 2018
Do not use the db.Open() in your code, in my project I fixed it by using db.OpenAsync().GetAwaiter().GetResult().
Somehow in .net 2.1 and azure the normal db.Open is calling a async function international and is not waiting for it
hkurma commentedon Aug 5, 2018
I don't have db.OpenAsync() at all in my code since I'm using Entity Framework. How do I solve this ?
hkurma commentedon Aug 21, 2018
Can I expect a fix or solution for this issue? I'm using .NET Core 2.1, Entity framework and Azure MySQL.
bgrainger commentedon Aug 21, 2018
I don't yet have enough information to reliably reproduce, diagnose, and fix the problem. Any more specific information you can provide would be helpful. Are you able to create a small example that reproduces the problem (e.g., when run in a loop)?
bgrainger commentedon Aug 21, 2018
This same crash has been filed with
SqlConnection: dotnet/corefx#27006bgrainger commentedon Aug 21, 2018
Looks like this exception is always throw from
ServerSession.DisposeAsync. We were previously ignoringIOException#159 andObjectDisposedException#411 in that method; I think the most straightforward workaround is to assume we may be in an inconsistent state and also ignoreNotSupportedException.Ignore NotSupportedException. Fixes #509
Ignore NotSupportedException. Fixes #509
bgrainger commentedon Aug 21, 2018
Added a workaround in 0.44.0.