Skip to content

Commit 8a0c6e2

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/server: report HTTP panics via telemetry
Change-Id: Ifcb5cdbcde92d73fd5d626b8d70ed47200745633 Reviewed-on: https://go-review.googlesource.com/c/tools/+/576682 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent c7b6b8d commit 8a0c6e2

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

gopls/internal/server/server.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"golang.org/x/tools/gopls/internal/progress"
2929
"golang.org/x/tools/gopls/internal/protocol"
3030
"golang.org/x/tools/gopls/internal/settings"
31+
"golang.org/x/tools/gopls/internal/util/bug"
3132
"golang.org/x/tools/internal/event"
3233
)
3334

@@ -263,7 +264,7 @@ func (s *server) initWeb() (*web, error) {
263264

264265
secret := "/gopls/" + base64.RawURLEncoding.EncodeToString(token)
265266
webMux := http.NewServeMux()
266-
rootMux.Handle(secret+"/", http.StripPrefix(secret, webMux))
267+
rootMux.Handle(secret+"/", withPanicHandler(http.StripPrefix(secret, webMux)))
267268

268269
webServer := &http.Server{Addr: listener.Addr().String(), Handler: rootMux}
269270
go func() {
@@ -396,3 +397,19 @@ func (w *web) url(path, query, fragment string) protocol.URI {
396397
url2.Fragment = fragment
397398
return protocol.URI(url2.String())
398399
}
400+
401+
// withPanicHandler wraps an HTTP handler with telemetry-reporting of
402+
// panics that would otherwise be silently recovered by the net/http
403+
// root handler.
404+
func withPanicHandler(h http.Handler) http.HandlerFunc {
405+
return func(w http.ResponseWriter, req *http.Request) {
406+
panicked := true
407+
defer func() {
408+
if panicked {
409+
bug.Report("panic in HTTP handler")
410+
}
411+
}()
412+
h.ServeHTTP(w, req)
413+
panicked = false
414+
}
415+
}

0 commit comments

Comments
 (0)