diff --git a/pkg/owner/BUILD.bazel b/pkg/owner/BUILD.bazel index 319601e30eba6..fe9755c440bd3 100644 --- a/pkg/owner/BUILD.bazel +++ b/pkg/owner/BUILD.bazel @@ -37,7 +37,7 @@ go_test( ], embed = [":owner"], flaky = True, - shard_count = 7, + shard_count = 8, deps = [ "//pkg/ddl", "//pkg/infoschema", diff --git a/pkg/owner/manager.go b/pkg/owner/manager.go index a3dcb2eae9ffc..a216e5b1972ba 100644 --- a/pkg/owner/manager.go +++ b/pkg/owner/manager.go @@ -192,7 +192,9 @@ func (m *ownerManager) CampaignOwner(withTTL ...int) error { } m.sessionLease.Store(int64(session.Lease())) m.wg.Add(1) - go m.campaignLoop(session) + var campaignContext context.Context + campaignContext, m.campaignCancel = context.WithCancel(m.ctx) + go m.campaignLoop(campaignContext, session) return nil } @@ -232,9 +234,7 @@ func (m *ownerManager) CampaignCancel() { m.wg.Wait() } -func (m *ownerManager) campaignLoop(etcdSession *concurrency.Session) { - var campaignContext context.Context - campaignContext, m.campaignCancel = context.WithCancel(m.ctx) +func (m *ownerManager) campaignLoop(campaignContext context.Context, etcdSession *concurrency.Session) { defer func() { m.campaignCancel() if r := recover(); r != nil { diff --git a/pkg/owner/manager_test.go b/pkg/owner/manager_test.go index e72496730700e..4ece04560053d 100644 --- a/pkg/owner/manager_test.go +++ b/pkg/owner/manager_test.go @@ -428,3 +428,20 @@ func deleteLeader(cli *clientv3.Client, prefixKey string) error { _, err = cli.Delete(context.Background(), string(resp.Kvs[0].Key)) return errors.Trace(err) } + +func TestImmediatelyCancel(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("integration.NewClusterV3 will create file contains a colon which is not allowed on Windows") + } + integration.BeforeTestExternal(t) + + tInfo := newTestInfo(t) + d := tInfo.ddl + defer tInfo.Close(t) + ownerManager := d.OwnerManager() + for i := 0; i < 10; i++ { + err := ownerManager.CampaignOwner() + require.NoError(t, err) + ownerManager.CampaignCancel() + } +}