Skip to content

Commit e669984

Browse files
authored
owner: fix data race on ownerManager.campaignCancel (#56362) (#57132)
close #56053
1 parent cf2978a commit e669984

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

pkg/owner/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ go_test(
3737
],
3838
embed = [":owner"],
3939
flaky = True,
40-
shard_count = 7,
40+
shard_count = 8,
4141
deps = [
4242
"//pkg/ddl",
4343
"//pkg/infoschema",

pkg/owner/manager.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ func (m *ownerManager) CampaignOwner(withTTL ...int) error {
192192
}
193193
m.sessionLease.Store(int64(session.Lease()))
194194
m.wg.Add(1)
195-
go m.campaignLoop(session)
195+
var campaignContext context.Context
196+
campaignContext, m.campaignCancel = context.WithCancel(m.ctx)
197+
go m.campaignLoop(campaignContext, session)
196198
return nil
197199
}
198200

@@ -232,9 +234,7 @@ func (m *ownerManager) CampaignCancel() {
232234
m.wg.Wait()
233235
}
234236

235-
func (m *ownerManager) campaignLoop(etcdSession *concurrency.Session) {
236-
var campaignContext context.Context
237-
campaignContext, m.campaignCancel = context.WithCancel(m.ctx)
237+
func (m *ownerManager) campaignLoop(campaignContext context.Context, etcdSession *concurrency.Session) {
238238
defer func() {
239239
m.campaignCancel()
240240
if r := recover(); r != nil {

pkg/owner/manager_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,20 @@ func deleteLeader(cli *clientv3.Client, prefixKey string) error {
428428
_, err = cli.Delete(context.Background(), string(resp.Kvs[0].Key))
429429
return errors.Trace(err)
430430
}
431+
432+
func TestImmediatelyCancel(t *testing.T) {
433+
if runtime.GOOS == "windows" {
434+
t.Skip("integration.NewClusterV3 will create file contains a colon which is not allowed on Windows")
435+
}
436+
integration.BeforeTestExternal(t)
437+
438+
tInfo := newTestInfo(t)
439+
d := tInfo.ddl
440+
defer tInfo.Close(t)
441+
ownerManager := d.OwnerManager()
442+
for i := 0; i < 10; i++ {
443+
err := ownerManager.CampaignOwner()
444+
require.NoError(t, err)
445+
ownerManager.CampaignCancel()
446+
}
447+
}

0 commit comments

Comments
 (0)