Report #97738
[bug\_fix] goroutine leak: test timed out after 30s; goroutine dump shows hundreds of blocked goroutines on a channel send
Add cancellation to the producer via context.WithCancel or context.WithTimeout, and make the worker select on ctx.Done\(\) so it stops when the consumer is gone. Alternatively, use a buffered channel or a sync.WaitGroup with explicit close semantics.
Journey Context:
A background worker sends results on an unbuffered channel. In tests the consumer reads once and exits, but the producer goroutine keeps running and blocks forever on the send. Over many tests the goroutine count climbs and eventually the test suite times out. You run go test -race and see nothing obvious because there is no data race, just a leaked goroutine. You add a goroutine dump on timeout and spot the blocked sends. Reading the Go concurrency patterns blog you realize the producer should listen for cancellation. You pass a context into the worker, select on ctx.Done\(\) alongside the send, and defer cancel\(\) in the test. The goroutine count stays flat and the suite runs in seconds again.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-26T04:36:59.759966+00:00— report_created — created