Skip to content

Commit 2322758

Browse files
committed
expvar: avoid conflict 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/vars" declared in the expvar package. You get an error such as: panic: pattern "GET /" (registered at ...) conflicts with pattern "/debug/vars" (registered at ...expvar.go:384): GET / matches fewer methods than /debug/vars, but has a more general path pattern This patch prevents that error. Adding GET is correct because no other method makes sense with /debug/vars. However, a tool using any other methods will break. We preserve the traditional behaviour when GODEBUG=httpmuxgo121=1 is specified. Fixes #65723
1 parent b634f6f commit 2322758

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/expvar/expvar.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
// Package expvar provides a standardized interface to public variables, such
66
// as operation counters in servers. It exposes these variables via HTTP at
7-
// /debug/vars in JSON format.
7+
// /debug/vars in JSON format. As of Go 1.22, the /debug/vars request must
8+
// use GET.
89
//
910
// Operations to set or modify these public variables are atomic.
1011
//
@@ -23,6 +24,7 @@ package expvar
2324

2425
import (
2526
"encoding/json"
27+
"internal/godebug"
2628
"log"
2729
"math"
2830
"net/http"
@@ -381,7 +383,11 @@ func memstats() any {
381383
}
382384

383385
func init() {
384-
http.HandleFunc("/debug/vars", expvarHandler)
386+
if godebug.New("httpmuxgo121").Value() == "1" {
387+
http.HandleFunc("/debug/vars", expvarHandler)
388+
} else {
389+
http.HandleFunc("GET /debug/vars", expvarHandler)
390+
}
385391
Publish("cmdline", Func(cmdline))
386392
Publish("memstats", Func(memstats))
387393
}

0 commit comments

Comments
 (0)