Skip to content

Commit ba10413

Browse files
Merge pull request #1116 from Denton24646/fix/catalogsource-context-leak
Bug 1774720: catalogsource context cancel function leak
2 parents 02ff127 + b15e91f commit ba10413

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

pkg/controller/registry/grpc/source.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -142,27 +142,30 @@ func (s *SourceStore) watch(ctx context.Context, key resolver.CatalogKey, source
142142
case <-ctx.Done():
143143
return
144144
default:
145-
timer, _ := context.WithTimeout(ctx, s.stateTimeout(state))
146-
if source.Conn.WaitForStateChange(timer, state) {
147-
newState := source.Conn.GetState()
148-
state = newState
149-
150-
// update connection state
151-
src := s.Get(key)
152-
if src == nil {
153-
// source was removed, cleanup this goroutine
154-
return
145+
func() {
146+
timer, cancel := context.WithTimeout(ctx, s.stateTimeout(state))
147+
defer cancel()
148+
if source.Conn.WaitForStateChange(timer, state) {
149+
newState := source.Conn.GetState()
150+
state = newState
151+
152+
// update connection state
153+
src := s.Get(key)
154+
if src == nil {
155+
// source was removed, cleanup this goroutine
156+
return
157+
}
158+
159+
src.LastConnect = metav1.Now()
160+
src.ConnectionState = newState
161+
s.sourcesLock.Lock()
162+
s.sources[key] = *src
163+
s.sourcesLock.Unlock()
164+
165+
// notify subscriber
166+
s.notify <- SourceState{Key: key, State: newState}
155167
}
156-
157-
src.LastConnect = metav1.Now()
158-
src.ConnectionState = newState
159-
s.sourcesLock.Lock()
160-
s.sources[key] = *src
161-
s.sourcesLock.Unlock()
162-
163-
// notify subscriber
164-
s.notify <- SourceState{Key: key, State: newState}
165-
}
168+
}()
166169
}
167170
}
168171
}
@@ -190,6 +193,7 @@ func (s *SourceStore) Remove(key resolver.CatalogKey) error {
190193
func (s *SourceStore) AsClients(globalNamespace, localNamespace string) map[resolver.CatalogKey]client.Interface {
191194
refs := map[resolver.CatalogKey]client.Interface{}
192195
s.sourcesLock.RLock()
196+
defer s.sourcesLock.RUnlock()
193197
for key, source := range s.sources {
194198
if !(key.Namespace == globalNamespace || key.Namespace == localNamespace) {
195199
continue
@@ -199,7 +203,6 @@ func (s *SourceStore) AsClients(globalNamespace, localNamespace string) map[reso
199203
}
200204
refs[key] = client.NewClientFromConn(source.Conn)
201205
}
202-
s.sourcesLock.RUnlock()
203206

204207
// TODO : remove unhealthy
205208
return refs

0 commit comments

Comments
 (0)