Goroutine leak: a blocked send after ctx.Done()

Read the explanation
What if

Each handler gives up on ctx.Done(), but its worker still sends on an unbuffered channel nobody reads. Three cancelled requests leave three goroutines stuck forever.

0123456789101112request 1: selecthandler: request cancelledcancelrequest 2: selecthandler: request cancelledcancelrequest 3: selecthandler: request cancelledcanceltime.Sleep(100 ms)handler: print goroutines: 4printquery (50 ms)result <- "data": stuck foreverquery (50 ms)result <- "data": stuck foreverquery (50 ms)worker 3: result <- "data": stuck foreverstuck

Tap any block to see what that thread is doing.

  • Waiting / parked
  • Running
Step 1/5

Request 1: the handler starts a worker, then waits in select for the result or for ctx.Done().

1func worker(ctx context.Context, result chan<- string) {2    time.Sleep(50 * time.Millisecond) // a slow database query3    result <- "data"                  // waits for a reader that is gone4}5 6func handle(ctx context.Context) {7    result := make(chan string)8    go worker(ctx, result)9    select {10    case r := <-result:11        fmt.Println("got", r)12    case <-ctx.Done(): // the client gave up13        fmt.Println("request cancelled")14    }15}16 17func main() {18    for range 3 { // three requests, each with a 10 ms deadline19        ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)20        handle(ctx)21        cancel()22    }23    time.Sleep(100 * time.Millisecond) // every query has finished by now24    fmt.Println("goroutines:", runtime.NumGoroutine())25}

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

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