Skip to content

Commit

Permalink
CSHARP-3761: Fix issue with ObjectDisposedException. (#729)
Browse files Browse the repository at this point in the history
CSHARP-3761: Fix issue with ObjectDisposedException.
  • Loading branch information
DmitryLukyanov authored Mar 2, 2022
1 parent f5b2597 commit 3b7d210
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/MongoDB.Driver.Core/Core/Servers/HeartbeatDelay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void RequestHeartbeat()

public void Wait(CancellationToken cancellationToken)
{
_taskCompletionSource.Task.Wait();
_taskCompletionSource.Task.Wait(cancellationToken);
}

private void TimerCallback(object state)
Expand Down
25 changes: 9 additions & 16 deletions src/MongoDB.Driver.Core/Core/Servers/ServerMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/

using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Net;
using System.Threading;
Expand All @@ -36,6 +35,7 @@ internal sealed class ServerMonitor : IServerMonitor
private readonly EndPoint _endPoint;
private HeartbeatDelay _heartbeatDelay;
private readonly object _lock = new object();
private readonly CancellationToken _monitorCancellationToken; // used to cancel the entire monitor
private readonly CancellationTokenSource _monitorCancellationTokenSource; // used to cancel the entire monitor
private readonly IRoundTripTimeMonitor _roundTripTimeMonitor;
private readonly ServerApi _serverApi;
Expand Down Expand Up @@ -101,7 +101,8 @@ public ServerMonitor(
eventSubscriber.TryGetEventHandler(out _sdamInformationEventHandler);
_serverApi = serverApi;

_heartbeatCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_monitorCancellationTokenSource.Token);
_monitorCancellationToken = _monitorCancellationTokenSource.Token;
_heartbeatCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_monitorCancellationToken);
}

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

toDispose = _connection;
Expand Down Expand Up @@ -147,20 +148,13 @@ public void Initialize()
if (_state.TryChange(State.Initial, State.Open))
{
_roundTripTimeMonitor.Start();
_serverMonitorThread = new Thread(ThreadStart) { IsBackground = true };
_serverMonitorThread.Start();
_serverMonitorThread = new Thread(new ParameterizedThreadStart(ThreadStart)) { IsBackground = true };
_serverMonitorThread.Start(_monitorCancellationToken);
}

void ThreadStart()
void ThreadStart(object monitorCancellationToken)
{
try
{
MonitorServer();
}
catch (OperationCanceledException)
{
// ignore OperationCanceledException
}
MonitorServer((CancellationToken)monitorCancellationToken);
}
}

Expand Down Expand Up @@ -218,10 +212,9 @@ private IConnection InitializeConnection(CancellationToken cancellationToken) //
return connection;
}

private void MonitorServer()
private void MonitorServer(CancellationToken monitorCancellationToken)
{
var metronome = new Metronome(_serverMonitorSettings.HeartbeatInterval);
var monitorCancellationToken = _monitorCancellationTokenSource.Token;

while (!monitorCancellationToken.IsCancellationRequested)
{
Expand Down

0 comments on commit 3b7d210

Please sign in to comment.