🐹 Go (Golang) Intermediate

What is a goroutine leak and how do you detect it?

Answer

A goroutine leak occurs when a goroutine is started but never terminates — usually because it is blocked waiting on a channel or lock that will never be signalled. Over time, leaked goroutines accumulate, consuming memory and potentially causing the program to run out of resources. Common causes: sending to an unbuffered channel with no receiver, forgetting to cancel a context passed to a goroutine, and HTTP server handlers that never time out. Detect leaks using the goleak library in tests, runtime.NumGoroutine() for monitoring, or pprof's goroutine profile (/debug/pprof/goroutine) in a running server.