By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A service's memory climbs slowly and never comes back down. Goroutine count rises with traffic and does not fall when traffic stops. The process is not crashing and the CPU is fine, which is why the problem survives in production: everything looks healthy until the OOM killer disagrees. A goroutine leak is almost always a goroutine blocked forever on a channel, a lock or a network read with no cancellation path.
The lab has two halves. First, reproduce the leak with a worker that ignores its context and blocks on a channel nobody will write to. Second, prove the fix: the worker must own a decrement of an active-goroutine counter, and cancellation must return it to zero. The counter is the assertion you can put in a test; a raw runtime.NumGoroutine() baseline is the production version of the same idea.
ctx and starts with active.Add(1) / defer active.Add(-1).ctx.Done() instead of an unbuffered channel.<-ctx.Done() in a scratch copy and watch the count stay at one — that is the leak.runtime.NumGoroutine() stays bounded.package main
import (
"context"
"fmt"
"sync"
"sync/atomic"
)
func worker(ctx context.Context, active *atomic.Int64, ready chan<- struct{}) {
active.Add(1)
defer active.Add(-1)
close(ready)
<-ctx.Done()
}
func main() {
var active atomic.Int64
ctx, cancel := context.WithCancel(context.Background())
ready := make(chan struct{})
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
worker(ctx, &active, ready)
}()
<-ready
fmt.Println("active while running:", active.Load())
cancel()
wg.Wait()
fmt.Println("active after cancel:", active.Load())
}When you suspect a leak, take a goroutine profile before restarting. runtime/pprof and net/http/pprof expose the stack of every goroutine, grouped by state; the top of the list is usually a chan receive, semacquire or select frame pointing at the exact line that forgot a cancellation case. The profile also shows how many goroutines share each stack, which turns a guess into a count.
| Leak signature | Blocked on | Usual fix |
|---|---|---|
chan receive growing with traffic | Unbuffered channel with no writer | Close the channel or select on ctx.Done() |
semacquire on a mutex | Lock never released on an error path | defer mu.Unlock() immediately after Lock |
select with ctx.Done() never closed | Context not cancelled or wrong parent | defer cancel() on every derived context |
http client read | No client or request timeout | Set Client.Timeout and a request context |
sync.WaitGroup.Wait | wg.Add and wg.Done mismatch | Add before go, defer wg.Done() inside |
Key takeaways
Goroutine leaks are blocked goroutines with no cancellation or join path.
Give every goroutine an owner, a cancel signal and a join.
Put leak assertions in tests with an active-goroutine counter or a bounded runtime.NumGoroutine delta.
Capture the goroutine profile before restarting a leaking process.
Blocked-on stack signatures map to standard fixes: close channels, defer unlocks, cancel contexts.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.