Guard the counter value caches with a lock - #1871
Merged
Merged
Conversation
Each Counter cache is a plain Dictionary, written through GetOrAdd, while the number backing the name came from Interlocked.Increment. That mix only reads as thread safe: user code can reach Counter.Current from parallel work inside one test, and concurrent calls corrupted the dictionary. All the caches now share one lock, which makes the Interlocked increments redundant, so they become plain increments under that lock.
This was referenced Aug 26, 2026
This was referenced Aug 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Each
Countervalue cache (Guid,DateTime,DateTimeOffset,Date,Time, numeric id) is a plainDictionarywritten throughExtensions.GetOrAdd, while the number behind the name came fromInterlocked.Increment. The mix reads as thread safe but is not: the interlocked part guards only the counter, not the dictionary that stores the result.Counter.Currentis public and reachable from user code, so parallel work inside a single test hits it concurrently. Onmainthat reliably produces:and sometimes
Destination array was not long enoughfrom a resize racing a read.All the caches now share one lock.
ConcurrentDictionarywould have been the smaller diff, but itsGetOrAddcan run the factory more than once under contention, which would burn counter numbers and hand out a name that is then discarded. A lock keeps the numbering tight, and it also covers the numeric id path, which mutates two dictionaries (the per entity cache and the per entity counter) that no single concurrent collection would make atomic together.With the caches locked the interlocked increments are redundant, so they become plain increments, and the
Build*methods are commented as being called under the lock.CounterConcurrencyTestsdrives 500 distinct values throughParallel.ForEachfor guids and numeric ids, asserting one name per input and that each input keeps its name. Both fail onmain— 3 runs out of 3 while checking.Verify.Testspasses on net11.0 (1301) and net48 (1222);StaticSettingsTestsandApplyScrubbersTestspass.