Skip to content

Commit fc73db2

Browse files
committed
Quiet context.Canceled errors during shutdown
Runnable implementations that return ctx.Err() cause a spurious "error received after stop" log message.
1 parent 2136860 commit fc73db2

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

pkg/manager/internal.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,8 @@ func (cm *controllerManager) engageStopProcedure(stopComplete <-chan struct{}) e
485485
cm.internalCancel()
486486
})
487487
select {
488-
case err, ok := <-cm.errChan:
489-
if ok {
488+
case err := <-cm.errChan:
489+
if !errors.Is(err, context.Canceled) {
490490
cm.logger.Error(err, "error received after stop sequence was engaged")
491491
}
492492
case <-stopComplete:

pkg/manager/manager_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"time"
3131

3232
"github.com/go-logr/logr"
33+
"github.com/go-logr/logr/funcr"
3334
. "github.com/onsi/ginkgo/v2"
3435
. "github.com/onsi/gomega"
3536
"github.com/prometheus/client_golang/prometheus"
@@ -1044,6 +1045,44 @@ var _ = Describe("manger.Manager", func() {
10441045
}))).NotTo(Succeed())
10451046
})
10461047

1048+
It("should not return runnables context.Canceled errors", func() {
1049+
Expect(options.Logger).To(BeZero(), "this test overrides Logger")
1050+
1051+
logs := []string{}
1052+
options.Logger = funcr.NewJSON(func(object string) {
1053+
logs = append(logs, object)
1054+
}, funcr.Options{})
1055+
1056+
m, err := New(cfg, options)
1057+
Expect(err).NotTo(HaveOccurred())
1058+
for _, cb := range callbacks {
1059+
cb(m)
1060+
}
1061+
1062+
// Runnables may return ctx.Err() as shown in some [context.Context] examples.
1063+
started := make(chan struct{})
1064+
Expect(m.Add(RunnableFunc(func(ctx context.Context) error {
1065+
close(started)
1066+
<-ctx.Done()
1067+
return ctx.Err()
1068+
}))).To(Succeed())
1069+
1070+
stopped := make(chan error)
1071+
ctx, cancel := context.WithCancel(context.Background())
1072+
go func() {
1073+
stopped <- m.Start(ctx)
1074+
}()
1075+
1076+
// Wait for runnables to start, signal the manager, and wait for it.
1077+
<-started
1078+
cancel()
1079+
Expect(<-stopped).To(Succeed())
1080+
1081+
Expect(logs).To(Not(ContainElement(
1082+
ContainSubstring(context.Canceled.Error()),
1083+
)))
1084+
})
1085+
10471086
It("should return both runnables and stop errors when both error", func() {
10481087
m, err := New(cfg, options)
10491088
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)