Skip to content

Commit d410f5d

Browse files
committed
CSHARP-1774: Fix race condition in CommandEventHelper.
1 parent 3cf551f commit d410f5d

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/MongoDB.Driver.Core/Core/Connections/CommandEventHelper.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,23 @@ public void ConnectionFailed(ConnectionId connectionId, Exception exception)
200200
return;
201201
}
202202

203-
var pairs = _state.ToList();
204-
_state.Clear();
205-
foreach (var pair in pairs)
203+
var requestIds = _state.Keys;
204+
foreach (var requestId in requestIds)
206205
{
207-
pair.Value.Stopwatch.Stop();
208-
var @event = new CommandFailedEvent(
209-
pair.Value.CommandName,
210-
exception,
211-
pair.Value.OperationId,
212-
pair.Key,
213-
connectionId,
214-
pair.Value.Stopwatch.Elapsed);
206+
CommandState state;
207+
if (_state.TryRemove(requestId, out state))
208+
{
209+
state.Stopwatch.Stop();
210+
var @event = new CommandFailedEvent(
211+
state.CommandName,
212+
exception,
213+
state.OperationId,
214+
requestId,
215+
connectionId,
216+
state.Stopwatch.Elapsed);
215217

216-
_failedEvent(@event);
218+
_failedEvent(@event);
219+
}
217220
}
218221
}
219222

0 commit comments

Comments
 (0)