Skip to content

Commit

Permalink
gopls/internal/lsp/protocol: report panics in RPC goroutines
Browse files Browse the repository at this point in the history
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
adonovan committed Dec 11, 2023
1 parent 0640701 commit 113a081
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
19 changes: 17 additions & 2 deletions gopls/internal/lsp/protocol/generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func writeclient() {
"context"
"encoding/json"
"golang.org/x/tools/gopls/internal/util/bug"
"golang.org/x/tools/internal/jsonrpc2"
)
`)
Expand All @@ -109,8 +110,15 @@ func writeclient() {
out.WriteString(cdecls[k])
}
out.WriteString("}\n\n")
out.WriteString("func clientDispatch(ctx context.Context, client Client, reply jsonrpc2.Replier, r jsonrpc2.Request) (bool, error) {\n")
out.WriteString("\tswitch r.Method() {\n")
out.WriteString(`func clientDispatch(ctx context.Context, client Client, reply jsonrpc2.Replier, r jsonrpc2.Request) (bool, error) {
defer func() {
if x := recover(); x != nil {
bug.Reportf("client panic in %s request", r.Method())
panic(x)
}
}()
switch r.Method() {
`)
for _, k := range ccases.keys() {
out.WriteString(ccases[k])
}
Expand Down Expand Up @@ -138,6 +146,7 @@ func writeserver() {
"context"
"encoding/json"
"golang.org/x/tools/gopls/internal/util/bug"
"golang.org/x/tools/internal/jsonrpc2"
)
`)
Expand All @@ -149,6 +158,12 @@ func writeserver() {
}
func serverDispatch(ctx context.Context, server Server, reply jsonrpc2.Replier, r jsonrpc2.Request) (bool, error) {
defer func() {
if x := recover(); x != nil {
bug.Reportf("server panic in %s request", r.Method())
panic(x)
}
}()
switch r.Method() {
`)
for _, k := range scases.keys() {
Expand Down
7 changes: 7 additions & 0 deletions gopls/internal/lsp/protocol/tsclient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions gopls/internal/lsp/protocol/tsserver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 113a081

Please sign in to comment.