go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock!

ID: go/channel-deadlock-all-goroutines-asleep

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-02-11First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.16 active
1.20 active
1.22 active

Root Cause

The main goroutine blocked forever on a channel send/receive with no other goroutine able to unblock it. Common when receiving from an unbuffered channel with no sender, or sending with no receiver.

generic

中文

主 goroutine 在通道发送/接收上永久阻塞,且没有其他 goroutine 能够解除阻塞。常见于从无发送者的无缓冲通道接收,或向无接收者的通道发送。

Workarounds

  1. 90% success
    ch := make(chan int, 1)
    ch <- 1 // non-blocking
    v := <-ch
  2. 85% success
    select {
    case v := <-ch:
        use(v)
    case <-time.After(2 * time.Second):
        return errors.New("timeout")
    }

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Buffering only delays the deadlock. Once the buffer fills, the same blocking occurs; if the logic is fundamentally single-goroutine, it deadlocks regardless.

  2. 85% fail

    Sleeps don't create concurrency; if there is no sender goroutine at all, no amount of waiting helps.