Skip to content

Fix send on closed channel panic from scheduled status checks - #45

Merged
stefanprodan merged 1 commit into
fluxcd:mainfrom
Adham-Kiwan:fix/status-check-send-on-closed-channel
Nov 26, 2026
Merged

stefanprodan merged 1 commit into
fluxcd:mainfrom
Adham-Kiwan:fix/status-check-send-on-closed-channel

Conversation

@Adham-Kiwan

Copy link
Copy Markdown

Fixes a send on closed channel panic in ObjectStatusReporter, reported by Helm users in helm/helm#32951.

Cause

When a watched object has an unschedulable Pod, the event handler calls taskManager.Schedule, which re-reads the object after status.ScheduleWindow in a new goroutine and sends the result on the informer's event channel. Nothing waited for that goroutine:

  • the sync path closed eventCh as soon as informer.Run returned;
  • the async path waited for status computations, but not for scheduled checks.

If the watch is stopped while a check is between reading the object and sending the result, it sends on a closed channel and the process panics.

Fix

Each informer now has one WaitGroup covering every goroutine, other than the informer's own, that can send on its channel. taskManager.Schedule registers its goroutine in it, and both paths wait before close(eventCh).

informer.Run only returns after its event handlers have returned, so no new check can be registered from a handler once the wait has started (in the async path, a check registered by an in-flight computation is added while the counter is still non-zero). Checks that have not fired exit as soon as the informer context is cancelled.

One behaviour change: shutdown now waits for a check that is already running. The read receives the cancelled context, so a context-aware client returns promptly.

Testing

  • Added TestStopWaitsForScheduledStatusCheck, covering the sync (workers=1) and async (workers=8) paths. Without the fix both fail with event channel closed while a status check was in flight. It takes about 15s because it waits for status.ScheduleWindow; the two cases run in parallel.
  • go test -race -count=3 ./pkg/kstatus/watcher/ -run TestStopWaitsForScheduledStatusCheck, make test, make lint and go vet ./pkg/... pass.

I have not reproduced the original crash on a live cluster; the test reproduces the ordering that leads to it.

AI disclosure

This change was written with the help of an AI agent (Claude Code), as noted in the Assisted-by commit trailer.

🤖 Generated with Claude Code

ObjectStatusReporter schedules a delayed status check for objects with
unschedulable Pods. That check runs in its own goroutine, which nothing
waited for before the informer's event channel was closed. If the watch
was stopped while the check was between reading the object and sending
the result, it panicked with "send on closed channel".

Track the scheduled check goroutines in a per-informer WaitGroup and
wait on it before closing the channel, in both the sync and async event
handler paths.

Assisted-by: claude-code/claude-sonnet-5-5
Signed-off-by: Adham Kiwan <adhamkiwan@outlook.com>
@Adham-Kiwan
Adham-Kiwan force-pushed the fix/status-check-send-on-closed-channel branch from 97bde2b to 14af06a Compare November 25, 2026 23:02
@stefanprodan stefanprodan added the bug Something isn't working label Nov 26, 2026

@stefanprodan stefanprodan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Thanks @Adham-Kiwan

@stefanprodan
stefanprodan merged commit 8db7de8 into fluxcd:main Nov 26, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants