Skip to content

Commit 7dc6b39

Browse files
muirdmstamblerre
authored andcommitted
internal/lsp: use memoize store's context when type checking
The memoize store passes a detached context to the value getter function. This is important since if the value getter experiences a context cancellation it will end up caching context.Canceled, which you never want. When type checking, we were ignoring the detached context and using the "real" request context. This would cause the context.Canceled error to get cached and continue popping up in various situations. Fix by swapping the importer's context to the detached context. It is a little messy since the importer stores the context as a field. I added a defer to restore the original context since it doesn't seem correct to let the detached context escape the memoize function. Updates #33678 Change-Id: I20dd466b0072ac2e856adbe993364f77e93ab054 Reviewed-on: https://go-review.googlesource.com/c/tools/+/192719 Reviewed-by: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]>
1 parent be0da05 commit 7dc6b39

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

internal/lsp/cache/check.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,15 @@ func (imp *importer) checkPackageHandle(m *metadata) (*checkPackageHandle, error
9494
imports: make(map[packagePath]*checkPackageHandle),
9595
}
9696
h := imp.view.session.cache.store.Bind(key, func(ctx context.Context) interface{} {
97+
origCtx := imp.ctx
98+
defer func() { imp.ctx = origCtx }()
99+
100+
// We must use the store's detached context to avoid poisoning the
101+
// cache with context.Canceled if the request is cancelled.
102+
imp.ctx = ctx
103+
97104
data := &checkPackageData{}
98-
data.pkg, data.err = func() (*pkg, error) {
99-
return imp.typeCheck(cph, m)
100-
}()
105+
data.pkg, data.err = imp.typeCheck(cph, m)
101106
return data
102107
})
103108
cph.handle = h

0 commit comments

Comments
 (0)