go runtime_error ai_generated true

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

ID: go/goroutine-leak-on-channel-block

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.21 active

Root Cause

Goroutines are blocked waiting on channel operations that never complete, leading to a deadlock.

generic

中文

Goroutine 在等待永远不会完成的通道操作时被阻塞,导致死锁。

Workarounds

  1. 90% success Use context with timeout to unblock goroutines
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()
    select {
    case <-ctx.Done():
        return
    case ch <- data:
    }
  2. 95% success Ensure every send has a corresponding receive in a separate goroutine
    go func() { for v := range ch { process(v) } }()
    ch <- data

Dead Ends

Common approaches that don't work:

  1. Adding more goroutines to unblock 90% fail

    More goroutines may also block if they depend on the same channels.

  2. Increasing channel buffer size 80% fail

    Buffering delays but does not solve the underlying missing consumer/producer.