Improves thread safety for concurrent request handling#90
Merged
dennisdoomen merged 1 commit intodennisdoomen:mainfrom Mar 31, 2026
Merged
Improves thread safety for concurrent request handling#90dennisdoomen merged 1 commit intodennisdoomen:mainfrom
dennisdoomen merged 1 commit intodennisdoomen:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Improves Mockly’s behavior under concurrent HttpClient usage by hardening state that is mutated during request handling, and adds a parallel test to exercise multi-threaded execution.
Changes:
- Add locking around host pattern normalization and make invocation counting atomic.
- Add a lock around
RequestCollection.Addto prevent concurrentList<T>mutation issues and keepSequenceassignment consistent. - Add a parallel test scenario (net6+) to exercise concurrent request execution.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| Mockly/RequestMock.cs | Adds locking for host normalization and switches invocation counting to Interlocked/Volatile. |
| Mockly/RequestCollection.cs | Serializes Add operations with a lock to avoid concurrent List<T> corruption. |
| Mockly.Specs/HttpMockSpecs.cs | Adds a parallel/concurrency test scenario for net6+. |
8b7d3bc to
d780c7f
Compare
dennisdoomen
requested changes
Mar 31, 2026
Addresses race conditions when multiple requests are processed simultaneously by adding locking to request collection and host normalization logic. Also ensures atomic updates to invocation counters and adds a parallel test scenario to verify multi-threaded behavior.
d780c7f to
24fff89
Compare
dennisdoomen
approved these changes
Mar 31, 2026
This was referenced Apr 2, 2026
This was referenced Apr 14, 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.
Addresses race conditions when multiple requests are processed simultaneously by adding locking to request collection and host normalization logic. Also ensures atomic updates to invocation counters and adds a parallel test scenario to verify multi-threaded behavior.
Remark: The test scenario does not really prove it's fully thread safe. You can run it hundreds of times for debugging purposes.
I've also avoided using
SemaphoreSlimor similar methods that would require marking classes as disposable. That would change the API surface for Mockly and probably is not desirable.