From f8da476b54f9f4e3f3178294775d393ee13b7cc1 Mon Sep 17 00:00:00 2001 From: Liiiz <160093988+liznear@users.noreply.github.com> Date: Fri, 15 Aug 2025 00:49:18 +0800 Subject: [PATCH] fix(lsp): return a copy of lsp diagnostics to avoid data race (#681) * fix(lsp): return a copy of lsp diagnostics to avoid data race * Replace maps.Copy with maps.Clone --- internal/llm/tools/diagnostics.go | 4 +--- internal/lsp/client.go | 6 +++++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/llm/tools/diagnostics.go b/internal/llm/tools/diagnostics.go index fc9bd211735afd4d1f8a536a90d5705d88bd9790..b6773a8cf9de28b71cafca6fb45d3e2cb69d8c0a 100644 --- a/internal/llm/tools/diagnostics.go +++ b/internal/llm/tools/diagnostics.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "log/slog" - "maps" "sort" "strings" "time" @@ -109,8 +108,7 @@ func waitForLspDiagnostics(ctx context.Context, filePath string, lsps map[string diagChan := make(chan struct{}, 1) for _, client := range lsps { - originalDiags := make(map[protocol.DocumentURI][]protocol.Diagnostic) - maps.Copy(originalDiags, client.GetDiagnostics()) + originalDiags := client.GetDiagnostics() handler := func(params json.RawMessage) { lsp.HandleDiagnostics(client, params) diff --git a/internal/lsp/client.go b/internal/lsp/client.go index 279ec1feb80b79ef093fc8d1395022d4949756d7..1dbdd32f543db4b0b6b37df49de7c513de128b45 100644 --- a/internal/lsp/client.go +++ b/internal/lsp/client.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "log/slog" + "maps" "os" "os/exec" "path/filepath" @@ -765,7 +766,10 @@ func (c *Client) GetFileDiagnostics(uri protocol.DocumentURI) []protocol.Diagnos // GetDiagnostics returns all diagnostics for all files func (c *Client) GetDiagnostics() map[protocol.DocumentURI][]protocol.Diagnostic { - return c.diagnostics + c.diagnosticsMu.RLock() + defer c.diagnosticsMu.RUnlock() + + return maps.Clone(c.diagnostics) } // OpenFileOnDemand opens a file only if it's not already open