1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:34:40 -07:00

internal/lsp/cache: include session IDs in some cache keys

When caching file data specific to a session (anything with a Version or
tied to a view), we now need to be more careful about the existence of
multiple sessions.

This change fixes a few places where we appear to cache session data
without explicitly referring to the session. In principal this could
cause data corruption in multi-session gopls instances, but I have not
been able to force this to occur in either manual or automated testing.

Also fix a data race to the unsaved overlays:
https://storage.googleapis.com/go-build-log/588ee798/linux-amd64-race_d0762522.log

Updates golang/go#34111

Change-Id: I47117da1aba1afc2e211785544ad3f8b3416d15d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/222059
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rohan Challa <rohan@golang.org>
This commit is contained in:
Rob Findley 2020-03-04 13:55:41 -05:00 committed by Robert Findley
parent 6a641547f5
commit bc073721ad
3 changed files with 20 additions and 8 deletions

View File

@ -32,12 +32,14 @@ const (
) )
type modKey struct { type modKey struct {
sessionID string
cfg string cfg string
gomod string gomod string
view string view string
} }
type modTidyKey struct { type modTidyKey struct {
sessionID string
cfg string cfg string
gomod string gomod string
imports string imports string
@ -137,6 +139,7 @@ func (s *snapshot) ModHandle(ctx context.Context, fh source.FileHandle) source.M
cfg := s.Config(ctx) cfg := s.Config(ctx)
key := modKey{ key := modKey{
sessionID: s.view.session.id,
cfg: hashConfig(cfg), cfg: hashConfig(cfg),
gomod: fh.Identity().String(), gomod: fh.Identity().String(),
view: folder, view: folder,
@ -295,10 +298,14 @@ func (s *snapshot) ModTidyHandle(ctx context.Context, realfh source.FileHandle)
if err != nil { if err != nil {
return nil, err return nil, err
} }
s.mu.Lock()
overlayHash := hashUnsavedOverlays(s.files)
s.mu.Unlock()
key := modTidyKey{ key := modTidyKey{
sessionID: s.view.session.id,
view: folder, view: folder,
imports: imports, imports: imports,
unsavedOverlays: hashUnsavedOverlays(s.files), unsavedOverlays: overlayHash,
gomod: realfh.Identity().Identifier, gomod: realfh.Identity().Identifier,
cfg: hashConfig(cfg), cfg: hashConfig(cfg),
} }

View File

@ -54,6 +54,7 @@ func (o *overlay) Identity() source.FileIdentity {
return source.FileIdentity{ return source.FileIdentity{
URI: o.uri, URI: o.uri,
Identifier: o.hash, Identifier: o.hash,
SessionID: o.session.id,
Version: o.version, Version: o.version,
Kind: o.kind, Kind: o.kind,
} }

View File

@ -327,7 +327,11 @@ type FileHandle interface {
type FileIdentity struct { type FileIdentity struct {
URI span.URI URI span.URI
// Version is the version of the file, as specified by the client. // SessionID is the ID of the LSP session.
SessionID string
// Version is the version of the file, as specified by the client. It should
// only be set in combination with SessionID.
Version float64 Version float64
// Identifier represents a unique identifier for the file. // Identifier represents a unique identifier for the file.