Skip to content

Commit 60bb302

Browse files
committed
internal/lsp: fix race condition in caching
This change fixes a race condition in the metadata caching logic. Also, some minor fixes to comments and invalidation logic (it's not necessary to invalidate ASTs when a package is invalidated). Change-Id: I927bf6fbc661a86ef0ba99b29a9ed979cd1eb95d Reviewed-on: https://go-review.googlesource.com/c/tools/+/190317 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Cottrell <[email protected]>
1 parent 2adf828 commit 60bb302

File tree

3 files changed

+6
-9
lines changed

3 files changed

+6
-9
lines changed

internal/lsp/cache/load.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,13 @@ func (v *view) link(ctx context.Context, g *importGraph) error {
229229
log.Error(ctx, "not a Go file", nil, telemetry.File.Of(filename))
230230
continue
231231
}
232+
// Cache the metadata for this file.
233+
gof.mu.Lock()
232234
if gof.meta == nil {
233235
gof.meta = make(map[packageID]*metadata)
234236
}
235237
gof.meta[m.id] = m
238+
gof.mu.Unlock()
236239
}
237240

238241
// Preserve the import graph.

internal/lsp/cache/parse.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"golang.org/x/tools/internal/lsp/source"
1616
"golang.org/x/tools/internal/lsp/telemetry"
17+
"golang.org/x/tools/internal/lsp/telemetry/log"
1718
"golang.org/x/tools/internal/lsp/telemetry/trace"
1819
"golang.org/x/tools/internal/memoize"
1920
errors "golang.org/x/xerrors"
@@ -105,7 +106,7 @@ func parseGo(ctx context.Context, c *cache, fh source.FileHandle, mode source.Pa
105106

106107
ioLimit <- struct{}{}
107108
buf, _, err := fh.Read(ctx)
108-
<-ioLimit // Make sure to release the token, even when an error is returned.
109+
<-ioLimit
109110
if err != nil {
110111
return nil, err
111112
}
@@ -122,7 +123,7 @@ func parseGo(ctx context.Context, c *cache, fh source.FileHandle, mode source.Pa
122123
// Fix any badly parsed parts of the AST.
123124
tok := c.fset.File(ast.Pos())
124125
if err := fix(ctx, ast, tok, buf); err != nil {
125-
// TODO: Do something with the error (need access to a logger in here).
126+
log.Error(ctx, "failed to fix AST", err)
126127
}
127128
}
128129
if ast == nil {

internal/lsp/cache/view.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,13 +409,6 @@ func (v *view) remove(ctx context.Context, id packageID, seen map[packageID]stru
409409
files: hashParseKeys(cph.Files()),
410410
config: hashConfig(cph.Config()),
411411
})
412-
// Also, delete all of the cached ParseGoHandles.
413-
for _, ph := range cph.Files() {
414-
v.session.cache.store.Delete(parseKey{
415-
file: ph.File().Identity(),
416-
mode: ph.Mode(),
417-
})
418-
}
419412
}
420413
delete(gof.pkgs, id)
421414
gof.mu.Unlock()

0 commit comments

Comments
 (0)