Skip to content

Commit

Permalink
Allow to set GracefulShutdownTimeout to -1, disabling timeouts
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <vincepri@redhat.com>
  • Loading branch information
vincepri committed Feb 2, 2023
1 parent 6adc01f commit 4272b5a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/manager/internal.go
Expand Up @@ -509,7 +509,12 @@ func (cm *controllerManager) engageStopProcedure(stopComplete <-chan struct{}) e
//
// The shutdown context immediately expires if the gracefulShutdownTimeout is not set.
var shutdownCancel context.CancelFunc
cm.shutdownCtx, shutdownCancel = context.WithTimeout(context.Background(), cm.gracefulShutdownTimeout)
if cm.gracefulShutdownTimeout < 0 {
// We want to wait forever for the runnables to stop.
cm.shutdownCtx, shutdownCancel = context.WithCancel(context.Background())
} else {
cm.shutdownCtx, shutdownCancel = context.WithTimeout(context.Background(), cm.gracefulShutdownTimeout)
}
defer shutdownCancel()

// Start draining the errors before acquiring the lock to make sure we don't deadlock
Expand Down
44 changes: 44 additions & 0 deletions pkg/manager/manager_test.go
Expand Up @@ -936,6 +936,50 @@ var _ = Describe("manger.Manager", func() {
<-runnableStopped
})

It("should wait forever for runnables if gracefulShutdownTimeout is <0 (-1)", func() {
m, err := New(cfg, options)
Expect(err).NotTo(HaveOccurred())
for _, cb := range callbacks {
cb(m)
}
m.(*controllerManager).gracefulShutdownTimeout = time.Duration(-1)

Expect(m.Add(RunnableFunc(func(ctx context.Context) error {
<-ctx.Done()
time.Sleep(100 * time.Millisecond)
return nil
}))).ToNot(HaveOccurred())
Expect(m.Add(RunnableFunc(func(ctx context.Context) error {
<-ctx.Done()
time.Sleep(200 * time.Millisecond)
return nil
}))).ToNot(HaveOccurred())
Expect(m.Add(RunnableFunc(func(ctx context.Context) error {
<-ctx.Done()
time.Sleep(500 * time.Millisecond)
return nil
}))).ToNot(HaveOccurred())
Expect(m.Add(RunnableFunc(func(ctx context.Context) error {
<-ctx.Done()
time.Sleep(1500 * time.Millisecond)
return nil
}))).ToNot(HaveOccurred())

ctx, cancel := context.WithCancel(context.Background())
managerStopDone := make(chan struct{})
go func() {
defer GinkgoRecover()
Expect(m.Start(ctx)).NotTo(HaveOccurred())
close(managerStopDone)
}()
<-m.Elected()
cancel()

beforeDone := time.Now()
<-managerStopDone
Expect(time.Since(beforeDone)).To(BeNumerically(">=", 1500*time.Millisecond))
})

}

Context("with defaults", func() {
Expand Down

0 comments on commit 4272b5a

Please sign in to comment.