Skip to content

Troubleshooting guide

Goroutine leaks in Go: how to find and fix them

A goroutine that can never finish keeps its stack and everything it references. The count rises until memory runs out.

3 min read10 sectionsUpdated

On this page

Key points

  • A steadily rising count is the signal.
  • Goroutines blocked on channels are the classic leak.
  • Every goroutine needs a way to stop, usually a context.
  • goleak catches them in tests.

What a leak is

Go does not collect a goroutine blocked forever. Its stack begins small, but what it references is retained. Thousands of leaked goroutines are memory growth with a very clear signature in pprof.

Common causes

  • Sending on a channel nobody receives from, often after a timeout returned early.
  • Receiving on a channel that is never closed.
  • No context cancellation.
  • HTTP response bodies that are never closed.
  • time.Ticker never stopped.

Diagnose it

Expose pprof on an internal port, and look at goroutines grouped by stack.

bash
// In main.go, on an internal-only port
import _ "net/http/pprof"
go http.ListenAndServe("127.0.0.1:6060", nil)

# Grouped stacks with counts
curl -s 'localhost:6060/debug/pprof/goroutine?debug=1' | head -60

# Compare two points in time
go tool pprof -base goroutine-1.pb.gz goroutine-2.pb.gz

# Interactive view
go tool pprof -http=:8081 'http://localhost:6060/debug/pprof/goroutine'

Fixes

Use a buffered channel of size 1 when a worker may outlive its caller, so that its send cannot block forever. Use select with <-ctx.Done() in every blocking operation. Close response bodies using defer resp.Body.Close(). Call ticker.Stop(). Limit concurrency with errgroup and SetLimit. When a leak is found, write a test that reproduces it: start the operation, cancel it, and confirm that the goroutine count returns to where it was. This keeps the same bug from coming back in a later refactor.

The timeout trap

A common leak looks harmless. A function starts a goroutine to do work and send the result on an unbuffered channel, then uses select with a timeout. If the timeout wins, the function returns, and no one receives. The goroutine stays blocked on its send forever, so every timed-out request leaks one. It shows up precisely when a dependency slows down, which turns a brief slowdown into a slow memory leak. Giving the channel a buffer of one fixes it, because the send then always succeeds even with no reader.

Catch it automatically

Export runtime.NumGoroutine() and alert on growth over hours. Add go.uber.org/goleak to tests so that leaks fail the build. Go performance monitoring charts goroutines beside memory and latency.

Frequently asked questions

How many goroutines are too many?

There is no fixed number. The important thing is a count that keeps rising.

Is pprof safe in production?

Yes, on an internal-only port. Never expose it publicly.

Do goroutines get garbage collected?

Not while blocked. They must exit.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.