Skip to content

Commit 113a081

Browse files
committed
gopls/internal/lsp/protocol: report panics in RPC goroutines
This change adds recover() + bug.Reportf to the generated dispatch routines for the server and client, so that we learn through telemetry of unexpected panics in RPC goroutines. Panics in other goroutines cannot yet be reported, but go1.23's runtime/debug.SetCrashOutput feature (golang/go#42888) will make that possible, with some extra work. Change-Id: I893333d296096632fa47e3238a72b0ac48386375 Reviewed-on: https://go-review.googlesource.com/c/tools/+/548735 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 0640701 commit 113a081

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

gopls/internal/lsp/protocol/generate/main.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func writeclient() {
101101
"context"
102102
"encoding/json"
103103
104+
"golang.org/x/tools/gopls/internal/util/bug"
104105
"golang.org/x/tools/internal/jsonrpc2"
105106
)
106107
`)
@@ -109,8 +110,15 @@ func writeclient() {
109110
out.WriteString(cdecls[k])
110111
}
111112
out.WriteString("}\n\n")
112-
out.WriteString("func clientDispatch(ctx context.Context, client Client, reply jsonrpc2.Replier, r jsonrpc2.Request) (bool, error) {\n")
113-
out.WriteString("\tswitch r.Method() {\n")
113+
out.WriteString(`func clientDispatch(ctx context.Context, client Client, reply jsonrpc2.Replier, r jsonrpc2.Request) (bool, error) {
114+
defer func() {
115+
if x := recover(); x != nil {
116+
bug.Reportf("client panic in %s request", r.Method())
117+
panic(x)
118+
}
119+
}()
120+
switch r.Method() {
121+
`)
114122
for _, k := range ccases.keys() {
115123
out.WriteString(ccases[k])
116124
}
@@ -138,6 +146,7 @@ func writeserver() {
138146
"context"
139147
"encoding/json"
140148
149+
"golang.org/x/tools/gopls/internal/util/bug"
141150
"golang.org/x/tools/internal/jsonrpc2"
142151
)
143152
`)
@@ -149,6 +158,12 @@ func writeserver() {
149158
}
150159
151160
func serverDispatch(ctx context.Context, server Server, reply jsonrpc2.Replier, r jsonrpc2.Request) (bool, error) {
161+
defer func() {
162+
if x := recover(); x != nil {
163+
bug.Reportf("server panic in %s request", r.Method())
164+
panic(x)
165+
}
166+
}()
152167
switch r.Method() {
153168
`)
154169
for _, k := range scases.keys() {

gopls/internal/lsp/protocol/tsclient.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gopls/internal/lsp/protocol/tsserver.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)