From faf83c64e9269d0b25a74424705e3606824fcbe6 Mon Sep 17 00:00:00 2001 From: Ian Cottrell Date: Fri, 17 May 2019 15:18:39 -0400 Subject: [PATCH] internal/lsp: fix race in delivering diagnostics to the command line client Fixes golang/go#32091 Change-Id: I1399a596169384f48d9f2409988226708dcd3473 Reviewed-on: https://go-review.googlesource.com/c/tools/+/177937 Run-TryBot: Ian Cottrell TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- internal/lsp/cmd/check.go | 2 ++ internal/lsp/cmd/cmd.go | 3 +++ 2 files changed, 5 insertions(+) diff --git a/internal/lsp/cmd/check.go b/internal/lsp/cmd/check.go index 59f0599a..c5d9960b 100644 --- a/internal/lsp/cmd/check.go +++ b/internal/lsp/cmd/check.go @@ -62,6 +62,8 @@ func (c *check) Run(ctx context.Context, args ...string) error { case <-time.Tick(30 * time.Second): return fmt.Errorf("timed out waiting for results from %v", file.uri) } + file.diagnosticsMu.Lock() + defer file.diagnosticsMu.Unlock() for _, d := range file.diagnostics { spn, err := file.mapper.RangeSpan(d.Range) if err != nil { diff --git a/internal/lsp/cmd/cmd.go b/internal/lsp/cmd/cmd.go index 9b676c9e..6c9ab2d8 100644 --- a/internal/lsp/cmd/cmd.go +++ b/internal/lsp/cmd/cmd.go @@ -220,6 +220,7 @@ type cmdFile struct { err error added bool hasDiagnostics chan struct{} + diagnosticsMu sync.Mutex diagnostics []protocol.Diagnostic } @@ -306,6 +307,8 @@ func (c *cmdClient) PublishDiagnostics(ctx context.Context, p *protocol.PublishD defer c.filesMu.Unlock() uri := span.URI(p.URI) file := c.getFile(ctx, uri) + file.diagnosticsMu.Lock() + defer file.diagnosticsMu.Unlock() hadDiagnostics := file.diagnostics != nil file.diagnostics = p.Diagnostics if file.diagnostics == nil {