Skip to content

PRW2: Fix data corruption caused by shallow copy#7337

Merged
friedrichg merged 3 commits into
cortexproject:masterfrom
SungJin1212:Fix-data-corruption-caused-by-shallow-copy
Mar 9, 2026
Merged

PRW2: Fix data corruption caused by shallow copy#7337
friedrichg merged 3 commits into
cortexproject:masterfrom
SungJin1212:Fix-data-corruption-caused-by-shallow-copy

Conversation

@SungJin1212
Copy link
Copy Markdown
Member

@SungJin1212 SungJin1212 commented Mar 9, 2026

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 convertV2RequestToV1 function, 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 the sync.Pool and 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_MemoryIndependence shows this situation.

Which issue(s) this PR fixes:
Fixes #

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
@dosubot dosubot Bot added the type/bug label Mar 9, 2026
@SungJin1212 SungJin1212 changed the title Fix data corruption casused by shallow copy Fix data corruption caused by shallow copy Mar 9, 2026
@SungJin1212 SungJin1212 changed the title Fix data corruption caused by shallow copy PRW2: Fix data corruption caused by shallow copy Mar 9, 2026
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
Copy link
Copy Markdown
Member

@friedrichg friedrichg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Mar 9, 2026
@friedrichg friedrichg merged commit a023b5c into cortexproject:master Mar 9, 2026
90 of 95 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size/L type/bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants