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

internal/imports: stop leaking listeners

(*dirInfoCache).ScanAndListen saves a listener when it's called. That
listener is a chain of callbacks that leads all the way back up to the
original caller, i.e. the gopls completion code. It needs to unregister
that listener, but in cases where the context was cancelled before it
finished its initial walk of the cache, it failed to. In gopls' case,
that means retaining the entire state of the workspace as of completion
triggering.

Return a real stop function on all paths.

Change-Id: Iff3046e627b1fa89f576371c4742fee6fdca7589
Reviewed-on: https://go-review.googlesource.com/c/tools/+/217677
Run-TryBot: Heschi Kreinick <heschi@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Heschi Kreinick 2020-02-04 00:32:14 -05:00
parent 35ac94b00d
commit cf5ba95194

View File

@ -132,20 +132,7 @@ func (d *dirInfoCache) ScanAndListen(ctx context.Context, listener cacheListener
}
d.mu.Unlock()
// Process the pre-existing keys.
for _, k := range keys {
select {
case <-ctx.Done():
cancel()
return func() {}
default:
}
if v, ok := d.Load(k); ok {
listener(v)
}
}
return func() {
stop := func() {
cancel()
d.mu.Lock()
delete(d.listeners, cookie)
@ -154,6 +141,20 @@ func (d *dirInfoCache) ScanAndListen(ctx context.Context, listener cacheListener
<-sema
}
}
// Process the pre-existing keys.
for _, k := range keys {
select {
case <-ctx.Done():
return stop
default:
}
if v, ok := d.Load(k); ok {
listener(v)
}
}
return stop
}
// Store stores the package info for dir.