Skip to content

Commit 3b7d210

Browse files
CSHARP-3761: Fix issue with ObjectDisposedException. (#729)
CSHARP-3761: Fix issue with ObjectDisposedException.
1 parent f5b2597 commit 3b7d210

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

src/MongoDB.Driver.Core/Core/Servers/HeartbeatDelay.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void RequestHeartbeat()
7676

7777
public void Wait(CancellationToken cancellationToken)
7878
{
79-
_taskCompletionSource.Task.Wait();
79+
_taskCompletionSource.Task.Wait(cancellationToken);
8080
}
8181

8282
private void TimerCallback(object state)

src/MongoDB.Driver.Core/Core/Servers/ServerMonitor.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515

1616
using System;
17-
using System.Collections.Concurrent;
1817
using System.Diagnostics;
1918
using System.Net;
2019
using System.Threading;
@@ -36,6 +35,7 @@ internal sealed class ServerMonitor : IServerMonitor
3635
private readonly EndPoint _endPoint;
3736
private HeartbeatDelay _heartbeatDelay;
3837
private readonly object _lock = new object();
38+
private readonly CancellationToken _monitorCancellationToken; // used to cancel the entire monitor
3939
private readonly CancellationTokenSource _monitorCancellationTokenSource; // used to cancel the entire monitor
4040
private readonly IRoundTripTimeMonitor _roundTripTimeMonitor;
4141
private readonly ServerApi _serverApi;
@@ -101,7 +101,8 @@ public ServerMonitor(
101101
eventSubscriber.TryGetEventHandler(out _sdamInformationEventHandler);
102102
_serverApi = serverApi;
103103

104-
_heartbeatCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_monitorCancellationTokenSource.Token);
104+
_monitorCancellationToken = _monitorCancellationTokenSource.Token;
105+
_heartbeatCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_monitorCancellationToken);
105106
}
106107

107108
public ServerDescription Description => Interlocked.CompareExchange(ref _currentDescription, null, null);
@@ -118,7 +119,7 @@ public void CancelCurrentCheck()
118119
{
119120
_heartbeatCancellationTokenSource.Cancel();
120121
_heartbeatCancellationTokenSource.Dispose();
121-
_heartbeatCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_monitorCancellationTokenSource.Token);
122+
_heartbeatCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_monitorCancellationToken);
122123
// the previous hello or legacy hello cancellation token is still cancelled
123124

124125
toDispose = _connection;
@@ -147,20 +148,13 @@ public void Initialize()
147148
if (_state.TryChange(State.Initial, State.Open))
148149
{
149150
_roundTripTimeMonitor.Start();
150-
_serverMonitorThread = new Thread(ThreadStart) { IsBackground = true };
151-
_serverMonitorThread.Start();
151+
_serverMonitorThread = new Thread(new ParameterizedThreadStart(ThreadStart)) { IsBackground = true };
152+
_serverMonitorThread.Start(_monitorCancellationToken);
152153
}
153154

154-
void ThreadStart()
155+
void ThreadStart(object monitorCancellationToken)
155156
{
156-
try
157-
{
158-
MonitorServer();
159-
}
160-
catch (OperationCanceledException)
161-
{
162-
// ignore OperationCanceledException
163-
}
157+
MonitorServer((CancellationToken)monitorCancellationToken);
164158
}
165159
}
166160

@@ -218,10 +212,9 @@ private IConnection InitializeConnection(CancellationToken cancellationToken) //
218212
return connection;
219213
}
220214

221-
private void MonitorServer()
215+
private void MonitorServer(CancellationToken monitorCancellationToken)
222216
{
223217
var metronome = new Metronome(_serverMonitorSettings.HeartbeatInterval);
224-
var monitorCancellationToken = _monitorCancellationTokenSource.Token;
225218

226219
while (!monitorCancellationToken.IsCancellationRequested)
227220
{

0 commit comments

Comments
 (0)