Skip to content

Commit f723610

Browse files
committed
Dispose ServerSession on failure. Fixes #1248
1 parent cc241f0 commit f723610

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/MySqlConnector/Core/ConnectionPool.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,18 @@ private async ValueTask<ServerSession> ConnectSessionAsync(MySqlConnection conne
383383
var session = new ServerSession(this, m_generation, Interlocked.Increment(ref m_lastSessionId));
384384
if (Log.IsDebugEnabled())
385385
Log.Debug(logMessage, m_logArguments[0], session.Id);
386-
var statusInfo = await session.ConnectAsync(ConnectionSettings, connection, startTickCount, m_loadBalancer, activity, ioBehavior, cancellationToken).ConfigureAwait(false);
387-
Exception? redirectionException = null;
386+
string? statusInfo;
387+
try
388+
{
389+
statusInfo = await session.ConnectAsync(ConnectionSettings, connection, startTickCount, m_loadBalancer, activity, ioBehavior, cancellationToken).ConfigureAwait(false);
390+
}
391+
catch (Exception)
392+
{
393+
await session.DisposeAsync(ioBehavior, default).ConfigureAwait(false);
394+
throw;
395+
}
388396

397+
Exception? redirectionException = null;
389398
if (statusInfo is not null && statusInfo.StartsWith("Location: mysql://", StringComparison.Ordinal))
390399
{
391400
// server redirection string has the format "Location: mysql://{host}:{port}/user={userId}[&ttl={ttl}]"

src/MySqlConnector/MySqlConnection.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,16 @@ private async ValueTask<ServerSession> CreateSessionAsync(ConnectionPool? pool,
931931
var session = new ServerSession();
932932
session.OwningConnection = new WeakReference<MySqlConnection>(this);
933933
Log.Debug("Created new non-pooled Session{0}", session.Id);
934-
await session.ConnectAsync(connectionSettings, this, startTickCount, loadBalancer, activity, actualIOBehavior, connectToken).ConfigureAwait(false);
935-
return session;
934+
try
935+
{
936+
await session.ConnectAsync(connectionSettings, this, startTickCount, loadBalancer, activity, actualIOBehavior, connectToken).ConfigureAwait(false);
937+
return session;
938+
}
939+
catch (Exception)
940+
{
941+
await session.DisposeAsync(actualIOBehavior, default).ConfigureAwait(false);
942+
throw;
943+
}
936944
}
937945
}
938946
catch (OperationCanceledException) when (timeoutSource?.IsCancellationRequested is true)

0 commit comments

Comments
 (0)