Skip to content

Commit 63ea654

Browse files
committed
internal/lsp: hold the gc details lock when storing diagnostics
At long last, with Pontus's help reproing on Github actions, we have tracked down the race to the gc_details diagnostics. Since toggling gc_details does not increment the snapshot ID, we have to use careful locking to ensure that the gc_details diagnostics we store are consistent with the current state of the gc_details toggle. Updates golang/go#44099 Fixes golang/go#44826 Change-Id: I7b9108a829c98a84360c9012c1b60f4990839b5a Reviewed-on: https://go-review.googlesource.com/c/tools/+/304169 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 7f6d50e commit 63ea654

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

internal/lsp/diagnostics.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,25 @@ func (s *Server) diagnosePkg(ctx context.Context, snapshot source.Snapshot, pkg
285285
if err != nil {
286286
event.Error(ctx, "warning: gc details", err, tag.Snapshot.Of(snapshot.ID()), tag.Package.Of(pkg.ID()))
287287
}
288-
for id, diags := range gcReports {
289-
fh := snapshot.FindFile(id.URI)
290-
// Don't publish gc details for unsaved buffers, since the underlying
291-
// logic operates on the file on disk.
292-
if fh == nil || !fh.Saved() {
293-
continue
288+
s.gcOptimizationDetailsMu.Lock()
289+
_, enableGCDetails := s.gcOptimizationDetails[pkg.ID()]
290+
291+
// NOTE(golang/go#44826): hold the gcOptimizationDetails lock, and re-check
292+
// whether gc optimization details are enabled, while storing gc_details
293+
// results. This ensures that the toggling of GC details and clearing of
294+
// diagnostics does not race with storing the results here.
295+
if enableGCDetails {
296+
for id, diags := range gcReports {
297+
fh := snapshot.FindFile(id.URI)
298+
// Don't publish gc details for unsaved buffers, since the underlying
299+
// logic operates on the file on disk.
300+
if fh == nil || !fh.Saved() {
301+
continue
302+
}
303+
s.storeDiagnostics(snapshot, id.URI, gcDetailsSource, diags)
294304
}
295-
s.storeDiagnostics(snapshot, id.URI, gcDetailsSource, diags)
296305
}
306+
s.gcOptimizationDetailsMu.Unlock()
297307
}
298308
}
299309

0 commit comments

Comments
 (0)