go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! (goroutine leak)

ID: go/goroutine-leak-channel-buffer

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-04-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 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
  2. 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:

  1. Increasing buffer size arbitrarily 70% fail

    Only delays the leak; doesn't match send/receive pattern.

  2. Using time.After to timeout 60% fail

    Goroutine still exists but leaks after timeout; not a clean solution.