Skip to content

Commit

Permalink
Merge pull request #521 from wking/post-main-when-main-never-launched
Browse files Browse the repository at this point in the history
Bug 1921413: pkg/start: Fix shutdown deadlock when die before getting a leader lock
  • Loading branch information
openshift-merge-robot committed Feb 25, 2021
2 parents 9e25ac8 + 4e20989 commit cf0e3d1
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func (o *Options) run(ctx context.Context, controllerCtx *Context, lock *resourc
defer shutdownCancel()
postMainContext, postMainCancel := context.WithCancel(context.Background()) // extends beyond ctx
defer postMainCancel()
launchedMain := false

ch := make(chan os.Signal, 1)
defer func() { signal.Stop(ch) }()
Expand Down Expand Up @@ -234,6 +235,7 @@ func (o *Options) run(ctx context.Context, controllerCtx *Context, lock *resourc
RetryPeriod: retryPeriod,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(_ context.Context) { // no need for this passed-through postMainContext, because goroutines we launch inside will use runContext
launchedMain = true
resultChannelCount++
go func() {
defer utilruntime.HandleCrash()
Expand Down Expand Up @@ -267,6 +269,9 @@ func (o *Options) run(ctx context.Context, controllerCtx *Context, lock *resourc
case <-runContext.Done():
klog.Info("Run context completed; beginning two-minute graceful shutdown period.")
shutdownTimer = time.NewTimer(2 * time.Minute)
if !launchedMain { // no need to give post-main extra time if main never ran
postMainCancel()
}
case result := <-resultChannel:
resultChannelCount--
if result.error == nil {
Expand All @@ -282,6 +287,7 @@ func (o *Options) run(ctx context.Context, controllerCtx *Context, lock *resourc
} else { // shutting down
select {
case <-shutdownTimer.C: // never triggers after the channel is stopped, although it would not matter much if it did because subsequent cancel calls do nothing.
postMainCancel()
shutdownCancel()
shutdownTimer.Stop()
case result := <-resultChannel:
Expand Down

0 comments on commit cf0e3d1

Please sign in to comment.