#13970·traefik

Recovery middleware loses the stack trace when a panic message and its "Stack" log are split across two writes

Author: mdabydeenCreated Sep 27, 2026Updated Sep 28, 2026
Labelskind/bug/possiblearea/logs

What did you do?

After #13693, where the traefik-internal-recovery output was the only way to see what paniced but carried no trace, I looked at recoverFunc in pkg/middlewares/recovery/recovery.go and found that the panic message and the stack trace are written as two independent log events.

What did you expect to see?

When the traefik-internal-recovery middleware recovers from a panic, the log record that says Recovered from panic in HTTP handler [...] is also the one that carries the stack trace.

What did you see instead?

A good fraction of panics log the Recovered from panic line but no stack trace, so the panic cannot be diagnosed.

recoverFunc emits the panic message and the stack trace as two separate zerolog writes:

go
logger.Error().Msgf("Recovered from panic in HTTP handler [%s - %s]: %+v", req.RemoteAddr, req.URL, err)
// ...
logger.Error().Msgf("Stack: %s", buf)

If the process exits between the two writes — which is exactly what happens when a panic races shutdown (see #13693, where a WebSocket handler unwinds after the access-log handler has closed) — the first write reaches the log sink but the second is lost. Over the panicking restart runs in #13693, roughly a third of the Recovered from panic lines had no Stack: line at all, which is why the original production occurrence was undiagnosable: its only surviving record carried no trace.

Fix

Emit the message and the stack trace as a single zerolog event, so they are one write and cannot be split apart by process exit:

go
logger.Error().Msgf("Recovered from panic in HTTP handler [%s - %s]: %+v\nStack:\n%s", req.RemoteAddr, req.URL, err, buf)

More

  • This is a distinct, smaller problem from #13693 itself: #13693 is the race that produces the panic; this one is why the resulting log is often useless.
  • The accompanying PR includes a regression test that fails before the fix and passes after: it drives a recoverable panic through the middleware with a zerolog logger capturing to a buffer, and asserts that the same log event contains both Recovered from panic and Stack:.