Skip to content

Ensure LSP readLoop doesn't block shutdown #1074

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
merged 2 commits into from
Jun 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,20 @@ func (s *Server) Run() error {
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error { return s.dispatchLoop(ctx) })
g.Go(func() error { return s.writeLoop(ctx) })
g.Go(func() error { return s.readLoop(ctx) })

if err := g.Wait(); err != nil && !errors.Is(err, io.EOF) {
// Don't run readLoop in the group, as it blocks on stdin read and cannot be cancelled.
readLoopErr := make(chan error, 1)
g.Go(func() error {
select {
case <-ctx.Done():
return ctx.Err()
case err := <-readLoopErr:
return err
}
})
go func() { readLoopErr <- s.readLoop(ctx) }()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goroutine can leak if the reader never exits, but I'm trying to handle the case where the process is exiting due to a cancellation cancel via a signal, so, it's hard to do better here without cancelable stdin reads ☹️


if err := g.Wait(); err != nil && !errors.Is(err, io.EOF) && ctx.Err() != nil {
return err
}
return nil
Expand Down