Skip to content

Commit

Permalink
Fix exception in DisposeAsync. Fixes #305
Browse files Browse the repository at this point in the history
If the connection is not in a known state (Connected or Failed), we'll just shut down the socket, which may be logged on the server as an aborted connection. This should prevent failure to shut down the session when an exception is thrown,  leaking connections to the server.
  • Loading branch information
bgrainger committed Aug 17, 2017
1 parent 2d3cbcc commit ef2a888
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/MySqlConnector/Serialization/MySqlSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,28 @@ public async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancella
if (m_payloadHandler != null)
{
// attempt to gracefully close the connection, ignoring any errors (it may have been closed already by the server, etc.)
State state;
lock (m_lock)
{
VerifyState(State.Connected, State.Failed);
m_state = State.Closing;
if (m_state == State.Connected || m_state == State.Failed)
m_state = State.Closing;
state = m_state;
}
try
{
m_payloadHandler.StartNewConversation();
await m_payloadHandler.WritePayloadAsync(QuitPayload.Create(), ioBehavior).ConfigureAwait(false);
await m_payloadHandler.ReadPayloadAsync(m_payloadCache, ProtocolErrorBehavior.Ignore, ioBehavior).ConfigureAwait(false);
}
catch (IOException)
{
}
catch (SocketException)

if (state == State.Closing)
{
try
{
m_payloadHandler.StartNewConversation();
await m_payloadHandler.WritePayloadAsync(QuitPayload.Create(), ioBehavior).ConfigureAwait(false);
await m_payloadHandler.ReadPayloadAsync(m_payloadCache, ProtocolErrorBehavior.Ignore, ioBehavior).ConfigureAwait(false);
}
catch (IOException)
{
}
catch (SocketException)
{
}
}
}
ShutdownSocket();
Expand All @@ -195,11 +201,7 @@ public async Task ConnectAsync(ConnectionSettings cs, IOBehavior ioBehavior, Can
throw new MySqlException("Unable to connect to any of the specified MySQL hosts.");
}

#if NETSTANDARD2_0
var byteHandler = new StreamByteHandler(m_networkStream);
#else
var byteHandler = new SocketByteHandler(m_socket);
#endif
m_payloadHandler = new StandardPayloadHandler(byteHandler);

var payload = await ReceiveAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
Expand Down

0 comments on commit ef2a888

Please sign in to comment.