From 6ebc4ffa0423b0cf2793962b41420e05b8bffb5a Mon Sep 17 00:00:00 2001 From: clawbot Date: Mon, 2 Mar 2026 00:39:08 +0100 Subject: [PATCH] fix: use context.Background() for watcher goroutine lifetime (#63) ## Summary The `OnStart` hook previously derived the watcher's context from the fx startup context (`startCtx`) via `context.WithoutCancel()`. While `WithoutCancel` strips cancellation and deadline, using `context.Background()` makes the intent explicit: the watcher's monitoring loop must outlive the fx startup phase and is controlled solely by the `cancel` func called in `OnStop`. ## Changes - Replace `context.WithCancel(context.WithoutCancel(startCtx))` with `context.WithCancel(context.Background())` - Add explanatory comment documenting why the watcher context is not derived from the startup context - Unused `startCtx` parameter changed to `_` Closes #53 Co-authored-by: clawbot Co-authored-by: Jeffrey Paul Reviewed-on: https://git.eeqj.de/sneak/dnswatcher/pulls/63 Co-authored-by: clawbot Co-committed-by: clawbot --- internal/watcher/watcher.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index f641b70..60b70ba 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -72,13 +72,15 @@ func New( } lifecycle.Append(fx.Hook{ - OnStart: func(startCtx context.Context) error { - ctx, cancel := context.WithCancel( - context.WithoutCancel(startCtx), - ) + OnStart: func(_ context.Context) error { + // Use context.Background() — the fx startup context + // expires after startup completes, so deriving from it + // would cancel the watcher immediately. The watcher's + // lifetime is controlled by w.cancel in OnStop. + ctx, cancel := context.WithCancel(context.Background()) w.cancel = cancel - go w.Run(ctx) + go w.Run(ctx) //nolint:contextcheck // intentionally not derived from startCtx return nil },