Skip to content

Commit 4cc26a0

Browse files
heschistamblerre
authored andcommitted
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 <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> (cherry picked from commit cf5ba95) Reviewed-on: https://go-review.googlesource.com/c/tools/+/217678 Reviewed-by: Robert Findley <[email protected]>
1 parent 0eded08 commit 4cc26a0

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

internal/imports/mod_cache.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,29 @@ func (d *dirInfoCache) ScanAndListen(ctx context.Context, listener cacheListener
132132
}
133133
d.mu.Unlock()
134134

135+
stop := func() {
136+
cancel()
137+
d.mu.Lock()
138+
delete(d.listeners, cookie)
139+
d.mu.Unlock()
140+
for i := 0; i < maxInFlight; i++ {
141+
<-sema
142+
}
143+
}
144+
135145
// Process the pre-existing keys.
136146
for _, k := range keys {
137147
select {
138148
case <-ctx.Done():
139-
cancel()
140-
return func() {}
149+
return stop
141150
default:
142151
}
143152
if v, ok := d.Load(k); ok {
144153
listener(v)
145154
}
146155
}
147156

148-
return func() {
149-
cancel()
150-
d.mu.Lock()
151-
delete(d.listeners, cookie)
152-
d.mu.Unlock()
153-
for i := 0; i < maxInFlight; i++ {
154-
<-sema
155-
}
156-
}
157+
return stop
157158
}
158159

159160
// Store stores the package info for dir.

0 commit comments

Comments
 (0)