What is a 'goroutine leak' and how can you prevent it?
Last updated
A goroutine leak is a goroutine that never terminates because it blocks forever (on a channel, lock, or select). Its stack and resources are never reclaimed, so memory grows under load.
Typical cause: sending to a channel nobody receives from (or receiving from one nobody sends to), often after the caller returned early.
Prevent it by giving every blocking goroutine an exit: select on
ctx.Done(), guarantee receivers or close channels, and detect leaks withgoleakorruntime.NumGoroutine().