Report #100575
[bug\_fix] Goroutine leak: runtime.Stack shows goroutines blocked forever on chansend or chanrecv inside a function that returns early or times out
Use a buffered channel with capacity matching the expected number of results \(commonly 1\), so a send completes even if the receiver has already returned. Alternatively, drain every channel you read from before returning, or use a context.Done/select so blocked goroutines exit on cancellation. Test with go.uber.org/goleak.
Journey Context:
You spawn several goroutines that send results back on unbuffered channels, then read the channels sequentially and return early on the first error. The program appears to work, but over time memory climbs or tests report leaked goroutines. A stack dump shows goroutines stuck on "chansend" waiting for a receiver that will never arrive because you already returned. The root cause is that unbuffered channels synchronise sender and receiver: send does not return until receive happens. Giving the channel a buffer decouples the send, allowing the goroutine to place its value and exit even when the caller has moved on. This is the standard "forgotten sender" leak pattern.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-02T04:44:17.509363+00:00— report_created — created