-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Keep LSP server alive after response marshal failures #4896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
John Favret (johnfav03)
merged 7 commits into
microsoft:main
from
johnfav03:fix-selrange-marshal-depth
Aug 13, 2026
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2959209
remove fatality from response marshal failures
johnfav03 759088a
unexport messagemarshalerror
johnfav03 bf3742e
cap selectionranges at 1000 for json serializing
johnfav03 d330694
moved test into server_shutdown_test.go
johnfav03 db9f170
switched range limiting from post pass to depth counter
johnfav03 5bf55d0
renamed server_shutdown_test to server_test
johnfav03 515bb44
extracted selectionrangebuilder type
johnfav03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package lsp | ||
|
johnfav03 marked this conversation as resolved.
Outdated
|
||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/microsoft/typescript-go/internal/jsonrpc" | ||
| "github.com/microsoft/typescript-go/internal/lsp/lsproto" | ||
| ) | ||
|
|
||
| type eofReader struct{} | ||
|
|
||
| func (eofReader) Read() (*lsproto.Message, error) { return nil, io.EOF } | ||
|
|
||
| // A response that exceeds the JSON encoder's nesting limit must fail only its | ||
| // request. The write loop must remain available to deliver subsequent responses. | ||
| func TestWriteLoopRecoversFromUnserializableResponse(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| pr, pw := io.Pipe() | ||
| server := NewServer(&ServerOptions{ | ||
| In: eofReader{}, | ||
| Out: ToWriter(pw), | ||
| Err: io.Discard, | ||
| Cwd: "/test", | ||
| }) | ||
|
|
||
| ctx, cancel := context.WithCancel(t.Context()) | ||
| defer cancel() | ||
| server.backgroundCtx = ctx | ||
|
|
||
| writeLoopErr := make(chan error, 1) | ||
| go func() { writeLoopErr <- server.writeLoop(ctx) }() | ||
|
|
||
| // A selection range whose parent chain is far deeper than the JSON encoder's nesting limit. | ||
| var deep *lsproto.SelectionRange | ||
| for range 20000 { | ||
| deep = &lsproto.SelectionRange{Parent: deep} | ||
| } | ||
| badResult := []*lsproto.SelectionRange{deep} | ||
| badID := jsonrpc.NewIDString("bad") | ||
| if err := server.send((&lsproto.ResponseMessage{ID: badID, Result: &badResult}).Message()); err != nil { | ||
| t.Fatalf("failed to enqueue bad response: %v", err) | ||
| } | ||
|
|
||
| // A subsequent well-formed response must still be delivered. | ||
| goodID := jsonrpc.NewIDString("good") | ||
| if err := server.send((&lsproto.ResponseMessage{ID: goodID, Result: &lsproto.SelectionRangesOrNull{}}).Message()); err != nil { | ||
| t.Fatalf("failed to enqueue good response: %v", err) | ||
| } | ||
|
|
||
| reader := lsproto.NewBaseReader(pr) | ||
| sawError := false | ||
| sawGood := false | ||
| for range 2 { | ||
| msg := readMessageWithTimeout(t, reader) | ||
| resp := msg.AsResponse() | ||
| switch { | ||
| case resp.ID != nil && *resp.ID == *badID: | ||
| if resp.Error == nil { | ||
| t.Errorf("expected an error response for the unmarshalable request, got a result") | ||
| } | ||
| sawError = true | ||
| case resp.ID != nil && *resp.ID == *goodID: | ||
| if resp.Error != nil { | ||
| t.Errorf("expected a successful response for the good request, got error: %v", resp.Error) | ||
| } | ||
| sawGood = true | ||
| default: | ||
| t.Errorf("unexpected response id: %v", resp.ID) | ||
| } | ||
| } | ||
|
|
||
| if !sawError { | ||
| t.Errorf("did not receive an error response for the unmarshalable request") | ||
| } | ||
| if !sawGood { | ||
| t.Errorf("did not receive the subsequent well-formed response (write loop likely died)") | ||
| } | ||
|
|
||
| // The write loop must still be running. | ||
| select { | ||
| case err := <-writeLoopErr: | ||
| t.Fatalf("write loop exited unexpectedly: %v", err) | ||
| default: | ||
| return | ||
| } | ||
| } | ||
|
|
||
| func readMessageWithTimeout(t *testing.T, reader *lsproto.BaseReader) *lsproto.Message { | ||
| t.Helper() | ||
| type result struct { | ||
| msg *lsproto.Message | ||
| err error | ||
| } | ||
| ch := make(chan result, 1) | ||
| go func() { | ||
| data, err := reader.Read() | ||
| if err != nil { | ||
| ch <- result{err: err} | ||
| return | ||
| } | ||
| msg := &lsproto.Message{} | ||
| ch <- result{msg: msg, err: msg.UnmarshalJSON(data)} | ||
| }() | ||
| select { | ||
| case r := <-ch: | ||
| if r.err != nil { | ||
| t.Fatalf("failed to read message: %v", r.err) | ||
| } | ||
| return r.msg | ||
| case <-time.After(2 * time.Second): | ||
| t.Fatal("timed out waiting for a message (write loop may have died)") | ||
| return nil | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.