Return the stored profile from concurrent CreateProfile calls - #117
Merged
Merged
Conversation
CreateProfile looked the id up, then created a Profile and ignored whether TryAdd stored it. Two callers racing on the same id could each return their own instance while only one was in the map, so bindings set on the other were invisible to GetProfile and never saved. Use ConcurrentDictionary.GetOrAdd so every caller gets the stored instance. Fixes #112 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P6PRqvoi3pKgXQu5F8XJb1
Use GetOrAdd's key parameter rather than capturing normalizedId (S6612), and pass the test's cancellation token to Barrier.SignalAndWait (MSTEST0049). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P6PRqvoi3pKgXQu5F8XJb1
|
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.



Fixes #112
What was wrong
ProfileManager.CreateProfilelooked the id up and, if it wasn't there, created aProfileand calledTryAddwithout checking whether the add succeeded. When two callers raced on the same id, both could get past the lookup. The caller whoseTryAddlost still returned its own instance, which was never stored. Any chord set on that instance was invisible toGetProfile, andSaveAsyncnever persisted it.Change
CreateProfilenow returns_profiles.GetOrAdd(normalizedId, _ => new Profile(...)), so every caller gets the one stored instance. Under contention the factory may run more than once, but only one result is stored and returned, which is what the documented contract ("the created profile, or existing profile if it already exists") needs.Tests
New test:
ProfileCreateConcurrencyTests.CreateProfile_CalledConcurrentlyForOneId_ReturnsTheStoredProfileToEveryCaller. Over 500 trials, 8 threads behind aBarrierall callCreateProfile("p", "P"), and the test asserts that every returned instance is the oneGetProfile("p")returns.dotnet test: 75 passed.This PR doesn't touch the neighbouring
ProfileManagerconcurrency issues, #109 (the innerDictionaryinBindChord) and #113 (RenameProfile).🤖 Generated with Claude Code
https://claude.ai/code/session_01P6PRqvoi3pKgXQu5F8XJb1
Generated by Claude Code