Skip to content

Commit 3f9cc2c

Browse files
committed
Catch and log exceptions.
1 parent db4931d commit 3f9cc2c

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

src/MySqlConnector/Core/BackgroundConnectionResetHelper.cs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,44 +76,51 @@ public static async Task ReturnSessionsAsync()
7676
// keep running until stopped
7777
while (!s_cancellationTokenSource.IsCancellationRequested)
7878
{
79-
// block until AddSession releases the semaphore
80-
Log.Info("Waiting for semaphore.");
81-
await s_semaphore.WaitAsync(s_cancellationTokenSource.Token).ConfigureAwait(false);
82-
83-
// process all sessions that have started being returned
84-
while (true)
79+
try
8580
{
86-
lock (s_lock)
81+
// block until AddSession releases the semaphore
82+
Log.Info("Waiting for semaphore.");
83+
await s_semaphore.WaitAsync(s_cancellationTokenSource.Token).ConfigureAwait(false);
84+
85+
// process all sessions that have started being returned
86+
while (true)
8787
{
88-
if (s_sessions.Count == 0)
88+
lock (s_lock)
8989
{
90-
if (localTasks.Count == 0)
91-
break;
92-
}
93-
else
94-
{
95-
foreach (var data in s_sessions)
90+
if (s_sessions.Count == 0)
91+
{
92+
if (localTasks.Count == 0)
93+
break;
94+
}
95+
else
9696
{
97-
localSessions.Add(data.Session);
98-
localTasks.Add(data.ResetTask);
97+
foreach (var data in s_sessions)
98+
{
99+
localSessions.Add(data.Session);
100+
localTasks.Add(data.ResetTask);
101+
}
102+
s_sessions.Clear();
99103
}
100-
s_sessions.Clear();
101104
}
102-
}
103105

104-
if (Log.IsDebugEnabled())
105-
Log.Debug("Found SessionCount {0} session(s) to return.", localSessions.Count);
106+
if (Log.IsDebugEnabled())
107+
Log.Debug("Found SessionCount {0} session(s) to return.", localSessions.Count);
106108

107-
while (localTasks.Count != 0)
108-
{
109-
var completedTask = await Task.WhenAny(localTasks).ConfigureAwait(false);
110-
var index = localTasks.IndexOf(completedTask);
111-
var session = localSessions[index];
112-
await session.Pool!.ReturnAsync(IOBehavior.Asynchronous, session).ConfigureAwait(false);
113-
localSessions.RemoveAt(index);
114-
localTasks.RemoveAt(index);
109+
while (localTasks.Count != 0)
110+
{
111+
var completedTask = await Task.WhenAny(localTasks).ConfigureAwait(false);
112+
var index = localTasks.IndexOf(completedTask);
113+
var session = localSessions[index];
114+
await session.Pool!.ReturnAsync(IOBehavior.Asynchronous, session).ConfigureAwait(false);
115+
localSessions.RemoveAt(index);
116+
localTasks.RemoveAt(index);
117+
}
115118
}
116119
}
120+
catch (Exception ex) when (!(ex is OperationCanceledException oce && oce.CancellationToken == s_cancellationTokenSource.Token))
121+
{
122+
Log.Error("Unhandled exception: {0}", ex);
123+
}
117124
}
118125
}
119126

0 commit comments

Comments
 (0)