PRW2: Fix data corruption caused by shallow copy#7337
Merged
friedrichg merged 3 commits intoMar 9, 2026
Merged
Conversation
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
Shvejan
pushed a commit
to Shvejan/cortex
that referenced
this pull request
Mar 25, 2026
* Fix data corruption casused by shallow copy Signed-off-by: SungJin1212 <tjdwls1201@gmail.com> * fix test Signed-off-by: SungJin1212 <tjdwls1201@gmail.com> * fix lint Signed-off-by: SungJin1212 <tjdwls1201@gmail.com> --------- Signed-off-by: SungJin1212 <tjdwls1201@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.
This PR fixes a data corruption in the push handler by using a deep copy instead of a shallow copy. This ensures that a new backing array is allocated for the V1 request, decoupling it from the V2 request's lifecycle.
The problem
I have noticed that the data has been corrupted when Prometheus sends V1 and V2 requests with different users (V1 request for user1 and V2 request for user2); the samples in the V2 request are overwritten by V1 or vice versa.
The bug was caused by a shallow copy of Samples and Histograms when converting V2 requests to V1, leading to memory overlap and data corruption when objects were reused from the
sync.Pool.In the
convertV2RequestToV1function, the code was directly assigning slices from the V2 request to the V1 structure. Since these slices share the same underlying memory (backing array), when a V2 request was returned to thesync.Pooland subsequently overwritten by a new incoming request, the V1 request (which was still referencing that memory) would see its data change unexpectedly.The test
TestIngest_PRW2_MemoryIndependenceshows this situation.Which issue(s) this PR fixes:
Fixes #
Checklist
CHANGELOG.mdupdated - the order of entries should be[CHANGE],[FEATURE],[ENHANCEMENT],[BUGFIX]