Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expected state to be Connected but was Failed #433

Closed
alandoherty opened this issue Feb 6, 2018 · 4 comments
Closed

Expected state to be Connected but was Failed #433

alandoherty opened this issue Feb 6, 2018 · 4 comments
Assignees
Labels

Comments

@alandoherty
Copy link

alandoherty commented Feb 6, 2018

The MySqlConnection object always seem to return a State of open even when connection is lost. My code currently checks this before querying to attempt an auto-reconnect. This code worked fine with the older MySql.Data connector.

I'm sure I saw an issue which said this bug was fixed, but I'm still getting it in NuGet version 0.35.0.

Symptoms:

MySqlConnection.State is always Open even when connection is lost

How to trigger

Connect to MySQL then shutdown server while application is running, causing the connection to drop. Start up MySQL server again does not resolve issue as user code thinks state is still Open.

Stack Trace:

[mysql] System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host
   at MySqlConnector.Utilities.SocketAwaitable.GetResult() in C:\projects\mysqlconnector\src\MySqlConnector\Utilities\SocketAwaitable.cs:line 30
   at MySqlConnector.Protocol.Serialization.SocketByteHandler.<<WriteBytesAsync>g__DoWriteBytesAsync|7_0>d.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Protocol\Serialization\SocketByteHandler.cs:line 105
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   at MySqlConnector.Utilities.ValueTaskExtensions.<ContinueWith>d__0`2.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Utilities\ValueTaskExtensions.cs:line 8
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MySqlConnector.Core.ServerSession.TryAsyncContinuation(Task`1 task) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 999
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   at MySqlConnector.Core.TextCommandExecutor.<ExecuteReaderAsync>d__3.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Core\TextCommandExecutor.cs:line 72
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
 <omitted>

[api] GET /part -> OK

[mysql] System.InvalidOperationException: Expected state to be Connected but was Failed.
   at MySqlConnector.Core.ServerSession.VerifyState(State state) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 1035
   at MySqlConnector.Core.ServerSession.StartQuerying(MySqlCommand command) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 137
   at MySqlConnector.Core.TextCommandExecutor.<ExecuteReaderAsync>d__3.MoveNext() in C:\projects\mysqlconnector\src\MySqlConnector\Core\TextCommandExecutor.cs:line 69
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   <omitted>
@bgrainger
Copy link
Member

bgrainger commented Feb 8, 2018

Is your code something like the following?

using (var connection = new MySqlConnection())
{
	connection.Open();
	try
	{
		// use connection
	}
	catch (Exception ex)
	{
	}

	// check connection.State, looks good...
	// ... but try to use connection, get InvalidOperationException
}

@bgrainger
Copy link
Member

bgrainger commented Feb 16, 2018

Fixed in 0.36.0.

@kwaazaar
Copy link

kwaazaar commented Mar 14, 2018

I get this error with 0.36.1:

System.InvalidOperationException: Expected state to be Connected but was Failed.
at MySqlConnector.Core.ServerSession.VerifyState(State state)
at MySqlConnector.Core.ServerSession.d__57.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at MySqlConnector.Core.ConnectionPool.d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable1.ConfiguredValueTaskAwaiter.GetResult() at MySql.Data.MySqlClient.MySqlConnection.<CreateSessionAsync>d__83.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable1.ConfiguredValueTaskAwaiter.GetResult()
at MySql.Data.MySqlClient.MySqlConnection.d__25.MoveNext()

@bgrainger
Copy link
Member

bgrainger commented Mar 14, 2018

@kwaazaar Can you open a new issue with more details about the problem, including your calling code (not just a stack trace)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants