Skip to content

Commit 915654e

Browse files
committed
trace: tighten the check for duplicate registration
From comment at @santsai at golang/go#24137 (comment) Change-Id: Icf0aff2172811752b240d94e709550b0da353360 Reviewed-on: https://go-review.googlesource.com/c/157378 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: JBD <[email protected]>
1 parent be1c187 commit 915654e

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

trace/trace.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ import (
8686
// FOR DEBUGGING ONLY. This will slow down the program.
8787
var DebugUseAfterFinish = false
8888

89+
// HTTP ServeMux paths.
90+
const (
91+
debugRequestsPath = "/debug/requests"
92+
debugEventsPath = "/debug/events"
93+
)
94+
8995
// AuthRequest determines whether a specific request is permitted to load the
9096
// /debug/requests or /debug/events pages.
9197
//
@@ -112,17 +118,17 @@ var AuthRequest = func(req *http.Request) (any, sensitive bool) {
112118
}
113119

114120
func init() {
115-
_, pat := http.DefaultServeMux.Handler(&http.Request{URL: &url.URL{Path: "/debug/requests"}})
116-
if pat != "" {
121+
_, pat := http.DefaultServeMux.Handler(&http.Request{URL: &url.URL{Path: debugRequestsPath}})
122+
if pat == debugRequestsPath {
117123
panic("/debug/requests is already registered. You may have two independent copies of " +
118124
"golang.org/x/net/trace in your binary, trying to maintain separate state. This may " +
119125
"involve a vendored copy of golang.org/x/net/trace.")
120126
}
121127

122128
// TODO(jbd): Serve Traces from /debug/traces in the future?
123129
// There is no requirement for a request to be present to have traces.
124-
http.HandleFunc("/debug/requests", Traces)
125-
http.HandleFunc("/debug/events", Events)
130+
http.HandleFunc(debugRequestsPath, Traces)
131+
http.HandleFunc(debugEventsPath, Events)
126132
}
127133

128134
// NewContext returns a copy of the parent context

0 commit comments

Comments
 (0)