Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix races in memory topo and watcher #11065

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go/vt/srvtopo/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ func (entry *watchEntry) onErrorLocked(callerCtx context.Context, err error, ini

entry.watchState = watchStateIdle

if len(entry.listeners) > 0 {
// only retry the watch if we haven't been explicitly interrupted
if len(entry.listeners) > 0 && !topo.IsErrType(err, topo.Interrupted) {
go func() {
time.Sleep(entry.rw.cacheRefreshInterval)

Expand Down
25 changes: 12 additions & 13 deletions go/vt/topo/memorytopo/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,29 @@ func (mp *cLeaderParticipation) WaitForLeadership() (context.Context, error) {
}

electionPath := path.Join(electionsPath, mp.name)
var ld topo.LockDescriptor

// We use a cancelable context here. If stop is closed,
// we just cancel that context.
lockCtx, lockCancel := context.WithCancel(context.Background())
go func() {
<-mp.stop
if ld != nil {
if err := ld.Unlock(context.Background()); err != nil {
log.Errorf("failed to unlock LockDescriptor %v: %v", electionPath, err)
}
}
lockCancel()
close(mp.done)
}()

// Try to get the primaryship, by getting a lock.
var err error
ld, err = mp.c.Lock(lockCtx, electionPath, mp.id)
ld, err := mp.c.Lock(lockCtx, electionPath, mp.id)
if err != nil {
lockCancel()
close(mp.done)
// It can be that we were interrupted.
return nil, err
}

go func() {
<-mp.stop
if err := ld.Unlock(context.Background()); err != nil {
log.Errorf("failed to unlock LockDescriptor %v: %v", electionPath, err)
}
lockCancel()
close(mp.done)
}()

// We got the lock. Return the lockContext. If Stop() is called,
// it will cancel the lockCtx, and cancel the returned context.
return lockCtx, nil
Expand Down