Skip to content

🐛 fix(collect,controller): stop dispatch workers on ctx cancel + thread reconcile ctx into preview (REL-03/REL-04) - #178

Merged
konih merged 1 commit into
mainfrom
fix/hyg-01-engine-shutdown-preview-ctx
Aug 3, 2026
Merged

konih merged 1 commit into
mainfrom
fix/hyg-01-engine-shutdown-preview-ctx

Conversation

@konih

@konih konih commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Audit finding HYG-01 (P3) — two reliability fixes:

REL-04: dispatch workers used for job := range dispatchCh with no stop path → leaked under in-process restart. Now each worker selects on a stopCtx (wired from the manager ctx) alongside the channel receive; dispatchCh is 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 via dispatchOnce.

REL-03: the controller preview path threaded context.Background() into scopeFloor/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) and TestPreviewAllSinksDebouncedThreadsReconcileContext (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.

… + thread reconcile ctx into preview (REL-03/REL-04)
Copilot AI review requested due to automatic review settings August 3, 2026 13:52
@konih
konih merged commit a3f8008 into main Aug 3, 2026
23 checks passed
@konih
konih deleted the fix/hyg-01-engine-shutdown-preview-ctx branch August 3, 2026 13:52

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 ctx into previewAllSinksDebounced and 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.

Comment on lines +437 to +440
go func() {
<-ctx.Done()
e.stopFn()
}()
Comment on lines +429 to +432
// 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

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.42857% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/collect/engine.go 68.00% 7 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@sonarqubecloud

sonarqubecloud Bot commented Aug 3, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants