Skip to content

Commit

Permalink
gopls/internal/lsp/cache: fail workspace load on context cancellation
Browse files Browse the repository at this point in the history
If the context was cancelled early during initialization, it was
possible that all module load scopes get skipped, because ParseMod
returns the context error. As a result, the subsequent load would
succeed trivially, even though the context was cancelled.

After analyzing the reinitialization codepath, it seems very plausible
to encounter this race, and it explains the flakiness of
TestReinitializeRepeatedly -- I have found nothing else that would
explain the failure mode observed in logs, that the reload bypasses
initialization.

Fix this by returning when context errors are encountered.

Fixes golang/go#57780

Change-Id: I3fb971503f280131c59146bc586da45dd2ed1126
Reviewed-on: https://go-review.googlesource.com/c/tools/+/495058
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
  • Loading branch information
findleyr committed May 16, 2023
1 parent e5c8d4d commit a059382
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions gopls/internal/lsp/cache/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,16 +752,18 @@ func (s *snapshot) loadWorkspace(ctx context.Context, firstAttempt bool) (loadEr
// errors.
fh, err := s.ReadFile(ctx, modURI)
if err != nil {
if ctx.Err() == nil {
addError(modURI, err)
if ctx.Err() != nil {
return ctx.Err()
}
addError(modURI, err)
continue
}
parsed, err := s.ParseMod(ctx, fh)
if err != nil {
if ctx.Err() == nil {
addError(modURI, err)
if ctx.Err() != nil {
return ctx.Err()
}
addError(modURI, err)
continue
}
if parsed.File == nil || parsed.File.Module == nil {
Expand Down

0 comments on commit a059382

Please sign in to comment.