Skip to content

[Info] InvalidOperationException: Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct #26868

@benaadams

Description

@benaadams

Just raising this so people will find the issue in a search.

This is an intentional new exception that is being thrown by the Dictionary in newer .NET Core (issue https://github.com/dotnet/corefx/issues/28123, PR dotnet/coreclr#16991) which indicates the dictionary is being incorrectly modified concurrently by multiple threads (it wouldn't have been detected previously and could lead to infinite loops, or incorrect data returned)

Also applies now to HashSets (issue https://github.com/dotnet/corefx/issues/28209, PR dotnet/corefx#28225)

Dictionary and HashSet are not safe for concurrent readers and writers. Dictionaries/Hashsets that are not written to after set-up are safe for concurrent readers. They are never safe for a write and any other operation (read or write).

They either need:

  1. Single threaded access
  2. Use under lock
  3. Change to a ConcurrentDictionary
  4. Or use immutable approach:
    1. Change to ImmutableDictionary, ImmutableHashSet or copy dict, add to new copy
    2. Interlocked.CompareExchange on reference with new dict (returned from Add for immutable, or copy for Dict) with added item
    3. If CompareExchange fails do re-copy + add (go to ii.)

/cc @danmosemsft @vancem @jkotas @stephentoub

Issues are being filed upstream DuendeArchive/IdentityServer4#2453 and dotnet/efcore#12713 and dotnet/efcore#12713 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions