Skip to content

tsdb: fix division by zero in stale series compaction#17952

Merged
aknuds1 merged 1 commit intoprometheus:mainfrom
aknuds1:fix/stale-series-division-by-zero
Jan 29, 2026
Merged

tsdb: fix division by zero in stale series compaction#17952
aknuds1 merged 1 commit intoprometheus:mainfrom
aknuds1:fix/stale-series-division-by-zero

Conversation

@aknuds1
Copy link
Contributor

@aknuds1 aknuds1 commented Jan 28, 2026

Which issue(s) does the PR fix:

Fixes #17949

Does this PR introduce a user-facing change?

[BUGFIX] TSDB: Fix division by zero when computing stale series ratio with empty head.

Summary

Guard the stale series ratio calculation by checking numSeries > 0 before computing the ratio. This prevents division by zero when the head has no series.

When numSeries == 0:

  • If numStaleSeries > 0: ratio would be +Inf, incorrectly triggering compaction
  • If numStaleSeries == 0: ratio would be NaN, comparison returns false (safe but semantically incorrect)

The fix restructures the condition to check numSeries > 0 before computing the ratio, and moves the ratio calculation inside the condition block.

Test plan

  • Added TestStaleSeriesCompactionWithZeroSeries to verify CompactStaleHead() handles an empty head gracefully
  • Verified existing compaction tests pass

@aknuds1 aknuds1 force-pushed the fix/stale-series-division-by-zero branch from c0e48ec to 5af192f Compare January 28, 2026 08:21
Guard the stale series ratio calculation by checking numSeries > 0
before computing the ratio. This prevents division by zero when
the head has no series.

Fixes prometheus#17949

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
@aknuds1 aknuds1 merged commit 00a7faa into prometheus:main Jan 29, 2026
74 of 76 checks passed
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.

tsdb: Division by zero in stale series compaction ratio calculation

3 participants