fix(core): synchronize CanRegistry against concurrent access - #22
Open
dborgards wants to merge 1 commit into
Open
fix(core): synchronize CanRegistry against concurrent access#22dborgards wants to merge 1 commit into
dborgards wants to merge 1 commit into
Conversation
The registry's dictionaries were plain, unsynchronized maps: any late (re-)registration racing the public read surface (Resolve/Factory/ TryOpenEndPoint/EnumerateEndPoints) could corrupt state or throw from concurrent enumeration. All accesses now take one private lock; readers that iterate (Factory's known-list, EnumerateEndPoints) snapshot under the lock and enumerate outside, and endpoint handlers are invoked outside the lock so slow opens never block readers. CanKit.Core exposes InternalsVisibleTo to the test assembly so the new CanRegistryConcurrencyTests can drive the internal Register* surface: parallel RegisterEndPoint churn plus concurrent readers on the shared singleton.
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
CanRegistry's dictionaries were plain, unsynchronized maps: any late (re-)registration racing the public read surface (Resolve/Factory/TryOpenEndPoint/EnumerateEndPoints) could corrupt state or throw from concurrent enumeration. This is a realistic scenario for applications that register endpoints/providers after startup (plugin-style hosts, adapter assemblies finishing their preload registration while a bus is already being opened).Fix
Factory's known-list,EnumerateEndPoints) snapshot under the lock and enumerate outside of it.Opennever blocks other readers (and a handler that itself touches the registry cannot deadlock).No public API changes. Ported from a fix we have been running in our fork.
Tests
CanRegistryConcurrencyTests: parallelRegisterEndPointchurn (16 schemes) plus 4 concurrent readers spinning onTryOpenEndPoint/EnumerateEndPointsagainst the shared singleton.CanKit.Corenow exposesInternalsVisibleToto the test assembly so the test can drive the internalRegister*surface.dotnet build CanKitAdapters.slnf -c Release/-c Fakeclean;CANKIT_TEST_ADAPTERS=CanKit.Adapter.Virtual dotnet test CanKitAdapters.slnf -c Release -f net8.0→ 73 passed, 0 failed (72 baseline + 1 new).Note
The test cleans up its stress registrations from the shared singleton via reflection over the registry's private field names — a rename of those fields would break the test with a clear error message.