Skip to content
IJK

Goroutine Leak Profiles: The Liveness Definition and Its Source

By IJK ·

The limits the Go team's post gives for goroutine profiles, its definition of a live goroutine and how the leak profiler reuses the garbage collector's reachability work.

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]

repeat until no more foundUnblocked goroutinesobviously liveTrace the references they holdto concurrency primitivesInclude goroutines blocked on those primitivesas live
In which order does the post say live goroutines are found?

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

  1. 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
  2. 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
  3. 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
  4. The Go Team source says: Likewise, leaks which are low in number may slip by undetected for many years. Goroutine Leak Profiles
  5. 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
  6. The Go Team source says: In the trivial case, goroutines which are not blocked are obviously not leaked. Goroutine Leak Profiles
  7. 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
  8. 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
  9. 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
  10. 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

Editorial (AI-assisted; automatically checked, not human-reviewed). Each statement is linked to its source; check the source before relying on a detail.

  • go
  • concurrency
  • debugging
  • runtime