Repository navigation
caddyhttp: log recovered handler panics at ERROR level - #7924
Merged
francislavoie merged 3 commits intoAug 10, 2026
Merged
francislavoie merged 3 commits into
francislavoie merged 3 commits into
Conversation
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
Member
|
Damn Windows |
Signed-off-by: Zen Dodd <mail@steadytao.com>
Member
|
Will leave for another maintainers review. |
francislavoie
approved these changes
Aug 10, 2026
This was referenced Sep 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/httpserver recovers a panic that escapes a handler and writes themessage and stack trace to
http.Server.ErrorLog(see the deferredrecoverin
net/http.(*conn).serve, which emits lines prefixedhttp: panic serving).In
caddyhttp,App.Start()wired thatErrorLogthroughzap.NewStdLogAt(app.logger.Named("stdlib"), zap.DebugLevel)— i.e. everystdlib server message, including recovered handler panics, was logged at
DEBUG. At the defaultINFOlevel these panics were completely invisible, soa handler panic in production would be silently swallowed.
Caddy does not recover handler panics itself in the serving path (only
reverseproxyhealth-checks/metrics userecover()), so a handler panicgenuinely reaches
net/http's serve loop and itsErrorLog.The fix
Route
http.Server.ErrorLogthrough a smallio.Writerthat inspects eachmessage: lines prefixed
http: panic servingare logged at ERROR (so theystay 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 testfor the routing logic (both branches).
TestHandlerPanicLogsAtError(caddytest/integration/panic_test.go) — bootsa 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 islogged at DEBUG, so no ERROR entry is found:
GREEN — with the fix:
Local verification
Run in
golang:1.26(andgolangci/golangci-lint:v2.12.2):go build ./...— OKgo vet ./modules/caddyhttp/— OKgo test -race -short ./modules/caddyhttp/(incl.TestServerErrorLoggerLevels) — OKgo test -race -short ./caddytest/integration/— OKgolangci-lint runon the changed packages —0 issueswiring in
App.Start()is covered by the integration test.CI covers Linux/macOS/Windows; the change is platform-agnostic.