fix(queryeviction): fix flaky TestPrometheusMetrics_IncrementedCorrectly#7680
Closed
krunaljain wants to merge 1 commit into
Closed
fix(queryeviction): fix flaky TestPrometheusMetrics_IncrementedCorrectly#7680krunaljain wants to merge 1 commit into
krunaljain wants to merge 1 commit into
Conversation
The eviction signal (closing the `evicted` channel) and the evictionsTotal metric increment both happen in QueryEvictor.running(), but Cancel() runs before Inc(). waitEvicted() only synchronizes on the signal, so the final assertion could observe the metric before the third increment lands, causing an intermittent failure (expected: 3, actual: 2). Poll for the metric with require.Eventually instead of asserting immediately. Fixes cortexproject#7604. AI-assistance note: drafted with Claude Code (root-cause analysis of the reported race and the test fix); reviewed and validated by me before submitting, per GENAI_POLICY.md. Signed-off-by: Krunal Jain <kjain25@apple.com>
Author
|
Closing in favor of #7616, which was already open (opened 2026-06-11, ahead of this one) and fixes the root cause more thoroughly — it reorders the eviction metric increment/registry deregistration to happen before the cancel signal, rather than the test-side polling approach here, and also catches a related double-eviction accounting bug I hadn't spotted. Apologies for the duplicate. |
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.
What this PR does
Fixes the flaky
TestPrometheusMetrics_IncrementedCorrectlytest inpkg/util/queryeviction.QueryEvictor.running()(pkg/util/queryeviction/evictor.go) callsvictim.Cancel()— which closesthe test's
evictedchannel — before incrementingevictionsTotal:The test synchronizes only on the eviction signal (
waitEvicted), not on the metric write, so thefinal assertion can observe the counter before the last increment lands:
Verified the flake is real
Checked out the pre-fix version of
evictor_test.goand stress-tested it directly (not just relyingon the issue report):
This reproduced the exact
expected: 3, actual: 2failure from the issue — 8 failures out of 500 runs.With the fix applied, the same 500-run stress test (
-cpu=1,2,4, plus a separate 100-run pass under-race) passes cleanly every time.The fix
Replace the immediate assertion with
require.Eventually, polling for the metric to reach theexpected value instead of asserting right after the signal:
Test-only change; no production behavior is affected.
Which issue(s) this PR fixes
Fixes #7604
Checklist
CHANGELOG.mdupdateddocs/configuration/v1-guarantees.mdupdated — N/A, no new flagsAI-assistance disclosure
Per
GENAI_POLICY.md: root-cause analysis of the race and the fix were drafted with Claude Code. Ireviewed the diff, understand the race (signal-before-metric ordering in
running()), and independentlyverified the flake reproduces pre-fix and is resolved post-fix using the stress-test runs described above.