Repository navigation
🐛 fix(collect,controller): stop dispatch workers on ctx cancel + thread reconcile ctx into preview (REL-03/REL-04) - #178
Conversation
… + thread reconcile ctx into preview (REL-03/REL-04)
There was a problem hiding this comment.
Pull request overview
This PR addresses two reliability audit findings by (1) ensuring collect engine dispatch workers stop cleanly on manager context cancellation, and (2) threading the reconcile context through the KollectInventory preview path so cancelled reconciles fail fast rather than continuing with context.Background().
Changes:
- Wire dispatch workers to exit on a cancellable
stopCtx, with new shutdown-focused tests to ensure workers terminate and in-flight work completes (REL-04). - Thread reconcile
ctxintopreviewAllSinksDebouncedand downstream client calls (scopeFloor/loadResolvedSink), plus a test asserting no live-context client calls when reconcile is pre-cancelled (REL-03). - Update existing debounce tests for the new
previewAllSinksDebounced(ctx, ...)signature.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/controller/kollectinventory_preview_ctx_test.go | Adds test to assert preview path passes the reconcile ctx into client List/Get calls. |
| internal/controller/kollectinventory_debounce_test.go | Updates callers to pass an explicit context into previewAllSinksDebounced. |
| internal/controller/kollectinventory_controller.go | Threads reconcile ctx through preview path and into scope/sink reads. |
| internal/collect/engine.go | Introduces stopCtx/stopFn + worker WaitGroup and updates dispatch worker loop to stop on cancellation. |
| internal/collect/engine_shutdown_test.go | Adds test to verify dispatch workers exit on ctx cancel, don’t drop in-flight job, and don’t panic with concurrent producers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| go func() { | ||
| <-ctx.Done() | ||
| e.stopFn() | ||
| }() |
| // Start is non-blocking (it is a manager Runnable): it wires the manager context into the | ||
| // engine's stop function via a goroutine so that cancelling ctx cancels stopCtx, which in | ||
| // turn drains and terminates every dispatch worker (REL-04). The wiring goroutine itself | ||
| // exits as soon as ctx is done, so it does not leak. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|



Audit finding HYG-01 (P3) — two reliability fixes:
REL-04: dispatch workers used
for job := range dispatchChwith no stop path → leaked under in-process restart. Now each worker selects on astopCtx(wired from the manager ctx) alongside the channel receive;dispatchChis never closed (no send-on-closed), producers unblock via the existing ctx.Done() backpressure, in-flight jobs complete (non-preemptible) then the worker exits. WaitGroup balanced viadispatchOnce.REL-03: the controller preview path threaded
context.Background()intoscopeFloor/loadResolvedSink; now threads the reconcile ctx so a cancelled/expired reconcile fails fast.TDD:
TestEngineDispatchWorkersStopOnContextCancel(-race ×5; proven to fail on the old loop; asserts in-flight non-drop + concurrent-producer no-panic) andTestPreviewAllSinksDebouncedThreadsReconcileContext(asserts 0 live-ctx downstream calls + cancelled List/Get surface).Independent review: APPROVE, no P0/P1 — shutdown deadlock/panic-free, workers provably exit, happy path unchanged. Non-blocking P3 follow-up logged: the currently-unreachable
Engine.Stop()has latent hazards if ever wired (delete-or-document precondition).Disjoint from other in-flight lanes.