Skip to content

Commit 192438b

Browse files
committed
Fix session leak. Fixes #469
If an exception was thrown, the 'session' local variable would be leaked, but the semaphore would be released, allowing another ServerSession to be created.
1 parent ee5e903 commit 192438b

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/MySqlConnector/Core/ConnectionPool.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ public async ValueTask<ServerSession> GetSessionAsync(MySqlConnection connection
4343
else
4444
m_sessionSemaphore.Wait(cancellationToken);
4545

46+
ServerSession session = null;
4647
try
4748
{
4849
// check for a waiting session
49-
ServerSession session = null;
5050
lock (m_sessions)
5151
{
5252
if (m_sessions.Count > 0)
@@ -121,8 +121,22 @@ public async ValueTask<ServerSession> GetSessionAsync(MySqlConnection connection
121121
Log.Debug("Pool{0} returning new Session{1} to caller; LeasedSessionsCount={2}", m_logArguments[0], session.Id, leasedSessionsCountNew);
122122
return session;
123123
}
124-
catch
124+
catch (Exception ex)
125125
{
126+
if (session != null)
127+
{
128+
try
129+
{
130+
Log.Debug(ex, "Pool{0} disposing created Session{1} due to exception: {2}", m_logArguments[0], session.Id, ex.Message);
131+
AdjustHostConnectionCount(session, -1);
132+
await session.DisposeAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false);
133+
}
134+
catch (Exception unexpectedException)
135+
{
136+
Log.Error(unexpectedException, "Pool{0} unexpected error in GetSessionAsync: {1}", m_logArguments[0], unexpectedException.Message);
137+
}
138+
}
139+
126140
m_sessionSemaphore.Release();
127141
throw;
128142
}

0 commit comments

Comments
 (0)