Skip to content

Distributor, Ingester: stop the discarded series metrics goroutines - #7978

Open
blaketsk wants to merge 2 commits into
cortexproject:masterfrom
blaketsk:discardedseries-stop-trackers
Open

blaketsk wants to merge 2 commits into
cortexproject:masterfrom
blaketsk:discardedseries-stop-trackers

Conversation

@blaketsk

@blaketsk blaketsk commented Dec 1, 2026

Copy link
Copy Markdown

What this PR does:

Stops the goroutines updating the cortex_discarded_series and cortex_discarded_series_per_labelset metrics. DiscardedSeriesTracker and DiscardedSeriesPerLabelsetTracker started them with for range ticker.C, so they ran forever, including after the distributor or ingester owning them stopped.

  • Both trackers embed a small metricsVendor which runs the update loop and exposes Stop(). Stop() waits for the goroutine to exit, and is safe to call more than once or when the goroutine was never started.
  • ValidateMetrics.Stop() stops both trackers. The distributor and the ingester call it from their stopping functions.
  • Tests that create ValidateMetrics directly, or create an ingester without running its stopping function, now stop them.

Running the pkg/distributor, pkg/ingester and pkg/util/validation tests with goleak.VerifyTestMain reported about 2500 leaked tracker goroutines before this change (2044 in the distributor, 384 in the ingester, 82 in validation) and none after.

As with #7977, the stopping function doesn't run when the distributor or ingester fails to start, so the goroutines are still left running in that case. A failed start ends the process in production, so I kept this out of scope.

Which issue(s) this PR fixes:
Part of #3962

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
  • docs/configuration/v1-guarantees.md updated if this PR introduces experimental flags

AI disclosure: per GENAI_POLICY.md, the leak survey, the fix and the tests were prepared with Claude Code. I reviewed and verified the changes.

🤖 Generated with Claude Code

The discarded series trackers started a goroutine updating the
cortex_discarded_series and cortex_discarded_series_per_labelset metrics
which was never stopped, because it ranged over a ticker forever.

Make the trackers stoppable, add ValidateMetrics.Stop() and call it when
the distributor and the ingester stop. Stop the validate metrics in
tests that create them directly or never start the ingester. This
removes about 2500 goroutines leaked by the distributor, ingester and
validation tests.

Ref cortexproject#3962

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YDgKpXdZmLs9YSYU1ZSf8d
Signed-off-by: Andrey Osipov <[email protected]>
@blaketsk
blaketsk requested a review from a team as a code owner December 1, 2026 04:37
@blaketsk
blaketsk requested a review from SungJin1212 December 1, 2026 04:37

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant