Skip to content

Commit

Permalink
owner: fix data race on ownerManager.campaignCancel (#56362)
Browse files Browse the repository at this point in the history
close #56053
  • Loading branch information
fishiu authored Sep 27, 2024
1 parent bad2ecd commit afdd5c2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/owner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ go_test(
],
embed = [":owner"],
flaky = True,
shard_count = 8,
shard_count = 9,
deps = [
"//pkg/ddl",
"//pkg/infoschema",
Expand Down
8 changes: 4 additions & 4 deletions pkg/owner/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,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
}

Expand Down Expand Up @@ -241,9 +243,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 {
Expand Down
17 changes: 17 additions & 0 deletions pkg/owner/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,23 @@ func deleteLeader(cli *clientv3.Client, prefixKey string) error {
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()
}
}

func TestAcquireDistributedLock(t *testing.T) {
const addrFmt = "http://127.0.0.1:%d"
cfg := embed.NewConfig()
Expand Down

0 comments on commit afdd5c2

Please sign in to comment.