From 91b33a97635b9642a8a88760d7a3199d89ad9b72 Mon Sep 17 00:00:00 2001 From: Max Korp Date: Fri, 21 Sep 2018 16:06:40 -0700 Subject: [PATCH] Ensure that closed watchers are always stopped This would cause a new watcher to be started without ever cleaning up the old one, which was causing memory leaks. --- store/proxy/proxy_store.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/store/proxy/proxy_store.go b/store/proxy/proxy_store.go index 70a4a5c4..54f66fb7 100644 --- a/store/proxy/proxy_store.go +++ b/store/proxy/proxy_store.go @@ -243,8 +243,9 @@ func (s *Store) realWatch(apiContext *types.APIContext, schema *types.Schema, op decoder := streaming.NewDecoder(framer, &unstructuredDecoder{}) watcher := watch.NewStreamWatcher(restclientwatch.NewDecoder(decoder, &unstructuredDecoder{})) + watchingContext, cancelWatchingContext := context.WithCancel(apiContext.Request.Context()) go func() { - <-apiContext.Request.Context().Done() + <-watchingContext.Done() logrus.Debugf("stopping watcher for %s", schema.ID) watcher.Stop() }() @@ -261,6 +262,7 @@ func (s *Store) realWatch(apiContext *types.APIContext, schema *types.Schema, op } logrus.Debugf("closing watcher for %s", schema.ID) close(result) + cancelWatchingContext() }() return result, nil