fix(router): swap requestsMetric slice under exclusive Lock instead of RLock in collectMetrics - #7266
Open
harsh4vardhan wants to merge 1 commit into
Open
Conversation
collectMetrics acquires RLock (shared) then writes to requestsMetric (sets it to nil). Writing shared state under RLock violates sync.RWMutex contract: RLock permits concurrent readers; a writer that modifies data while holding RLock races with trackRequestMetrics callers that hold the exclusive Lock. Fix: take a short exclusive Lock, swap the slice into a local snapshot, release the lock, then iterate the snapshot. This mirrors the pattern used by trackRequestMetrics on the same field and keeps the lock held for only the swap, not the full iteration + diagnostics call. Fixes rudderlabs#7261 Signed-off-by: harsh4vardhan <hvardhan609@gmail.com>
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.
Summary
Resolves #7261.
collectMetricsacquiresrequestsMetricLock.RLock()(shared) andthen sets
rt.telemetry.requestsMetric = nilwhile holding it. Writingshared state under
RLockviolatessync.RWMutex's contract.trackRequestMetrics, called from every worker, takes the exclusiveLock()to append to the same slice — a concurrent read+reset underRLockraces with those appends.Fix
Take a short exclusive lock, swap the slice into a local snapshot,
release the lock, then iterate the snapshot outside the lock:
This is shorter under lock than the original (lock held only for the
pointer swap, not for the full iteration and diagnostics call) and
correct by
sync.RWMutexcontract.harsh4vardhan