-
Notifications
You must be signed in to change notification settings - Fork 1.3k
🐛 manager: fix infinite CPU spin in runnableGroup.Start on context cancellation #3440
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
Changes from all commits
4e1d027
c00cf65
ab2c563
8b18f6f
b0f36fd
8deb807
bc6b45c
9304dd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -167,8 +167,6 @@ func (r *runnableGroup) Start(ctx context.Context) error { | |
| var retErr error | ||
|
|
||
| r.startOnce.Do(func() { | ||
| defer close(r.startReadyCh) | ||
|
|
||
| // Start the internal reconciler. | ||
| go r.reconcile() | ||
|
|
||
|
|
@@ -194,6 +192,12 @@ func (r *runnableGroup) Start(ctx context.Context) error { | |
| if err := ctx.Err(); !errors.Is(err, context.Canceled) { | ||
| retErr = err | ||
| } | ||
| return | ||
| case <-r.ctx.Done(): | ||
| // The group's internal context was cancelled (by StopAndWait). | ||
| // This unblocks readiness waiting when senders have exited via | ||
| // r.ctx.Done() and will no longer send to startReadyCh. | ||
| return | ||
| case rn := <-r.startReadyCh: | ||
| for i, existing := range r.startQueue { | ||
| if existing == rn { | ||
|
|
@@ -202,8 +206,10 @@ func (r *runnableGroup) Start(ctx context.Context) error { | |
| break | ||
| } | ||
| } | ||
| // We're done waiting if the queue is empty, return. | ||
| // We're done waiting if the queue is empty. | ||
| // All senders have already sent, so it is safe to close. | ||
| if len(r.startQueue) == 0 { | ||
| close(r.startReadyCh) | ||
| return | ||
| } | ||
| } | ||
|
|
@@ -245,7 +251,14 @@ func (r *runnableGroup) reconcile() { | |
| go func() { | ||
| if rn.Check(r.ctx) { | ||
| if rn.signalReady { | ||
| r.startReadyCh <- rn | ||
| // Use select to avoid sending after Start() has returned. | ||
| // When ctx is cancelled, Start() exits and is no longer | ||
| // receiving from startReadyCh. Without this select, we'd | ||
| // either block forever or panic if the channel were closed. | ||
| select { | ||
| case <-r.ctx.Done(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this has to be the first case otherwise this will still send always on startReadyCh |
||
| case r.startReadyCh <- rn: | ||
| } | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -363,6 +363,54 @@ func (r leaderElectionAndWarmupRunnable) NeedLeaderElection() bool { | |
| return r.needLeaderElection | ||
| } | ||
|
|
||
| // TestStartReturnsWhenContextCancelledWithPendingReadinessCheck verifies that | ||
| // runnableGroup.Start() returns promptly when the context is cancelled, even if | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like there's an issue, not sure if in prod or test code https://prow.k8s.io/view/gs/kubernetes-ci-logs/pr-logs/pull/kubernetes-sigs_controller-runtime/3440/pull-controller-runtime-test/2018359601396715520 (see https://storage.googleapis.com/kubernetes-ci-logs/pr-logs/pull/kubernetes-sigs_controller-runtime/3440/pull-controller-runtime-test/2018359601396715520/build-log.txt)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hi @sbueringer Thanks for the clarification request, this turned out to be a real concurrency issue in runnableGroup, not just a test artifact. Start() was closing startReadyCh while readiness goroutines could still send on it, which caused a data race and send on closed channel panic depending on timing. Tests just made this easier to reproduce. Removing the channel close exposed a second shutdown issue: Start() could block waiting for readiness when StopAndWait() cancelled the group context and all senders had already exited. The fix now handles both sides explicitly: senders stop on r.ctx.Done(), and Start() exits readiness waiting on either ctx.Done() or r.ctx.Done(). This avoids races and deadlocks while preserving normal startup behavior. Unit tests and go test -race pass locally; envtest-based integration tests should be covered by CI. |
||
| // some runnables have not signaled readiness. This is a regression test for a bug | ||
| // where Start() would spin indefinitely on ctx.Done() without returning. | ||
| func TestStartReturnsWhenContextCancelledWithPendingReadinessCheck(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| g := NewWithT(t) | ||
| rg := newRunnableGroup(defaultBaseContext, make(chan error, 1)) | ||
|
|
||
| // Add a runnable with a readiness check that never completes. | ||
| // This simulates a cache sync that hangs or fails. | ||
| g.Expect(rg.Add( | ||
| RunnableFunc(func(c context.Context) error { | ||
| <-c.Done() | ||
| return nil | ||
| }), | ||
| func(ctx context.Context) bool { | ||
| // Block forever - never signal ready | ||
| <-ctx.Done() | ||
| return false | ||
| }, | ||
| )).To(Succeed()) | ||
|
|
||
| ctx, cancel := context.WithCancel(t.Context()) | ||
|
|
||
| // Start the group in a goroutine | ||
| startReturned := make(chan struct{}) | ||
| go func() { | ||
| defer close(startReturned) | ||
| _ = rg.Start(ctx) | ||
| }() | ||
|
|
||
| // Cancel the context | ||
| cancel() | ||
|
|
||
| // Start() should return promptly after context cancellation, not hang or spin | ||
| select { | ||
| case <-startReturned: | ||
| // Success: Start() returned after context cancellation | ||
| case <-time.After(5 * time.Second): | ||
| t.Fatal("Start() did not return after context was cancelled - likely spinning on ctx.Done()") | ||
| } | ||
|
|
||
| // Cleanup | ||
| rg.StopAndWait(t.Context()) | ||
| } | ||
|
|
||
| func TestWarmupFunctionIsExecutedWhenWarmupGroupIsStarted(t *testing.T) { | ||
| t.Parallel() | ||
| synctest.Test(t, func(t *testing.T) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.