Thread-safe MessageBus collections + perf#33
Open
newky2k wants to merge 3 commits into
Open
Conversation
The handler and log-listener collections derive from Collection<T>, which is not thread-safe. Post iterates the collection while Subscribe/Unsubscribe mutate it, so concurrent use (the default path when RunPostOnSeperateTask is set or via PostAsync) could throw "Collection was modified" or read torn state. - Add a SyncRoot lock to both collections; guard all iteration and snapshot the result so handlers are invoked outside the lock (avoids reentrancy deadlock when a handler re-subscribes or posts). - Lock the service's compound check-then-act Subscribe/Unsubscribe/StopListening operations on the same root. - Make LogListernersCollection.FindAll snapshot eagerly (was lazy LINQ that enumerated after the lock released). - Optimise HandlersForEvent: drop per-handler ToLower allocations for OrdinalIgnoreCase compare, single-pass with no result alloc when no match. - Add ConcurrencyTest covering parallel Subscribe/Unsubscribe/Post (10 DataRow repeats) and parallel-post delivery count. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Summary
Collection<T>(not thread-safe), soPostiterating whileSubscribe/Unsubscribemutate — the default path underRunPostOnSeperateTaskorPostAsync— could throw "Collection was modified" or read torn state.SyncRootlock to both collections; guard all iteration and snapshot the result so handlers run outside the lock (no reentrancy deadlock when a handler re-subscribes or posts).Subscribe/Unsubscribe/StopListeningops on the same root.LogListernersCollection.FindAllnow snapshots eagerly (was lazy LINQ enumerating after the lock released).HandlersForEvent: drop per-handlerToLowerallocations for anOrdinalIgnoreCasecompare; single pass, no result allocation when nothing matches.Tests
ConcurrencyTest: parallel Subscribe/Unsubscribe/Post (10DataRowrepeats) + parallel-post delivery count.🤖 Generated with Claude Code