on main help sign in
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

commitf8da476b54f9f4e3f3178294775d393ee13b7cc1 authorLiiiz <160093988+liznear@users.noreply.github.com> dateFri 15 Aug 2025, 00:49 (1 year ago) parent57df1f9
2 files changed+6−4
internal/llm/tools/diagnostics.go+1−3 internal/lsp/client.go+5−1
internal/llm/tools/diagnostics.go+1−3
@@ -5,7 +5,6 @@ import (
55 "encoding/json"
66 "fmt"
77 "log/slog"
8− "maps"
98 "sort"
109 "strings"
1110 "time"
@@ -109,8 +108,7 @@ func waitForLspDiagnostics(ctx context.Context, filePath string, lsps map[string
109108 diagChan := make(chan struct{}, 1)
110109
111110 for _, client := range lsps {
112− originalDiags := make(map[protocol.DocumentURI][]protocol.Diagnostic)
113− maps.Copy(originalDiags, client.GetDiagnostics())
111+ originalDiags := client.GetDiagnostics()
114112
115113 handler := func(params json.RawMessage) {
116114 lsp.HandleDiagnostics(client, params)
internal/lsp/client.go+5−1
@@ -7,6 +7,7 @@ import (
77 "fmt"
88 "io"
99 "log/slog"
10+ "maps"
1011 "os"
1112 "os/exec"
1213 "path/filepath"
@@ -765,7 +766,10 @@ func (c *Client) GetFileDiagnostics(uri protocol.DocumentURI) []protocol.Diagnos
765766
766767// GetDiagnostics returns all diagnostics for all files
767768func (c *Client) GetDiagnostics() map[protocol.DocumentURI][]protocol.Diagnostic {
768− return c.diagnostics
769+ c.diagnosticsMu.RLock()
770+ defer c.diagnosticsMu.RUnlock()
771+
772+ return maps.Clone(c.diagnostics)
769773}
770774
771775// OpenFileOnDemand opens a file only if it's not already open