Skip to content

Commit 7a19e15

Browse files
authored
Merge pull request #2198 from k8s-infra-cherrypick-robot/cherry-pick-2169-to-release-0.14
[release-0.14] 🐛 Allow to set GracefulShutdownTimeout to -1, disabling timeouts
2 parents a9d6107 + d4a8a96 commit 7a19e15

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

pkg/manager/internal.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,12 @@ func (cm *controllerManager) engageStopProcedure(stopComplete <-chan struct{}) e
528528
//
529529
// The shutdown context immediately expires if the gracefulShutdownTimeout is not set.
530530
var shutdownCancel context.CancelFunc
531-
cm.shutdownCtx, shutdownCancel = context.WithTimeout(context.Background(), cm.gracefulShutdownTimeout)
531+
if cm.gracefulShutdownTimeout < 0 {
532+
// We want to wait forever for the runnables to stop.
533+
cm.shutdownCtx, shutdownCancel = context.WithCancel(context.Background())
534+
} else {
535+
cm.shutdownCtx, shutdownCancel = context.WithTimeout(context.Background(), cm.gracefulShutdownTimeout)
536+
}
532537
defer shutdownCancel()
533538

534539
// Start draining the errors before acquiring the lock to make sure we don't deadlock

pkg/manager/manager_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,50 @@ var _ = Describe("manger.Manager", func() {
10761076
<-runnableStopped
10771077
})
10781078

1079+
It("should wait forever for runnables if gracefulShutdownTimeout is <0 (-1)", func() {
1080+
m, err := New(cfg, options)
1081+
Expect(err).NotTo(HaveOccurred())
1082+
for _, cb := range callbacks {
1083+
cb(m)
1084+
}
1085+
m.(*controllerManager).gracefulShutdownTimeout = time.Duration(-1)
1086+
1087+
Expect(m.Add(RunnableFunc(func(ctx context.Context) error {
1088+
<-ctx.Done()
1089+
time.Sleep(100 * time.Millisecond)
1090+
return nil
1091+
}))).ToNot(HaveOccurred())
1092+
Expect(m.Add(RunnableFunc(func(ctx context.Context) error {
1093+
<-ctx.Done()
1094+
time.Sleep(200 * time.Millisecond)
1095+
return nil
1096+
}))).ToNot(HaveOccurred())
1097+
Expect(m.Add(RunnableFunc(func(ctx context.Context) error {
1098+
<-ctx.Done()
1099+
time.Sleep(500 * time.Millisecond)
1100+
return nil
1101+
}))).ToNot(HaveOccurred())
1102+
Expect(m.Add(RunnableFunc(func(ctx context.Context) error {
1103+
<-ctx.Done()
1104+
time.Sleep(1500 * time.Millisecond)
1105+
return nil
1106+
}))).ToNot(HaveOccurred())
1107+
1108+
ctx, cancel := context.WithCancel(context.Background())
1109+
managerStopDone := make(chan struct{})
1110+
go func() {
1111+
defer GinkgoRecover()
1112+
Expect(m.Start(ctx)).NotTo(HaveOccurred())
1113+
close(managerStopDone)
1114+
}()
1115+
<-m.Elected()
1116+
cancel()
1117+
1118+
beforeDone := time.Now()
1119+
<-managerStopDone
1120+
Expect(time.Since(beforeDone)).To(BeNumerically(">=", 1500*time.Millisecond))
1121+
})
1122+
10791123
}
10801124

10811125
Context("with defaults", func() {

0 commit comments

Comments
 (0)