Skip to content

Commit

Permalink
cmd/govim: do not wait more than 500ms for gopls Shutdown (#1075)
Browse files Browse the repository at this point in the history
As explained in golang.org/issue/45476, all call to Shutdown does not
properly interrupt "work" that might be in progress in gopls. This has
the effect of blocking an exit from Vim.

Workaround this for now by limiting the wait time to 500ms, which is
effectively the measure of a user's patience before anger/frustration
sets in!
  • Loading branch information
myitcv committed Apr 10, 2021
1 parent 0f4445c commit f114359
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions cmd/govim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -394,14 +395,19 @@ func (g *govimplugin) Shutdown() error {
}
os.RemoveAll(g.socketDir) // see note above

// Shutdown gopls
//
// We "kill" gopls by closing its stdin. Standard practice for processes
// that communicate over stdin/stdout is to exit cleanly when stdin is
// closed.
if err := g.server.Shutdown(context.Background()); err != nil {
// TODO: remove this workaround for golang.org/issue/45476. We should not
// have to set a deadline for our call to Shutdown. 500ms is the longest a
// user should be expected to wait before "forcing" the shutdown sequence.
// Because of golang.org/issue/45476, the shorter we make this timeout the
// greater the likelihood that gopls will leave $TMPDIR artefacts lying
// around, and not have properly tidied up after itself.
ctxt, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
if err := g.server.Shutdown(ctxt); err != nil && !errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf("failed to call gopls Shutdown: %v", err)
}
// As the initiator of the connection to gopls, complete shutdown by closing
// stdin to the process we started.
if err := g.goplsStdin.Close(); err != nil {
return fmt.Errorf("failed to close gopls stdin: %v", err)
}
Expand Down

0 comments on commit f114359

Please sign in to comment.