Skip to content

Commit 7beb471

Browse files
authored
fixed datastore bug to clean all go routines when pool is unset. (#810)
current implementation leaves dangling go routines and structs which will consume resources and hold unused objects from being GCd Signed-off-by: Nir Rozenbaum <[email protected]>
1 parent 2b66451 commit 7beb471

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

pkg/epp/datastore/datastore.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ func (ds *datastore) Clear() {
101101
defer ds.poolAndModelsMu.Unlock()
102102
ds.pool = nil
103103
ds.models = make(map[string]*v1alpha2.InferenceModel)
104+
// stop all pods go routines before clearing the pods map.
105+
ds.pods.Range(func(_, v any) bool {
106+
v.(backendmetrics.PodMetrics).StopRefreshLoop()
107+
return true
108+
})
104109
ds.pods.Clear()
105110
}
106111

@@ -245,14 +250,15 @@ func (ds *datastore) PodGetAll() []backendmetrics.PodMetrics {
245250

246251
func (ds *datastore) PodList(predicate func(backendmetrics.PodMetrics) bool) []backendmetrics.PodMetrics {
247252
res := []backendmetrics.PodMetrics{}
248-
fn := func(k, v any) bool {
253+
254+
ds.pods.Range(func(k, v any) bool {
249255
pm := v.(backendmetrics.PodMetrics)
250256
if predicate(pm) {
251257
res = append(res, pm)
252258
}
253259
return true
254-
}
255-
ds.pods.Range(fn)
260+
})
261+
256262
return res
257263
}
258264

@@ -307,15 +313,14 @@ func (ds *datastore) podResyncAll(ctx context.Context, ctrlClient client.Client)
307313
}
308314

309315
// Remove pods that don't belong to the pool or not ready any more.
310-
deleteFn := func(k, v any) bool {
316+
ds.pods.Range(func(k, v any) bool {
311317
pm := v.(backendmetrics.PodMetrics)
312318
if exist := activePods[pm.GetPod().NamespacedName.Name]; !exist {
313319
logger.V(logutil.VERBOSE).Info("Removing pod", "pod", pm.GetPod())
314320
ds.PodDelete(pm.GetPod().NamespacedName)
315321
}
316322
return true
317-
}
318-
ds.pods.Range(deleteFn)
323+
})
319324

320325
return nil
321326
}

0 commit comments

Comments
 (0)