Fix race condition in PekkoRouteHolder using synchronized #15839
+6
−6
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.
In Pekko HTTP’s asynchronous execution model, multiple threads may access the same PekkoRouteHolder instance concurrently.
The class previously relied on a non-thread-safe StringBuilder and mutable fields without synchronization, which allowed concurrent route-matching operations to corrupt internal state.
Changes
Added synchronized to all methods that mutate internal state in PekkoRouteHolder.
Ensured that route path updates and unmatched path state updates occur atomically.
Technical Rationale (Alternatives Considered)
StringBuffer
StringBuffer synchronizes individual method calls, but it does not guarantee atomicity across multiple dependent state updates. Since the route path and unmatched path must be updated together to maintain consistency, this approach was insufficient to fully resolve the race condition.
Immutable String
Using immutable String objects was also considered. However, frequent string concatenation during Pekko route matching would result in excessive object creation and increased GC pressure, negatively impacting performance.
synchronized methods (chosen approach)
Synchronizing state-modifying methods ensures consistency across related fields with minimal structural changes. Given that route matching is not a hot path with heavy contention, the performance impact is negligible while fully addressing the race condition.
issue : #15681