05 / 10

What is a goroutine leak and how do you detect and prevent one?

A goroutine leak occurs when a goroutine blocks indefinitely — usually waiting on a channel or lock that is never resolved — consuming memory and system resources silently over time.

Common causes of goroutine leaks
  1. 1

    Goroutine waiting on a channel that is never written to or closed

  2. 2

    Missing context cancellation — goroutine has no way to know the request was cancelled

  3. 3

    Infinite loop without a done signal

  4. 4

    Deadlocked mutual sends — two goroutines each waiting for the other to receive

  5. 5

    HTTP response body not read and closed — underlying goroutine in transport stays alive

Detection and prevention