Skip to content
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

🐛 Quiet context.Canceled errors during shutdown #2745

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions pkg/manager/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,8 @@ func (cm *controllerManager) engageStopProcedure(stopComplete <-chan struct{}) e
cm.internalCancel()
})
select {
case err, ok := <-cm.errChan:
if ok {
case err := <-cm.errChan:
sbueringer marked this conversation as resolved.
Show resolved Hide resolved
if !errors.Is(err, context.Canceled) {
Comment on lines -488 to +489
Copy link
Member

Choose a reason for hiding this comment

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

Why not keep checking the ok value here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see any calls to close errChan, so ok is always true (today).

Also, in my experience, reading from a closed channel in select is for some kind of control flow (break or return or so), or the channel is assigned nil so that it is not selected again.

Copy link
Member

@sbueringer sbueringer Apr 15, 2024

Choose a reason for hiding this comment

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

I don't see any calls to close errChan, so ok is always true (today).

Sounds okay to me. I guess if we ever start closing the channel we have to check all usages of the channel anyway.

cm.logger.Error(err, "error received after stop sequence was engaged")
}
case <-stopComplete:
Expand Down
49 changes: 49 additions & 0 deletions pkg/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"time"

"github.com/go-logr/logr"
"github.com/go-logr/logr/funcr"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -1044,6 +1045,54 @@ var _ = Describe("manger.Manager", func() {
}))).NotTo(Succeed())
})

It("should not return runnables context.Canceled errors", func() {
Expect(options.Logger).To(BeZero(), "this test overrides Logger")

var log struct {
sync.Mutex
messages []string
}
options.Logger = funcr.NewJSON(func(object string) {
log.Lock()
log.messages = append(log.messages, object)
log.Unlock()
}, funcr.Options{})

m, err := New(cfg, options)
Expect(err).NotTo(HaveOccurred())
for _, cb := range callbacks {
cb(m)
}

// Runnables may return ctx.Err() as shown in some [context.Context] examples.
started := make(chan struct{})
Expect(m.Add(RunnableFunc(func(ctx context.Context) error {
close(started)
<-ctx.Done()
return ctx.Err()
}))).To(Succeed())

stopped := make(chan error)
ctx, cancel := context.WithCancel(context.Background())
go func() {
stopped <- m.Start(ctx)
}()

// Wait for runnables to start, signal the manager, and wait for it.
<-started
cancel()
Expect(<-stopped).To(Succeed())
sbueringer marked this conversation as resolved.
Show resolved Hide resolved

// TODO: Diagnose why LeaderElection races after Start() returns.
if options.LeaderElection {
sbueringer marked this conversation as resolved.
Show resolved Hide resolved
time.Sleep(time.Second)
}

Expect(log.messages).To(Not(ContainElement(
ContainSubstring(context.Canceled.Error()),
)))
})

It("should return both runnables and stop errors when both error", func() {
m, err := New(cfg, options)
Expect(err).NotTo(HaveOccurred())
Expand Down