-
Notifications
You must be signed in to change notification settings - Fork 34
Description
When calling SmbTransport.ClearCachedConnections (true) with force=true, we experienced an InvalidOperationException:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at SharpCifs.Smb.SmbTransport.ClearCachedConnections(Boolean force)
Here is the code: SmbTransport.ClearCachedConnections. The code enumerates over that collection (SmbConstants.Connections), and then tries to remove an element from it (with force=true), which gives it the InvalidOperationException. At least that is what stackoverflow suggests "You can't remove items from the same list you're using in the foreach, you'll get an exception (System.InvalidOperationException)."
Otherwise this other stack overflow suggests that the collection itself is modified by another thread, but the code above has a lock on the collection, so I think it is the one responsible stack overflow
So, it seems if we changed it to use an index-based for loop instead, we could get around this problem.