Skip to content

🐛 Wait for NonLeaderElectionRunnables to terminate #1005

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

Closed
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
29 changes: 25 additions & 4 deletions pkg/manager/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,14 @@ func (cm *controllerManager) serveHealthProbes(stop <-chan struct{}) {
}

func (cm *controllerManager) Start(stop <-chan struct{}) error {
// join the passed-in stop channel as an upstream feeding into cm.internalStopper
defer close(cm.internalStopper)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work as an alternative? This should wait for the done channel on all exits from this function without having to explicitly write it at all exits from the function

doneCh := make(chan error, 1)
defer func() {
  close(cm.internalStopper)
  <-doneCh
}()

// This will block until all runnables started via
// startNonLeaderElectionRunnables finish.
doneCh := make(chan struct{})
defer func() {
// join the passed-in stop channel as an upstream feeding into cm.internalStopper
close(cm.internalStopper)
<-doneCh
}()

// initialize this here so that we reset the signal channel state on every start
cm.errSignal = &errSignaler{errSignal: make(chan struct{})}
Expand All @@ -454,7 +460,7 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
go cm.serveHealthProbes(cm.internalStop)
}

go cm.startNonLeaderElectionRunnables()
go cm.startNonLeaderElectionRunnables(doneCh)

if cm.resourceLock != nil {
err := cm.startLeaderElection()
Expand All @@ -477,12 +483,13 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
}
}

func (cm *controllerManager) startNonLeaderElectionRunnables() {
func (cm *controllerManager) startNonLeaderElectionRunnables(doneCh chan<- struct{}) {
cm.mu.Lock()
defer cm.mu.Unlock()

cm.waitForCache()

returnCh := make(chan struct{})
// Start the non-leaderelection Runnables after the cache has synced
for _, c := range cm.nonLeaderElectionRunnables {
// Controllers block, but we want to return an error if any have an error starting.
Expand All @@ -495,8 +502,22 @@ func (cm *controllerManager) startNonLeaderElectionRunnables() {
// we use %T here because we don't have a good stand-in for "name",
// and the full runnable might not serialize (mutexes, etc)
log.V(1).Info("non-leader-election runnable finished", "runnable type", fmt.Sprintf("%T", ctrl))
returnCh <- struct{}{}
}()
}

doneCount := 0

numRunners := len(cm.nonLeaderElectionRunnables)
for doneCount < numRunners {
select {
case <-returnCh:
doneCount++
default:
}
}
Comment on lines +511 to +518
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works and I'm fine with it, though it does make me think of a sync.WaitGroup, are we effectively reimplementing that?

close(returnCh)
close(doneCh)
}

func (cm *controllerManager) startLeaderElectionRunnables() {
Expand Down