Skip to content

Commit e4e9872

Browse files
authored
owner: fix data race on ownerManager.campaignCancel (#56362) (#57019)
close #56053
1 parent 7578037 commit e4e9872

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

owner/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ go_test(
3535
],
3636
embed = [":owner"],
3737
flaky = True,
38-
shard_count = 6,
38+
shard_count = 7,
3939
deps = [
4040
"//ddl",
4141
"//infoschema",

owner/manager.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ func (m *ownerManager) CampaignOwner(withTTL ...int) error {
186186
}
187187
m.sessionLease.Store(int64(session.Lease()))
188188
m.wg.Add(1)
189-
go m.campaignLoop(session)
189+
var campaignContext context.Context
190+
campaignContext, m.campaignCancel = context.WithCancel(m.ctx)
191+
go m.campaignLoop(campaignContext, session)
190192
return nil
191193
}
192194

@@ -226,9 +228,7 @@ func (m *ownerManager) CampaignCancel() {
226228
m.wg.Wait()
227229
}
228230

229-
func (m *ownerManager) campaignLoop(etcdSession *concurrency.Session) {
230-
var campaignContext context.Context
231-
campaignContext, m.campaignCancel = context.WithCancel(m.ctx)
231+
func (m *ownerManager) campaignLoop(campaignContext context.Context, etcdSession *concurrency.Session) {
232232
defer func() {
233233
m.campaignCancel()
234234
if r := recover(); r != nil {

owner/manager_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,20 @@ func deleteLeader(cli *clientv3.Client, prefixKey string) error {
379379
_, err = cli.Delete(context.Background(), string(resp.Kvs[0].Key))
380380
return errors.Trace(err)
381381
}
382+
383+
func TestImmediatelyCancel(t *testing.T) {
384+
if runtime.GOOS == "windows" {
385+
t.Skip("integration.NewClusterV3 will create file contains a colon which is not allowed on Windows")
386+
}
387+
integration.BeforeTestExternal(t)
388+
389+
tInfo := newTestInfo(t)
390+
d := tInfo.ddl
391+
defer tInfo.Close(t)
392+
ownerManager := d.OwnerManager()
393+
for i := 0; i < 10; i++ {
394+
err := ownerManager.CampaignOwner()
395+
require.NoError(t, err)
396+
ownerManager.CampaignCancel()
397+
}
398+
}

0 commit comments

Comments
 (0)