Goroutine leaks occur when goroutines block on concurrency primitives or OS operations in a way that prevents them from ever unblocking, gradually degrading memory and CPU performance. Existing tools like goleak and synctest improve unit testing but cannot reliably find leaks in production. Go 1.27 adds a goroutine leak profiler that inspects running programs (including production) to precisely identify goroutines permanently blocked on channels and sync package primitives (Mutex, RWMutex, WaitGroup, Cond, blocking select). The profiler produces a specialized goroutineleak profile that integrates with pprof and yields few false positives by focusing on the common, diagnosable class of leaks while excluding non-standard blocking (file/network IO, syscalls, and user-defined primitives).
The profiler is implemented by adapting the garbage collector’s marking phase: it treats only unblocked goroutines as initial roots, marks objects reachable from them, then iteratively promotes blocked goroutines that are blocked on marked concurrency primitives as live; any blocked goroutine not thus promoted is reported leaked. This approach is efficient in typical cases but has limits: primitives reachable via globals or runnable goroutines hide leaks; only first-class concurrency primitives are considered; detection is post hoc, and certain pathological topologies (a daisy-chain of references) increase GC time and can yield O(n²) inspection cost. The recommended workflow combines periodic production profiling with test-time tools to ensure comprehensive detection.
Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.