Modules: Execute mutations in sequence (clean implementation) #4586
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
This PR implements sequential mutation execution to ensure mutations are applied in the order hooks are listed in configuration, not in the order they complete. This makes mutation behavior predictable and allows dependent mutations to work correctly.
This is a clean-room implementation built from first principles that achieves the same goal as PR #4279 while proactively addressing all critical issues identified during code review.
Problem Statement
Current behavior: Hooks execute in parallel and mutations are applied in an unpredictable order based on which hook completes first (race condition).
Desired behavior: Mutations should be applied in the order hooks are listed in config, providing predictable sequencing for dependent mutations.
Solution Approach
Uses the same core strategy as PR #4279:
[]hookResponse[P]with size =len(group.Hooks)WaitGroupcompletes, responses are in deterministic orderKey Improvements Over Original Implementation
1. Proper Goroutine Cancellation ✅
2. Simplified Timeout Handling ✅
ctx.Done()instead oftime.After()3. Zero-Value Bug Fix ✅
handleHookResponsesto skip canceled hooks4. Race-Free Parameter Passing ✅
newPayloadvariable5. Comprehensive Documentation ✅
Test Coverage
✅ Sequential Mutation Order Test
Added
TestSequentialMutationExecutionthat proves mutations apply in config order using channel-based synchronization (no flakytime.Sleep):Test Design:
User.CustomData(building on previous values)hook-2 → hook-3 → hook-1(out of config!)The Proof:
Test uses only concurrency constructs (channels, WaitGroups), no time-based testing.
✅ All Existing Tests Pass
Comparison with Related PRs
vs. PR #4279 (Original)
Detailed comparison available here: https://github.com/prebid/prebid-server/blob/28a5cd15d06e1f10cb8e89a8295a0b128ab2ce95/IMPLEMENTATION_COMPARISON.md
vs. PR #4585 (Original + Improvements Plan)
ctx.Done()instead of timer managementRelated PRs
Files Changed
hooks/hookexecution/execution.go- Core implementation (~80 lines changed)hooks/hookexecution/mocks_test.go- AddedmockSequencedHookfor testinghooks/hookexecution/executor_test.go- Added sequential mutation testCode Review Notes
This implementation incorporates valuable feedback during development:
ctx.Done()instead oftime.NewTimerThe implementation was built as a clean-room exercise to verify the approach works and compare design decisions.
🤖 Generated with Claude Code
Co-Authored-By: Claude [email protected]