Skip to content

Commit 9fcffc5

Browse files
xpmatteojba
authored andcommitted
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 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. Updates #65723 Change-Id: I49c21f5f3e802ad7538062d824354b2e4d8a800e GitHub-Last-Rev: 35e4012 GitHub-Pull-Request: #65791 Reviewed-on: https://go-review.googlesource.com/c/go/+/565176 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> Reviewed-by: Carlos Amedee <[email protected]>
1 parent 0584574 commit 9fcffc5

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/net/http/pprof/pprof.go

+11-5
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,15 @@ 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+
prefix := ""
97+
if godebug.New("httpmuxgo121").Value() != "1" {
98+
prefix = "GET "
99+
}
100+
http.HandleFunc(prefix+"/debug/pprof/", Index)
101+
http.HandleFunc(prefix+"/debug/pprof/cmdline", Cmdline)
102+
http.HandleFunc(prefix+"/debug/pprof/profile", Profile)
103+
http.HandleFunc(prefix+"/debug/pprof/symbol", Symbol)
104+
http.HandleFunc(prefix+"/debug/pprof/trace", Trace)
99105
}
100106

101107
// Cmdline responds with the running program's

0 commit comments

Comments
 (0)