Skip to content

SmbTransport.ClearCachedConnections with force=true leads to InvalidOperationException #40

@GoldenCave

Description

@GoldenCave

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions