Skip to content

Commit 00887f6

Browse files
LukeAVanDriekfswain
authored andcommitted
fix: Prevent panic on nil item during shutdown (kubernetes-sigs#1257)
A race condition existed where the ShardProcessor could panic if a `nil` flowItem was present in its enqueue channel during shutdown. The main `Run` loop correctly checked for and handled `nil` items, but the channel drain loop within the `shutdown` method did not. If a context cancellation or other shutdown signal occurred before the main loop could process the `nil` item, the shutdown logic would read the `nil` from the channel and attempt to call `item.finalize()`, resulting in a nil pointer dereference. This bug was exposed by a flaky test, which was difficult to reproduce locally because of its timing-dependent nature. The test enqueues a `nil` item and then uses a `time.Sleep(50ms)` to allow the processor's `Run` loop to handle it before the test harness is stopped. On a typical developer machine, 50ms is ample time for the `Run` loop to win this race. However, in a resource-constrained CI environment, goroutine scheduling can be less predictable. The `Run` loop could be delayed, allowing the 50ms sleep to expire and the test to initiate shutdown first. This triggered the panic, explaining why the flake appeared primarily in the CI/CD environment. This commit resolves the underlying bug by adding a `nil` check to the drain loop inside the `shutdown` method. This makes the shutdown process robust against this race condition, fixing the production code and stabilizing the test regardless of environment timing.
1 parent 58d15c8 commit 00887f6

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

pkg/epp/flowcontrol/controller/internal/processor.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,9 @@ func (sp *ShardProcessor) shutdown() {
549549
for {
550550
select {
551551
case item := <-sp.enqueueChan:
552+
if item == nil { // This is a safeguard against logic errors in the distributor.
553+
continue
554+
}
552555
item.finalize(types.QueueOutcomeRejectedOther,
553556
fmt.Errorf("%w: %w", types.ErrRejected, types.ErrFlowControllerShutdown))
554557
default:

0 commit comments

Comments
 (0)