From 4133aa1f3c9ce62a5bed5ba8a9162888193ee455 Mon Sep 17 00:00:00 2001 From: hauer <254958181@qq.com> Date: Mon, 19 Feb 2024 15:24:38 +0800 Subject: [PATCH] fix: memory leak when etcd watch failed (#2920) --- api/internal/core/store/store.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/api/internal/core/store/store.go b/api/internal/core/store/store.go index b75f406dda..6383575651 100644 --- a/api/internal/core/store/store.go +++ b/api/internal/core/store/store.go @@ -63,6 +63,7 @@ type GenericStore struct { cancel context.CancelFunc closing bool + inited bool //is inited } type GenericStoreOption struct { @@ -109,12 +110,20 @@ func ReInit() error { return err } } + + //clear storeNeedReInit + storeNeedReInit = storeNeedReInit[:0] return nil } func (s *GenericStore) Init() error { s.initLock.Lock() defer s.initLock.Unlock() + //return if inited + if s.IsInited() { + return nil + } + return s.listAndWatch() } @@ -351,6 +360,7 @@ func (s *GenericStore) listAndWatch() error { // start watch s.cancel = s.watch() + s.SetIsInited(true) return nil } @@ -362,7 +372,11 @@ func (s *GenericStore) watch() context.CancelFunc { defer func() { if !s.closing { log.Errorf("etcd watch exception closed, restarting: resource: %s", s.Type()) - storeNeedReInit = append(storeNeedReInit, s) + //only add to storeNeedReInit when from inited to uninited + if s.IsInited() { + s.SetIsInited(false) + storeNeedReInit = append(storeNeedReInit, s) + } } }() defer runtime.HandlePanic() @@ -421,3 +435,11 @@ func (s *GenericStore) GetObjStorageKey(obj interface{}) string { func (s *GenericStore) GetStorageKey(key string) string { return fmt.Sprintf("%s/%s", s.opt.BasePath, key) } + +func (s *GenericStore) IsInited() bool { + return s.inited +} + +func (s *GenericStore) SetIsInited(inited bool) { + s.inited = inited +}