Skip to content

fix metric vec label input aliasing#2019

Closed
Saithej2k wants to merge 1 commit into
prometheus:mainfrom
Saithej2k:codex/fix-issue-1951-label-copy
Closed

fix metric vec label input aliasing#2019
Saithej2k wants to merge 1 commit into
prometheus:mainfrom
Saithej2k:codex/fix-issue-1951-label-copy

Conversation

@Saithej2k

Copy link
Copy Markdown

Summary

  • clone caller-provided Labels maps when MetricVec has no label constraints
  • clone caller-provided label-value slices in the no-constraint GetMetricWithLabelValues path
  • add regression tests that mutate caller-owned inputs between hashing and metric creation

Why

When no label constraints are configured, constrainLabels and constrainLabelValues returned the caller-owned map or slice directly. MetricVec hashes those values first and can then read the same caller-owned input again while creating or matching a metric. If the caller mutates that input concurrently, the internal reads can race and can associate the hash with mutated label values.

Fixes #1951.

Validation

  • go test -race ./prometheus -run 'TestMetricVecGetMetricWith.*Copies' -count=1
  • go test -race ./prometheus -run 'TestMetricVec' -count=1

I also tried go test -race ./prometheus -count=1, but my local Go runtime metrics differed from the checked-in Go collector fixtures, causing unrelated go_collector_latest_test.go failures.

Signed-off-by: Saithej2k <saithej2003@gmail.com>
@Saithej2k Saithej2k changed the title [codex] fix metric vec label input aliasing fix metric vec label input aliasing Jun 7, 2026
@Saithej2k Saithej2k marked this pull request as ready for review June 7, 2026 12:39
@Saithej2k Saithej2k marked this pull request as draft June 7, 2026 12:40
@Saithej2k Saithej2k marked this pull request as ready for review June 7, 2026 12:41
@bwplotka

Copy link
Copy Markdown
Member

Thanks but we prefer performance vs making it ultra safe for all cases. However some commentary would be useful. See #1951 (comment)

@bwplotka bwplotka closed this Jun 22, 2026
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.

bug: metrics with no label constraint are exposed to potential race conditions

2 participants