Skip to content

Commit 3af9097

Browse files
authored
Ensure LSP readLoop doesn't block shutdown (#1074)
1 parent e68fa65 commit 3af9097

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

internal/lsp/server.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,20 @@ func (s *Server) Run() error {
194194
g, ctx := errgroup.WithContext(ctx)
195195
g.Go(func() error { return s.dispatchLoop(ctx) })
196196
g.Go(func() error { return s.writeLoop(ctx) })
197-
g.Go(func() error { return s.readLoop(ctx) })
198197

199-
if err := g.Wait(); err != nil && !errors.Is(err, io.EOF) {
198+
// Don't run readLoop in the group, as it blocks on stdin read and cannot be cancelled.
199+
readLoopErr := make(chan error, 1)
200+
g.Go(func() error {
201+
select {
202+
case <-ctx.Done():
203+
return ctx.Err()
204+
case err := <-readLoopErr:
205+
return err
206+
}
207+
})
208+
go func() { readLoopErr <- s.readLoop(ctx) }()
209+
210+
if err := g.Wait(); err != nil && !errors.Is(err, io.EOF) && ctx.Err() != nil {
200211
return err
201212
}
202213
return nil

0 commit comments

Comments
 (0)