Skip to content

Commit 6e0efd0

Browse files
committed
Catch only I/O exceptions thrown from ResetConnectionAsync.
Catching all exceptions could mask a programming error introduced in ResetConnectionAsync, with the side-effect of silently disabling connection pooling.
1 parent 0e7e53f commit 6e0efd0

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/MySqlConnector/MySqlClient/ConnectionPool.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Threading;
@@ -50,17 +50,7 @@ public async Task<MySqlSession> GetSessionAsync(MySqlConnection connection, IOBe
5050
{
5151
if (m_connectionSettings.ConnectionReset)
5252
{
53-
try
54-
{
55-
// Depending on the server (Aurora?), this can randomly fail when re-authenticating to the server.
56-
// If it does, we can just pretend it didn't happen and continue with creating a new session below.
57-
await session.ResetConnectionAsync(m_connectionSettings, ioBehavior, cancellationToken).ConfigureAwait(false);
58-
reuseSession = true;
59-
}
60-
catch (Exception)
61-
{
62-
reuseSession = false;
63-
}
53+
reuseSession = await session.TryResetConnectionAsync(m_connectionSettings, ioBehavior, cancellationToken).ConfigureAwait(false);
6454
}
6555
else
6656
{

src/MySqlConnector/Serialization/MySqlSession.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public async Task ConnectAsync(ConnectionSettings cs, IOBehavior ioBehavior, Can
257257
m_payloadHandler = new CompressedPayloadHandler(m_payloadHandler.ByteHandler);
258258
}
259259

260-
public async Task ResetConnectionAsync(ConnectionSettings cs, IOBehavior ioBehavior, CancellationToken cancellationToken)
260+
private async Task ResetConnectionAsync(ConnectionSettings cs, IOBehavior ioBehavior, CancellationToken cancellationToken)
261261
{
262262
VerifyState(State.Connected);
263263
if (ServerVersion.Version.CompareTo(ServerVersions.SupportsResetConnection) >= 0)
@@ -288,6 +288,26 @@ public async Task ResetConnectionAsync(ConnectionSettings cs, IOBehavior ioBehav
288288
}
289289
}
290290

291+
public async Task<bool> TryResetConnectionAsync(ConnectionSettings cs, IOBehavior ioBehavior, CancellationToken cancellationToken)
292+
{
293+
try
294+
{
295+
await ResetConnectionAsync(cs, ioBehavior, cancellationToken).ConfigureAwait(false);
296+
return true;
297+
}
298+
catch (EndOfStreamException)
299+
{
300+
}
301+
catch (IOException)
302+
{
303+
}
304+
catch (SocketException)
305+
{
306+
}
307+
308+
return false;
309+
}
310+
291311
private async Task SwitchAuthenticationAsync(ConnectionSettings cs, PayloadData payload, IOBehavior ioBehavior, CancellationToken cancellationToken)
292312
{
293313
// if the server didn't support the hashed password; rehash with the new challenge

0 commit comments

Comments
 (0)