Avoid race condition when ProducerInstance is called multiple times for the same flow#45
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR addresses a race condition that occurs when ProducerInstance::produceAndQueueJobs() is called concurrently for the same flow (e.g., when multiple HTTP requests arrive simultaneously for an HttpRequestProducer). The race condition could cause jobs to be enqueued multiple times or lost entirely due to concurrent access to a shared batch in the QueueManager.
Key changes:
- Introduced batch IDs (UUIDs) to isolate concurrent batches in the QueueManager
- Modified the ProducerQueueManagerInterface to require a batchId parameter for enqueue and flush operations
- Updated QueueManager to maintain separate batches per batchId instead of a single shared batch
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/Integration/HttpRequestProducerAndWorkerTest.php | Added new test case to verify concurrent HTTP requests are properly handled without job loss or duplication |
| src/Service/QueueManager.php | Refactored from single batch array to multi-batch map keyed by batchId, updated all batch operations to use batchId parameter |
| src/Service/ProducerQueueManagerInterface.php | Extended interface methods (enqueue and flush) to accept batchId parameter for batch isolation |
| src/ProducerInstance.php | Generate unique UUID for each produceAndQueueJobs invocation and pass it to enqueue/flush operations |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
azambon
requested changes
Dec 23, 2025
3db8dc7 to
d1d70a7
Compare
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.
If for a given
ProducerInstancefor a flow, the methodProducerInstance::produceAndQueueJobs()is called concurrently multiple times (for example because the produces is anHttpRequestProducerand the same request arrives multiple times at the same time) there is the chance that same jobs are enqueued multiple times or, worst, some jobs are lost.This is because the "batch" of the
QueueManageris always the same and is shared between the multiple calls ofProducerInstance::produceAndQueueJobs().