From 145a1e401f50941cd7641e9570ec299e54a97d9d Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Thu, 19 Dec 2019 15:47:07 -0500 Subject: [PATCH] internal/lsp/cache: detach context before invalidation In one of my previous refactoring changes, I lost the fact that the context should be detached before invalidating a file's contents. If this function is canceled, we will be in a bad state. Also, small change to return ctx.Err() instead of a custom error message from (*packageHandle).check. Change-Id: I19e513e09e438feee105fdd89cb7364a0c3c5e7f Reviewed-on: https://go-review.googlesource.com/c/tools/+/212104 Run-TryBot: Rebecca Stambler Reviewed-by: Heschi Kreinick --- internal/lsp/cache/check.go | 2 +- internal/lsp/cache/view.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/lsp/cache/check.go b/internal/lsp/cache/check.go index 24136170c6..2298fab4b0 100644 --- a/internal/lsp/cache/check.go +++ b/internal/lsp/cache/check.go @@ -184,7 +184,7 @@ func (ph *packageHandle) Check(ctx context.Context) (source.Package, error) { func (ph *packageHandle) check(ctx context.Context) (*pkg, error) { v := ph.handle.Get(ctx) if v == nil { - return nil, errors.Errorf("no package for %s", ph.m.id) + return nil, ctx.Err() } data := v.(*packageData) return data.pkg, data.err diff --git a/internal/lsp/cache/view.go b/internal/lsp/cache/view.go index 8825e63007..b9cd3007d2 100644 --- a/internal/lsp/cache/view.go +++ b/internal/lsp/cache/view.go @@ -26,6 +26,7 @@ import ( "golang.org/x/tools/internal/span" "golang.org/x/tools/internal/telemetry/log" "golang.org/x/tools/internal/telemetry/tag" + "golang.org/x/tools/internal/xcontext" errors "golang.org/x/xerrors" ) @@ -349,6 +350,9 @@ func (v *view) getSnapshot() *snapshot { // including any position and type information that depends on it. // It returns true if we were already tracking the given file, false otherwise. func (v *view) invalidateContent(ctx context.Context, uri span.URI, kind source.FileKind, action source.FileAction) source.Snapshot { + // Detach the context so that content invalidation cannot be canceled. + ctx = xcontext.Detach(ctx) + // Cancel all still-running previous requests, since they would be // operating on stale data. switch action {