Skip to content

prometheus: add test reproducing waitForCooldown deadlock (#2147) + fix (resistance to panics) - #2161

Merged
bwplotka merged 3 commits into
mainfrom
bwplotka/repro-2147-wait-for-cooldown
Oct 8, 2026
Merged

bwplotka merged 3 commits into
mainfrom
bwplotka/repro-2147-wait-for-cooldown

Conversation

@bwplotka

@bwplotka bwplotka commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Repro of #2147

Demonstrate how a recovered panic inside histogram.Write (after flipping countAndHotIdx and before addAndResetCounts) leaves coldCounts unmerged, causing the next Write() to spin forever in waitForCooldown.

Note

This is expected to fail to reproduce the issue. #2162 can be then merged and it's clear tests were not changed, yet now passing.

Ok, but can Write(nil) or panic even happen?

In both histogram.Write and noObjectivesSummary.Write, passing out == nil (Write(nil), which panics at out.Histogram = his and out.Summary = sum) is the only thing that can trigger a recoverable panic between flipping countAndHotIdx and merging coldCounts into hotCounts (all slice accesses in between are bounded by len(h.upperBounds), and OOM on allocation is a fatal runtime.throw rather than a recoverable panic).

In standard Registry.Gather(), Write(nil) cannot happen because processMetric always allocates dtoMetric := &dto.Metric{} before calling metric.Write(dtoMetric). It can only happen if a custom Collector or caller invokes Write(nil) directly (for instance inside Collect, where safeCollect recovers the panic while defer h.mtx.Unlock() unlocks the mutex with coldCounts unmerged).

@bwplotka
bwplotka added this pull request to stack #2163 October 7, 2026 13:45
@bwplotka
bwplotka force-pushed the bwplotka/repro-2147-wait-for-cooldown branch 3 times, most recently from a74eb8c to ecc1fdc Compare October 7, 2026 14:07
Demonstrate how a recovered panic inside histogram.Write (after flipping countAndHotIdx and before addAndResetCounts) leaves coldCounts unmerged, causing the next Write() to spin forever in waitForCooldown.

Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
@bwplotka
bwplotka force-pushed the bwplotka/repro-2147-wait-for-cooldown branch from ecc1fdc to 3a29f73 Compare October 7, 2026 14:35
Defer addAndResetCounts immediately after waitForCooldown in histogram.Write (and merge before populating out in noObjectivesSummary.Write) so a panic inside Write cannot unlock the mutex with coldCounts unmerged and wedge subsequent Write calls in waitForCooldown.

Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
@bwplotka
bwplotka removed this pull request from stack #2163 October 8, 2026 12:51
…#2162)

Defer addAndResetCounts immediately after waitForCooldown in histogram.Write (and merge before populating out in noObjectivesSummary.Write) so a panic inside Write cannot unlock the mutex with coldCounts unmerged and wedge subsequent Write calls in waitForCooldown.

Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
@bwplotka
bwplotka enabled auto-merge (squash) October 8, 2026 12:51
@bwplotka bwplotka changed the title prometheus: add test reproducing waitForCooldown deadlock (#2147) prometheus: add test reproducing waitForCooldown deadlock (#2147) + fix (resistance to panics) Oct 8, 2026
@bwplotka
bwplotka disabled auto-merge October 8, 2026 12:52
@bwplotka
bwplotka enabled auto-merge (squash) October 8, 2026 12:52
@bwplotka
bwplotka merged commit 53e30e1 into main Oct 8, 2026
15 checks passed
@bwplotka
bwplotka deleted the bwplotka/repro-2147-wait-for-cooldown branch October 8, 2026 12:56
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.

2 participants