Skip to content

querier: drop redundant detachChunksFromBuffer copy - #7773

Open
mehrdadbn9 wants to merge 4 commits into
cortexproject:masterfrom
mehrdadbn9:fix/remove-redundant-detach-chunks-7732
Open

querier: drop redundant detachChunksFromBuffer copy#7773
mehrdadbn9 wants to merge 4 commits into
cortexproject:masterfrom
mehrdadbn9:fix/remove-redundant-detach-chunks-7732

Conversation

@mehrdadbn9

Copy link
Copy Markdown
Contributor

Summary

Fixes #7732.

#7670 added a detachChunksFromBuffer copy on the distributor queryable's ingester-read path, assuming chunk Data aliases the gRPC wire buffer. It does not: the gogo-generated Chunk.Unmarshal does m.Data = append(m.Data[:0], dAtA[iNdEx:postIndex]...), allocating a fresh slice per Recv. So the copy was a wasted allocation + memcpy, and because the QueryStreamResponse stays live for the whole streamingSelect body, the original and the copy are reachable at once — peak heap during the loop went up rather than down.

This change removes detachChunksFromBuffer and passes result.Chunks directly to chunkcompat.FromChunks, keeping only the FromLabelAdaptersToLabelsWithCopy change (label values do alias the buffer and still need detaching).

Test plan

  • Added TestChunksDoNotAliasWireBuffer: marshals a QueryStreamResponse, unmarshals it, and asserts chunk Data is a distinct allocation from the wire buffer (the invariant that makes the copy unnecessary), and that querying returns the correct bytes.
  • TestIngesterStreaming and the rest of pkg/querier still pass.
  • Removed the obsolete with_detach benchmark subtest (it exercised the deleted function); the without_detach path now matches production.

Checklist

  • CHANGELOG updated
  • Tests added/updated
  • DCO sign-off present

Signed-off-by: mehrdadbn9 mehrdadbn9@users.noreply.github.com

Mehrdad Biukian Naeini added 4 commits August 16, 2026 03:34
trimStringByBytes scans backwards from the truncation point to find a
UTF-8 rune start, but the loop had no lower bound. When a request field
consists only of UTF-8 continuation bytes (0x80-0xBF) there is no rune
start to land on, so size underflows past zero and bytesStr[-1] panics
with 'index out of range [-1]'. Because the active query tracker is
enabled by default and the panic happens on a goroutine without recover(),
an attacker-supplied match[]/query value can crash the querier.

Bound the scan with 'size > 0' so it stops at the start of the string
instead of underflowing. When no rune start exists the field is truncated
to an empty string, which is safe.

Add TestTrimForJsonMarshalContinuationBytes covering the all-continuation-
byte case; existing multi-byte tests use valid UTF-8 ('世') which always
terminates the scan at index 0 and therefore never exercised the underflow.

Fixes cortexproject#7729

Signed-off-by: ...
Signed-off-by: Mehrdad Biukian Naeini <mehrdad.biu@mtnirancell.ir>
…roject#7731)

Signed-off-by: Mehrdad Biukian Naeini <mehrdad.biu@mtnirancell.ir>
…#7731)

Signed-off-by: Mehrdad Biukian Naeini <mehrdad.biu@mtnirancell.ir>
Signed-off-by: Mehrdad Biukian Naeini <mehrdad.biu@mtnirancell.ir>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

detachChunksFromBuffer copies chunk data that Unmarshal already allocated separately (#7670)

1 participant