Skip to content

Commit 625332f

Browse files
committed
internal/lsp/protocol: send responses for cancelled requests
LSP https://microsoft.github.io/language-server-protocol/specifications/specification-current/#cancelRequest expects the server to send back the response even when the request is cancelled. Gopls LSP protocol implements the cancellation using the context cancellation. That is, upon a cancellation request from the client, the server calls the corresponding canceller that cancels the context passed to the handler. Reusing this cancelled context for the replyer is not safe because code in any layer can decide to shortcircuit and skip sending the data back to the client. E.g. https://cs.opensource.google/go/tools/+/master:internal/jsonrpc2/stream.go;l=63 This CL make sure to pass the detached context to the replier. Alternative, or a better approach to avoid any unexpected side-effect of using a detached context is to send out the response at the point of cancellation with a separate context. But that requires more significant code change. Testing is currently hard, but maybe doable once the current refactoring is done. Test is left as a TODO. Change-Id: I4611af00ad913e96b62c6b7180c6673b0465daf9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/232300 Reviewed-by: Ian Cottrell <[email protected]>
1 parent a1532b8 commit 625332f

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

internal/lsp/protocol/protocol.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,29 @@ func ServerDispatcher(conn *jsonrpc2.Conn) Server {
3333

3434
func Handlers(handler jsonrpc2.Handler) jsonrpc2.Handler {
3535
return CancelHandler(
36-
CancelHandler(
37-
jsonrpc2.AsyncHandler(
38-
jsonrpc2.MustReplyHandler(handler))))
36+
jsonrpc2.AsyncHandler(
37+
jsonrpc2.MustReplyHandler(handler)))
3938
}
4039

4140
func CancelHandler(handler jsonrpc2.Handler) jsonrpc2.Handler {
4241
handler, canceller := jsonrpc2.CancelHandler(handler)
4342
return func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
4443
if req.Method() != "$/cancelRequest" {
45-
return handler(ctx, reply, req)
44+
// TODO(iancottrell): See if we can generate a reply for the request to be cancelled
45+
// at the point of cancellation rather than waiting for gopls to naturally reply.
46+
// To do that, we need to keep track of whether a reply has been sent already and
47+
// be careful about racing between the two paths.
48+
// TODO(iancottrell): Add a test that watches the stream and verifies the response
49+
// for the cancelled request flows.
50+
replyWithDetachedContext := func(ctx context.Context, resp interface{}, err error) error {
51+
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#cancelRequest
52+
if ctx.Err() != nil && err == nil {
53+
err = RequestCancelledError
54+
}
55+
ctx = xcontext.Detach(ctx)
56+
return reply(ctx, resp, err)
57+
}
58+
return handler(ctx, replyWithDetachedContext, req)
4659
}
4760
var params CancelParams
4861
if err := json.Unmarshal(req.Params(), &params); err != nil {

0 commit comments

Comments
 (0)