Skip to content

Commit

Permalink
Merge pull request #1379 from vincepri/recorder-proper-stop
Browse files Browse the repository at this point in the history
🐛 Fix a race condition between leader election and recorder
  • Loading branch information
k8s-ci-robot committed Feb 10, 2021
2 parents 0247574 + c326e7a commit 16bf3ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
19 changes: 16 additions & 3 deletions pkg/manager/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ type controllerManager struct {
// it must be deferred until after gracefulShutdown is done.
leaderElectionCancel context.CancelFunc

// leaderElectionStopped is an internal channel used to signal the stopping procedure that the
// LeaderElection.Run(...) function has returned and the shutdown can proceed.
leaderElectionStopped chan struct{}

// stop procedure engaged. In other words, we should not add anything else to the manager
stopProcedureEngaged bool

Expand Down Expand Up @@ -553,11 +557,16 @@ func (cm *controllerManager) engageStopProcedure(stopComplete <-chan struct{}) e

// waitForRunnableToEnd blocks until all runnables ended or the
// tearDownTimeout was reached. In the latter case, an error is returned.
func (cm *controllerManager) waitForRunnableToEnd(shutdownCancel context.CancelFunc) error {
func (cm *controllerManager) waitForRunnableToEnd(shutdownCancel context.CancelFunc) (retErr error) {
// Cancel leader election only after we waited. It will os.Exit() the app for safety.
defer func() {
if cm.leaderElectionCancel != nil {
if retErr == nil && cm.leaderElectionCancel != nil {
// After asking the context to be cancelled, make sure
// we wait for the leader stopped channel to be closed, otherwise
// we might encounter race conditions between this code
// and the event recorder, which is used within leader election code.
cm.leaderElectionCancel()
<-cm.leaderElectionStopped
}
}()

Expand Down Expand Up @@ -660,7 +669,11 @@ func (cm *controllerManager) startLeaderElection() (err error) {
}

// Start the leader elector process
go l.Run(ctx)
go func() {
l.Run(ctx)
<-ctx.Done()
close(cm.leaderElectionStopped)
}()
return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
livenessEndpointName: options.LivenessEndpointName,
gracefulShutdownTimeout: *options.GracefulShutdownTimeout,
internalProceduresStop: make(chan struct{}),
leaderElectionStopped: make(chan struct{}),
}, nil
}

Expand Down

0 comments on commit 16bf3ad

Please sign in to comment.