From d58b88183333325f38b1b940c2935b479384a802 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Sun, 23 Apr 2023 11:54:43 +0800 Subject: [PATCH] fix startWatchLoop leak (#6352) Signed-off-by: Ryan Leung Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com> --- pkg/keyspace/keyspace_test.go | 2 +- pkg/keyspace/tso_keyspace_group.go | 8 ++++---- pkg/keyspace/tso_keyspace_group_test.go | 2 +- server/cluster/cluster.go | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/keyspace/keyspace_test.go b/pkg/keyspace/keyspace_test.go index f1ef85711fd..55e6ca35221 100644 --- a/pkg/keyspace/keyspace_test.go +++ b/pkg/keyspace/keyspace_test.go @@ -61,7 +61,7 @@ func (suite *keyspaceTestSuite) SetupTest() { allocator := mockid.NewIDAllocator() kgm := NewKeyspaceGroupManager(suite.ctx, store, nil, 0) suite.manager = NewKeyspaceManager(store, nil, allocator, &mockConfig{}, kgm) - suite.NoError(kgm.Bootstrap()) + suite.NoError(kgm.Bootstrap(suite.ctx)) suite.NoError(suite.manager.Bootstrap()) } diff --git a/pkg/keyspace/tso_keyspace_group.go b/pkg/keyspace/tso_keyspace_group.go index ce9e3005bbc..28a16afeb74 100644 --- a/pkg/keyspace/tso_keyspace_group.go +++ b/pkg/keyspace/tso_keyspace_group.go @@ -101,7 +101,7 @@ func NewKeyspaceGroupManager(ctx context.Context, store endpoint.KeyspaceGroupSt } // Bootstrap saves default keyspace group info and init group mapping in the memory. -func (m *GroupManager) Bootstrap() error { +func (m *GroupManager) Bootstrap(ctx context.Context) error { // Force the membership restriction that the default keyspace must belong to default keyspace group. // Have no information to specify the distribution of the default keyspace group replicas, so just // leave the replica/member list empty. The TSO service will assign the default keyspace group replica @@ -135,7 +135,7 @@ func (m *GroupManager) Bootstrap() error { m.nodesBalancer = balancer.GenByPolicy[string](m.policy) m.serviceRegistryMap = make(map[string]string) m.wg.Add(1) - go m.startWatchLoop() + go m.startWatchLoop(ctx) } return nil } @@ -146,10 +146,10 @@ func (m *GroupManager) Close() { m.wg.Wait() } -func (m *GroupManager) startWatchLoop() { +func (m *GroupManager) startWatchLoop(parentCtx context.Context) { defer logutil.LogPanic() defer m.wg.Done() - ctx, cancel := context.WithCancel(m.ctx) + ctx, cancel := context.WithCancel(parentCtx) defer cancel() var ( resp *clientv3.GetResponse diff --git a/pkg/keyspace/tso_keyspace_group_test.go b/pkg/keyspace/tso_keyspace_group_test.go index ed7992ba552..080e62a6e25 100644 --- a/pkg/keyspace/tso_keyspace_group_test.go +++ b/pkg/keyspace/tso_keyspace_group_test.go @@ -47,7 +47,7 @@ func (suite *keyspaceGroupTestSuite) SetupTest() { idAllocator := mockid.NewIDAllocator() cluster := mockcluster.NewCluster(suite.ctx, mockconfig.NewTestOptions()) suite.kg = NewKeyspaceManager(store, cluster, idAllocator, &mockConfig{}, suite.kgm) - suite.NoError(suite.kgm.Bootstrap()) + suite.NoError(suite.kgm.Bootstrap(suite.ctx)) } func (suite *keyspaceGroupTestSuite) TearDownTest() { diff --git a/server/cluster/cluster.go b/server/cluster/cluster.go index 3fcd3efdf28..14c3523ba02 100644 --- a/server/cluster/cluster.go +++ b/server/cluster/cluster.go @@ -275,7 +275,7 @@ func (c *RaftCluster) Start(s Server) error { return nil } if s.IsAPIServiceMode() { - err = c.keyspaceGroupManager.Bootstrap() + err = c.keyspaceGroupManager.Bootstrap(c.ctx) if err != nil { return err }