Skip to content

Commit afc49af

Browse files
committed
net/http/pprof: avoid panic with user-defined "GET /" route
With the new routing style in go 1.22, declaring http.Handle("GET /", h) generates a conflict with route "/debug/pprof/" and the others declared in the net/http/pprof package. You get an error such as: panic: pattern "GET /" (registered at .../pprof.go:94): GET / matches fewer methods than /debug/pprof/, but has a more general path pattern [recovered] panic: pattern "GET /" (registered at ...:17) conflicts with pattern "/debug/pprof/" (registered at .../pprof.go:94): This patch prevents that error. Adding GET is correct because no other method makes sense with the /debug/pprof routes. However, a tool using any method other than GET will break. We preserve the traditional behaviour when GODEBUG=httpmuxgo121=1 is specified. This is related to issue #65723
1 parent b634f6f commit afc49af

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/net/http/pprof/pprof.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// The package is typically only imported for the side effect of
99
// registering its HTTP handlers.
1010
// The handled paths all begin with /debug/pprof/.
11+
// As of Go 1.22, all the paths must be requested with GET.
1112
//
1213
// To use pprof, link this package into your program:
1314
//
@@ -75,6 +76,7 @@ import (
7576
"context"
7677
"fmt"
7778
"html"
79+
"internal/godebug"
7880
"internal/profile"
7981
"io"
8082
"log"
@@ -91,11 +93,19 @@ import (
9193
)
9294

9395
func init() {
94-
http.HandleFunc("/debug/pprof/", Index)
95-
http.HandleFunc("/debug/pprof/cmdline", Cmdline)
96-
http.HandleFunc("/debug/pprof/profile", Profile)
97-
http.HandleFunc("/debug/pprof/symbol", Symbol)
98-
http.HandleFunc("/debug/pprof/trace", Trace)
96+
if godebug.New("httpmuxgo121").Value() == "1" {
97+
http.HandleFunc("/debug/pprof/", Index)
98+
http.HandleFunc("/debug/pprof/cmdline", Cmdline)
99+
http.HandleFunc("/debug/pprof/profile", Profile)
100+
http.HandleFunc("/debug/pprof/symbol", Symbol)
101+
http.HandleFunc("/debug/pprof/trace", Trace)
102+
} else {
103+
http.HandleFunc("GET /debug/pprof/", Index)
104+
http.HandleFunc("GET /debug/pprof/cmdline", Cmdline)
105+
http.HandleFunc("GET /debug/pprof/profile", Profile)
106+
http.HandleFunc("GET /debug/pprof/symbol", Symbol)
107+
http.HandleFunc("GET /debug/pprof/trace", Trace)
108+
}
99109
}
100110

101111
// Cmdline responds with the running program's

0 commit comments

Comments
 (0)