What profiles cannot tell you
Unfortunately, neither approach can check for goroutine leaks in production systems, especially at larger scales, which may behave in ways unaccounted for by tests. [1]
Goroutine profiles are a rudimentary way to check for operations that block too many goroutines, or analyze growth trends. [2]
However, goroutine profiles cannot distinguish between goroutines which are leaked, and those which are temporarily blocked in high numbers by design, e.g., as caused by increased traffic in a microservice. [3]
Likewise, leaks which are low in number may slip by undetected for many years. [4]
The liveness definition
Let’s start with an initial observation: if a goroutine is blocked over some concurrency primitive that no other goroutine has access to (in this case, via a reference in memory), then it is obviously leaked. [5]
In the trivial case, goroutines which are not blocked are obviously not leaked. [6]
In the inductive case, the underlying assumption is that any goroutine which is not leaked may eventually use concurrency primitives it references to unblock any other goroutines blocked by those primitives. [7]
Reusing the garbage collector
To find all live goroutines, we start from the obviously live unblocked goroutines and trace any references they hold, i.e., through their local variables, to find the concurrency primitives they have access to. [8]
We then incrementally include any goroutines blocked over those primitives as live, and repeat the process until no additional live goroutines are discovered. [9]
Fortunately for us, the Go runtime already computes memory reachability through the garbage collector (GC), so the next step is to adapt the GC to suit our purposes. [10]
Sources
- The Go Team source says: Unfortunately, neither approach can check for goroutine leaks in production systems, especially at larger scales, which may behave in ways unaccounted for by tests. Goroutine Leak Profiles
- The Go Team source says: Goroutine profiles are a rudimentary way to check for operations that block too many goroutines, or analyze growth trends. Goroutine Leak Profiles
- The Go Team source says: However, goroutine profiles cannot distinguish between goroutines which are leaked, and those which are temporarily blocked in high numbers by design, e.g., as caused by increased traffic in a microservice. Goroutine Leak Profiles
- The Go Team source says: Likewise, leaks which are low in number may slip by undetected for many years. Goroutine Leak Profiles
- The Go Team source says: Let’s start with an initial observation: if a goroutine is blocked over some concurrency primitive that no other goroutine has access to (in this case, via a reference in memory), then it is obviously leaked. Goroutine Leak Profiles
- The Go Team source says: In the trivial case, goroutines which are not blocked are obviously not leaked. Goroutine Leak Profiles
- The Go Team source says: In the inductive case, the underlying assumption is that any goroutine which is not leaked may eventually use concurrency primitives it references to unblock any other goroutines blocked by those primitives. Goroutine Leak Profiles
- The Go Team source says: To find all live goroutines, we start from the obviously live unblocked goroutines and trace any references they hold, i.e., through their local variables, to find the concurrency primitives they have access to. Goroutine Leak Profiles
- The Go Team source says: We then incrementally include any goroutines blocked over those primitives as live, and repeat the process until no additional live goroutines are discovered. Goroutine Leak Profiles
- The Go Team source says: Fortunately for us, the Go runtime already computes memory reachability through the garbage collector (GC), so the next step is to adapt the GC to suit our purposes. Goroutine Leak Profiles