A goroutine leak happens when a goroutine is started but can never finish, usually because it is blocked forever on a channel, a lock or a network call. The Go garbage collector never reclaims a blocked goroutine or anything it references, so leaks slowly grow memory and scheduler load until the service degrades or is killed. You prevent them by giving every goroutine a clear exit path, normally through context.Context.
The rule of thumb is simple: before you write go f(), know exactly how and when f will stop.
Here is a classic leak. The function launches a worker that sends on an unbuffered channel, but the caller returns early on timeout and nobody ever receives.
Every call leaks one goroutine stuck on ch <- fetch(). After 100 calls, 100 goroutines remain, plus main. In a server handling thousands of requests an hour, this adds up quickly.
| Cause | Example |
|---|---|
| Send with no receiver | The case above, or a consumer that stopped reading |
| Receive that never completes | for v := range ch when the producer never calls close(ch) |
| Missing cancellation | A loop with time.Ticker and no way to stop |
| Blocking I/O without a deadline | HTTP calls, database queries or sockets with no timeout |
Forgotten sync.WaitGroup.Done or lock release | Waiters stay blocked on Wait or Lock |
| Unstopped timers and tickers | Missing ticker.Stop() in older code |
When exactly one value is sent and the receiver may leave, give the channel capacity of one. The send then succeeds immediately and the goroutine exits.
The result is thrown away on timeout, and the garbage collector reclaims the channel once both sides are gone.
For work that can be interrupted, pass a context and select on its Done channel everywhere the goroutine can block.
When the timeout fires, ctx.Done() closes, the worker leaves its loop and logs that it stopped. Always defer cancel() right after creating a context, and pass the same context to HTTP requests (http.NewRequestWithContext) and database calls (QueryContext) so I/O stops too.
The sender owns a channel and closes it when done; receivers can then use range. Use sync.WaitGroup, or errgroup from golang.org/x/sync, to wait for all goroutines before returning. Since Go 1.25 you can also write wg.Go(func() { ... }), which handles Add and Done for you.
A function that cannot return until its goroutines exit cannot leak them.
runtime.NumGoroutine() or the go_goroutines metric exported by Prometheus. A count that only climbs is the signature of a leak.net/http/pprof with curl localhost:6060/debug/pprof/goroutine?debug=2, then group by identical stacks to find where they are blocked.go.uber.org/goleak with goleak.VerifyNone(t) or goleak.VerifyTestMain(m), which fail the test when goroutines outlive it.GOEXPERIMENT=goroutineleakprofile; treat it as optional tooling, since it may change.Goroutine stacks show state labels such as chan send, chan receive and select (no cases), which tell you immediately what each one waits for.
No. A blocked goroutine is still considered live, so it and every object it references stay in memory.
A goroutine starts with a few kilobytes of stack, plus whatever it references. One is cheap, but thousands per hour, each holding buffers or connections, turn into real problems.
At program exit yes, since all goroutines die with the process. In long-running services it is a leak, and in tests it can break later cases or race with the next test.
No. A buffer of one is a targeted fix for a single-result handoff. A large buffer only delays the symptom and hides back-pressure problems.
A goroutine leaks when it blocks with no way out. Give every goroutine an exit through context cancellation, closed channels or buffered handoffs, wait for them with WaitGroup or errgroup, put deadlines on I/O and watch the goroutine count in production. Add goleak to tests so regressions are caught before release.