Skip to content

Recover panics from wrapped collectors (match Gather safeCollect)#2029

Open
greymoth-jp wants to merge 2 commits into
prometheus:mainfrom
greymoth-jp:fix/wrapped-collector-panic-recovery
Open

Recover panics from wrapped collectors (match Gather safeCollect)#2029
greymoth-jp wants to merge 2 commits into
prometheus:mainfrom
greymoth-jp:fix/wrapped-collector-panic-recovery

Conversation

@greymoth-jp

Copy link
Copy Markdown

wrappingCollector.Collect/.Describe (prometheus/wrap.go) run the inner collector in a child goroutine, so the panic recovery #1961 added to Registry.Gather (safeCollect) can't catch it — recover is per-goroutine. A collector registered via WrapRegistererWith (the standard const-label/prefix API) that panics still crashes the whole process.

#1961 hardened the unwrapped path; the wrapped path was the symmetric leftover. The fix applies the same recovery to the wrapped collect/describe. Verified both ways: old panics crash the test binary (exit 1); fixed surfaces them as a Gather error. Full ./prometheus/ suite passes, 0 regression.

@greymoth-jp greymoth-jp force-pushed the fix/wrapped-collector-panic-recovery branch from 8e3d3d7 to e4a9aa2 Compare June 26, 2026 07:43
PR prometheus#1961 added safeCollect to recover panics raised by a collector's
Collect method during Registry.Gather, turning a misbehaving collector
(issue prometheus#1877) into a recoverable error instead of a process crash.

However, a collector registered via WrapRegistererWith is wrapped in a
wrappingCollector whose Collect (and Describe) runs the inner collector
in a NEW goroutine. A recover only catches panics from its own
goroutine, so safeCollect in the gather worker cannot catch a panic
raised inside the wrappingCollector goroutine; it still crashes the
whole process. WrapRegistererWith is the standard way to add constant
labels/prefixes, so this leaves the common path unprotected.

Recover the panic inside the wrappingCollector goroutines themselves and
surface it as an invalid Metric (Collect) / invalid Desc (Describe),
mirroring safeCollect's behaviour. Adds regression tests for both paths.

Signed-off-by: greymoth-jp <m.hirakawa07@icloud.com>
Signed-off-by: greymoth-jp <m.hirakawa07@icloud.com>
@greymoth-jp greymoth-jp force-pushed the fix/wrapped-collector-panic-recovery branch from 805301e to 5facb0d Compare June 28, 2026 12:43
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.

1 participant