Skip to content

Commit

Permalink
Added waitgroups for autoupdate workers to complete before stopping
Browse files Browse the repository at this point in the history
The wait.UntilWithContext will repeatedly run the function passed to it
till the context also passed to it is done. At which point it will
return. Adding a wait group after the UntilWithContext ensures that
the top level goroutine does not return till all the spawned goroutines
terminate. Also added `HandleCrash()` to log any panics in inner
goroutines.
  • Loading branch information
arjunrn committed Jun 30, 2021
1 parent a304b71 commit f15f84e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/autoupdate/autoupdate.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"sort"
"sync"
"time"

"github.com/blang/semver/v4"
Expand Down Expand Up @@ -92,13 +93,18 @@ func (ctrl *Controller) Run(ctx context.Context, workers int) error {
if !cache.WaitForCacheSync(ctx.Done(), ctrl.cacheSynced...) {
return fmt.Errorf("caches never synchronized: %w", ctx.Err())
}

var wg sync.WaitGroup
wg.Add(workers)
for i := 0; i < workers; i++ {
// FIXME: actually wait until these complete if the Context is canceled. And possibly add utilruntime.HandleCrash.
go wait.UntilWithContext(ctx, ctrl.worker, time.Second)
go func() {
defer wg.Done()
defer utilruntime.HandleCrash()
wait.UntilWithContext(ctx, ctrl.worker, time.Second)
}()
}

<-ctx.Done()
wg.Wait()
return nil
}

Expand Down

0 comments on commit f15f84e

Please sign in to comment.