Skip to content

metrics: Add nil check for metricsHandler in AdminMetrics.serveHTTP#7553

Open
Br1an67 wants to merge 2 commits intocaddyserver:masterfrom
Br1an67:fix/issue-7079-fix-7079
Open

metrics: Add nil check for metricsHandler in AdminMetrics.serveHTTP#7553
Br1an67 wants to merge 2 commits intocaddyserver:masterfrom
Br1an67:fix/issue-7079-fix-7079

Conversation

@Br1an67
Copy link

@Br1an67 Br1an67 commented Mar 7, 2026

Fixes #7079

Summary

This PR fixes a panic that occurs in the admin metrics endpoint when the AdminMetrics module's serveHTTP method is called before the module is fully provisioned. The panic was causing a crash loop when invalid TLS configurations were POST-ed via the admin API.

Root Cause

The serveHTTP method called m.metricsHandler.ServeHTTP(w, r) without checking if metricsHandler was nil. This field is only set during the Provision phase, but routes are registered before provisioning completes. If a request hits the endpoint before provisioning finishes (or if provisioning fails), a nil pointer dereference causes a panic.

Changes

  • Added a nil check for m.metricsHandler in the serveHTTP method
  • Returns a proper caddy.APIError with HTTP status 500 instead of panicking
 modules/metrics/adminmetrics.go | 6 ++++++
 1 file changed, 6 insertions(+)

Testing

  • Verified the fix compiles successfully with go build ./...
  • Ran the metrics module tests: all pass
  • Ran admin-related tests: all pass

Assistance Disclosure

The fix was authored with the assistance of Claude. The solution was verified to be correct through build and test execution.

Prevents panic when the admin metrics endpoint is accessed before
the module is fully provisioned. Returns a proper API error instead
of crashing.
@CLAassistant
Copy link

CLAassistant commented Mar 7, 2026

CLA assistant check
All committers have signed the CLA.

@mholt
Copy link
Member

mholt commented Mar 9, 2026

Should we be provisioning metricsHandler earlier?

Also can you sign the CLA? Thanks!

Instead of adding a nil check for metricsHandler, address the root
cause by provisioning admin router modules before calling Routes().
This ensures all handler state is initialized before routes are
registered on the mux.

Merge newAdminHandler and provisionAdminRouters into a single step,
removing the two-phase setup where routes were registered first and
modules provisioned later. The AdminConfig.routers field is no longer
needed since provisioning happens inline.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

Panic and crash loop after POST-ing TLS config via admin API

3 participants