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:
- Single threaded access
- Use under lock
- Change to a
ConcurrentDictionary
- Or use immutable approach:
- Change to
ImmutableDictionary, ImmutableHashSet or copy dict, add to new copy
Interlocked.CompareExchange on reference with new dict (returned from Add for immutable, or copy for Dict) with added item
- 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)
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)
DictionaryandHashSetare 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:
ConcurrentDictionaryImmutableDictionary,ImmutableHashSetor copy dict, add to new copyInterlocked.CompareExchangeon reference with new dict (returned from Add for immutable, or copy for Dict) with added itemCompareExchangefails 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)