Make InMemory event store thread-safe#279
Open
artnim wants to merge 2 commits into
Open
Conversation
InMemoryEventStore uses a plain Dictionary with a check-then-add in CreateAsync, and InMemoryEventStream computes Version + 1 by enumerating its live List on append. Under concurrent use (e.g. an application host appending events on background threads while a test seeds streams) the collections corrupt, events get lost, or versions duplicate. Claude-Session: https://claude.ai/code/session_01J2j9sfE7zpM2mJq4dsz6m4
- InMemoryEventStore: replace the plain Dictionary and its check-then-add with a ConcurrentDictionary.TryAdd, making concurrent CreateAsync calls safe and stream creation atomic (exactly one caller wins, the rest get EventStreamAlreadyExistsException). - InMemoryEventStream: guard the event list with a Lock. Appends compute Version + 1 and add inside the lock, so concurrent appends can no longer corrupt the list, lose events, or assign duplicate versions. Reads (Version, LatestSnapshotVersion, GetLatestSnapshot, ListAsync*) take the same lock; the List* methods materialize a snapshot so enumeration does not observe a mutating list. - InMemoryTransactionalBatch: commit under the owning stream's lock. Claude-Session: https://claude.ai/code/session_01J2j9sfE7zpM2mJq4dsz6m4
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.
Problem
InMemoryEventStoreandInMemoryEventStreamare not safe for concurrent use:InMemoryEventStore.CreateAsyncdoes a check-then-add (ContainsKey+Add) on a plainDictionary— concurrent creates corrupt the dictionary (InvalidOperationException: Operations that change non-concurrent collections must have exclusive access…), and once corrupted every subsequent store call fails. Two racing creates for the same id can also both succeed.InMemoryEventStream.AppendAsynccomputesVersion + 1by enumerating the liveListand thenAdds to it — concurrent appends lose events, assign duplicate versions, or corrupt the list.InMemoryTransactionalBatch.CommitAsyncappends to the same raw list unguarded.This bites in practice when the store backs an ASP.NET Core test host (
WebApplicationFactory): background services or notification handlers append events on thread-pool threads while the test thread seeds streams. We hit it as a flaky CI failure in a downstream project — one corruption cascaded into three failing tests.Fix
InMemoryEventStore:ConcurrentDictionary+TryAdd, making creation atomic (exactly one winner, the rest getEventStreamAlreadyExistsException).InMemoryEventStream: aLockguards all reads and writes of the event list; appends compute the version inside the lock, and theList*methods materialize a snapshot under the lock so enumeration never observes a mutating list.InMemoryTransactionalBatch: commits under the owning stream's lock, so batched version computation stays consistent with concurrent direct appends.No public API changes;
Lockis fine since the package targets net10.0.Tests
First commit adds the failing tests (concurrent creates corrupt the store; concurrent appends lose events/duplicate versions — reproduced 3/3 runs before the fix), second commit makes them pass. A third test pins the exactly-one-winner contract for same-id concurrent creates. Full solution builds;
Papst.EventStore.Tests,CodeGeneration.Tests, and the InMemory tests all pass (the Cosmos/MongoDB integration suites need their emulators and were not run locally).