Skip to content

exp/api/remote: reset pooled buffer before generic marshal - #2139

Merged
kakkoyun merged 1 commit into
prometheus:mainfrom
lowgame:fix-generic-proto-buffer-26517d42
Oct 3, 2026
Merged

kakkoyun merged 1 commit into
prometheus:mainfrom
lowgame:fix-generic-proto-buffer-26517d42

Conversation

@lowgame

@lowgame lowgame commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

@vesari, this fixes corruption in the experimental remote-write client's generic protobuf path.

Reproduction

A regression test seeds the API's buffer pool with a non-empty slice, sends a protobuf message that uses the generic proto.Message branch, decompresses the HTTP request body, and compares it with proto.Marshal output. Before the production change it failed deterministically:

unexpected payload: got 7374616c65087b10c803, want 087b10c803

The leading bytes are the stale pooled contents.

Root cause

proto.MarshalOptions.MarshalAppend appends to the destination's current length. Buffers returned by the API pool can retain a non-zero length after a previous write, but the generic protobuf branch passed that slice directly to MarshalAppend. The optimized vtprotobuf and gogo branches already resize their buffers before writing.

Solution

Reset the pooled slice to length zero before the generic marshal while retaining its capacity. The regression test exercises this branch with timestamppb.Timestamp, which implements proto.Message but not the optimized marshal interfaces.

Validation

From exp/:

  • go test ./api/remote -run '^TestRemoteAPI_Write_GenericProtoResetsPooledBuffer$' -count=1 — pass
  • go test -race ./api/remote -run '^TestRemoteAPI_Write_GenericProtoResetsPooledBuffer$' -count=10 — pass
  • go test ./api/remote/... -count=1 — pass
  • go test ./... -count=1 — pass
  • go vet ./... — pass
  • golangci-lint run --new-from-rev=HEAD ./api/remote/... — 0 new issues

The unrestricted lint run reports five pre-existing findings in unchanged lines (one modernize, one revive, and three usestdlibvars).

Signed-off-by: lowgame <128625014+lowgame@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The targeted fix correctly addresses the append semantics and includes appropriate regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes stale pooled bytes corrupting payloads in the experimental remote-write client’s generic protobuf path.

Changes:

  • Reset the pooled buffer before MarshalAppend.
  • Add a deterministic regression test covering generic protobuf serialization.
File Description
exp/​api/​remote/​remote_api.go Resets the buffer while retaining capacity.
exp/​api/​remote/​remote_api_test.go Verifies stale bytes are excluded from requests.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@kakkoyun
kakkoyun enabled auto-merge (squash) October 3, 2026 10:50

@kakkoyun kakkoyun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@kakkoyun
kakkoyun merged commit 2ad001a into prometheus:main Oct 3, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants