Explanation
What just happened
Each handler waited for its worker’s result or for ctx.Done(). The 10 ms deadline came first, so the handler gave up and returned. Its worker finished the query later and tried to send on an unbuffered channel that nobody would ever read again. Three requests left three goroutines stuck in [chan send].
Try the what-if switch
- send in a select: the send also watches
ctx.Done(), so the worker exits when its caller has gone. - buffer of 1: the answer fits in the channel’s buffer, so the send never waits.
Why
An unbuffered send hands a value straight to a receiver, so both have to be there. When the receiver returns early, the sender has nobody to hand it to, and waits forever.
How to fix it
Give every send a way out: select { case result <- r: case <-ctx.Done(): }. A buffer of 1 also works when exactly one value is ever sent.
Watch out
It’s silent. A server keeps running while its goroutine count climbs. Watch runtime.NumGoroutine(), and read a goroutine dump: a pile of [chan send] points at this bug. The select timeout page shows the same leak with time.After.
Common questions
What is a goroutine leak?
A goroutine that never ends, because it waits for something that will never happen: here, a send nobody will receive. Go reports nothing: no error, and the program exits normally.
How do I find leaked goroutines?
Count them and look at what they wait on. Here runtime.NumGoroutine() grew by one with every cancelled request, and a goroutine dump showed each leaked worker in [chan send].
How do I fix this leak?
Send inside a select that also watches ctx.Done(), so the worker gives up with its caller, or give the channel a buffer of 1, so the send never waits. Either way the count went back to where it started.
Why doesn't Go report it like a deadlock?
Because main can still run. Go stops a program only when every goroutine is blocked, as in [all goroutines are asleep](/go/all-goroutines-asleep/).
Verified: this timeline matches a real program, checked in CI on Go 1.26 and 1.27. How we verify. Unclear or wrong? Tell us