Skip to content

caddyhttp: log recovered handler panics at ERROR level - #7924

Merged
francislavoie merged 3 commits into
caddyserver:masterfrom
ousamabenyounes:issue-7923-panic-log-level
Aug 10, 2026
Merged

francislavoie merged 3 commits into
caddyserver:masterfrom
ousamabenyounes:issue-7923-panic-log-level

Conversation

@ousamabenyounes

Copy link
Copy Markdown
Contributor

Assistance Disclosure

AI/LLM assistance was used and is disclosed here: I directed an AI coding
assistant (Claude) to help investigate the root cause, draft the fix, and
write the tests. I personally reviewed, ran, and verified every line locally
(RED→GREEN and the diff-coverage gate below), understand the change, and take
responsibility for it. I have the rights to contribute this change and will
sign the CLA.

Fixes #7923

Root cause

Go's net/http server recovers a panic that escapes a handler and writes the
message and stack trace to http.Server.ErrorLog (see the deferred recover
in net/http.(*conn).serve, which emits lines prefixed http: panic serving).

In caddyhttp, App.Start() wired that ErrorLog through
zap.NewStdLogAt(app.logger.Named("stdlib"), zap.DebugLevel) — i.e. every
stdlib server message, including recovered handler panics, was logged at
DEBUG. At the default INFO level these panics were completely invisible, so
a handler panic in production would be silently swallowed.

Caddy does not recover handler panics itself in the serving path (only
reverseproxy health-checks/metrics use recover()), so a handler panic
genuinely reaches net/http's serve loop and its ErrorLog.

The fix

Route http.Server.ErrorLog through a small io.Writer that inspects each
message: lines prefixed http: panic serving are logged at ERROR (so they
stay visible at the default level), while other low-signal stdlib messages
remain at DEBUG. No behavior change for non-panic messages.

The change is confined to modules/caddyhttp/app.go.

Test verification (RED → GREEN)

Two tests ship with the fix:

  • TestServerErrorLoggerLevels (modules/caddyhttp/app_test.go) — unit test
    for the routing logic (both branches).
  • TestHandlerPanicLogsAtError (caddytest/integration/panic_test.go) — boots
    a real Caddy server with a test-only handler that panics, sends a request that
    hits it, and asserts the recovered panic is logged at ERROR in Caddy's
    structured logs. Booting exercises the App.Start() server-boot wiring.

RED — integration test on unmodified master (fix reverted); the panic is
logged at DEBUG, so no ERROR entry is found:

    panic_test.go: did not find an error-level "http: panic serving" entry ...
--- FAIL: TestHandlerPanicLogsAtError (6.05s)
FAIL	github.com/caddyserver/caddy/v2/caddytest/integration

GREEN — with the fix:

--- PASS: TestHandlerPanicLogsAtError (0.01s)
ok  	github.com/caddyserver/caddy/v2/caddytest/integration

Local verification

Run in golang:1.26 (and golangci/golangci-lint:v2.12.2):

  • go build ./... — OK
  • go vet ./modules/caddyhttp/ — OK
  • go test -race -short ./modules/caddyhttp/ (incl. TestServerErrorLoggerLevels) — OK
  • go test -race -short ./caddytest/integration/ — OK
  • golangci-lint run on the changed packages — 0 issues
  • Diff-coverage of changed production lines: 100% (17/17) — the boot-time
    wiring in App.Start() is covered by the integration test.

CI covers Linux/macOS/Windows; the change is platform-agnostic.

ousamabenyounes and others added 2 commits August 6, 2026 14:17
Go's net/http server writes recovered handler panics (and their stack
traces) to http.Server.ErrorLog. Caddy wired that logger at DEBUG level,
so handler panics were invisible at the default INFO log level and could
go unnoticed in production.

Route the stdlib server error log through a small writer that logs
recovered-panic messages at ERROR while keeping other low-signal stdlib
messages at DEBUG.

Covered by a unit test for the routing logic and an integration test that
boots a real server, triggers a handler panic via a test-only handler,
and asserts the panic surfaces in the structured logs at ERROR level.

Fixes caddyserver#7923

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you.

@steadytao

Copy link
Copy Markdown
Member

Damn Windows

Signed-off-by: Zen Dodd <mail@steadytao.com>
@steadytao

Copy link
Copy Markdown
Member

Will leave for another maintainers review.

@francislavoie francislavoie added this to the v2.11.5 milestone Aug 10, 2026
@francislavoie
francislavoie merged commit c290397 into caddyserver:master Aug 10, 2026
24 checks passed
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.

net/http handler panics are logged at DEBUG level

3 participants