[CSHARP-449] MongoServer.VerifyInstances modifies collection that is being iterated Created: 18/Apr/12  Updated: 02/Apr/15  Resolved: 20/Apr/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.4.1
Fix Version/s: 1.4.2

Type: Bug Priority: Major - P3
Reporter: Aristarkh Zagorodnikov Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

The MongoServer.VerifyInstances iterates over the _instances member. Unfortunately, it occasionally calls RemoveInstance (when list is updated). This leads to _instances list to be updated and iteration to fail. I think snapshotting a list of instances with ToArray() call would be enough.



 Comments   
Comment by Craig Wilson [ 19/Apr/12 ]

Pull request submitted.

Comment by Aristarkh Zagorodnikov [ 19/Apr/12 ]

Counting backwards is surely better than ToArray().

Comment by Craig Wilson [ 19/Apr/12 ]

How about counting backwards so we don't have to make a copy...

for (int i = _instances.Count - 1; i >= 0; i--)
{
  if (!instanceAddresses.Contains(_instances[i].Address))
  {
    RemoveInstance(_instances[i]);
  }
}

Generated at Wed Feb 07 21:36:52 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.