go
runtime_error
ai_generated
true
fatal error: all goroutines are asleep - deadlock! (goroutine leak)
ID: go/goroutine-leak-channel-buffer
80%Fix Rate
86%Confidence
0Evidence
2024-04-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.19 | active | — | — | — |
| 1.20 | active | — | — | — |
Root Cause
Goroutines are blocked forever because a channel send has no receiver and the channel is unbuffered.
generic中文
由于通道发送没有接收者且通道无缓冲,goroutine永远阻塞。
Workarounds
-
95% success Ensure every send has a corresponding receive
// Before: ch <- 1 (no reader) // After: result := make(chan int) go func() { result <- doWork() }() val := <-result -
90% success Use a context with cancellation to clean up goroutines
ctx, cancel := context.WithCancel(context.Background()) go func() { select { case ch <- val: case <-ctx.Done(): return } }() cancel() // cleanup
Dead Ends
Common approaches that don't work:
-
Increasing buffer size arbitrarily
70% fail
Only delays the leak; doesn't match send/receive pattern.
-
Using time.After to timeout
60% fail
Goroutine still exists but leaks after timeout; not a clean solution.