Skip to content

Commit c340a79

Browse files
committed
Return session to pool even if connection is closed. Fixes #836
The ConnectionState can be set to Closed when a failure occurs on the connection, but the session still needs to be returned to the pool to avoid a leak.
1 parent ff5d594 commit c340a79

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

src/MySqlConnector/MySql.Data.MySqlClient/MySqlConnection.cs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -796,9 +796,6 @@ private void VerifyNotDisposed()
796796

797797
private Task CloseAsync(bool changeState, IOBehavior ioBehavior)
798798
{
799-
if (m_connectionState == ConnectionState.Closed)
800-
return Utility.CompletedTask;
801-
802799
// check fast path
803800
if (m_activeReader is null &&
804801
CurrentTransaction is null &&
@@ -866,31 +863,28 @@ private async Task DoCloseAsync(bool changeState, IOBehavior ioBehavior)
866863
}
867864
#endif
868865

869-
if (m_connectionState != ConnectionState.Closed)
866+
try
870867
{
871-
try
872-
{
873-
await CloseDatabaseAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false);
874-
}
875-
finally
868+
await CloseDatabaseAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false);
869+
}
870+
finally
871+
{
872+
if (m_session is object)
876873
{
877-
if (m_session is object)
874+
if (GetInitializedConnectionSettings().Pooling)
878875
{
879-
if (GetInitializedConnectionSettings().Pooling)
880-
{
881-
m_session.ReturnToPool();
882-
}
883-
else
884-
{
885-
await m_session.DisposeAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false);
886-
m_session.OwningConnection = null;
887-
}
888-
m_session = null;
876+
m_session.ReturnToPool();
889877
}
890-
891-
if (changeState)
892-
SetState(ConnectionState.Closed);
878+
else
879+
{
880+
await m_session.DisposeAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false);
881+
m_session.OwningConnection = null;
882+
}
883+
m_session = null;
893884
}
885+
886+
if (changeState)
887+
SetState(ConnectionState.Closed);
894888
}
895889
}
896890

0 commit comments

Comments
 (0)