Report #47736
[bug\_fix] goleak: Errors on leaked goroutines
Provide a mechanism for the producer goroutine to detect that the consumer has stopped reading, typically by using a context.Context and selecting on ctx.Done\(\).
Journey Context:
A developer implements a producer-consumer pattern where a background goroutine continuously sends data on an unbuffered channel. The consumer reads from this channel, but encounters an error and returns early. Because the channel is unbuffered, the producer blocks forever waiting for a receiver that no longer exists, causing a goroutine leak. This often goes unnoticed until CI runs with go.uber.org/goleak or the application eventually runs out of memory. The root cause is the lack of a cancellation signal. The fix is to pass a context.Context to the producer and use a select statement: select \{ case ch <- data: case <-ctx.Done\(\): return \}. When the consumer encounters an error, it cancels the context, unblocking the producer and allowing it to exit cleanly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T10:36:43.150067+00:00— report_created — created