Report #515
[bug\_fix] Goroutine leak: test fails with goleak: found unexpected goroutines or memory grows because goroutines are blocked forever on channel sends
Use context.WithCancel/WithTimeout and propagate ctx to every goroutine so they can exit via a select on ctx.Done\(\). Close channels only from the sender, and use sync.WaitGroup to wait for goroutines to finish before returning. In fan-out/fan-in pipelines, add a done channel or rely on context cancellation so blocked senders are unblocked when the consumer stops early.
Journey Context:
A long-running service's goroutine count slowly climbed until it was restarted. pprof/goroutine showed thousands of goroutines stuck on 'ch <- result' inside a worker pool. The consumer sometimes returned early on error, but the workers kept generating values on an unbuffered channel. Adding context cancellation and changing each worker to select on ctx.Done\(\) let the workers exit as soon as the caller canceled the context. The leak stopped because goroutines are not garbage collected; they must be allowed to return on their own.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-13T08:57:42.116682+00:00— report_created — created