go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock!

ID: go/all-goroutines-asleep-deadlock

Also available as: JSON · Markdown · 中文
80%Fix Rate
89%Confidence
0Evidence
2024-01-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —
1.22 active — — —

Root Cause

Main goroutine blocks on a channel receive/send that no other goroutine can satisfy; runtime detects total inactivity and aborts.

generic

中文

主 goroutine 阻塞在没有任何其他 goroutine 能满足的 channel 接收/发送上;运行时检测到完全无活动后中止。

Workarounds

  1. 92% success
    ch := make(chan int, 1)
    ch <- 42 // non-blocking with buffer
    v := <-ch
    _ = v
  2. 90% success
    select {
    case v := <-ch:
        _ = v
    case <-time.After(2 * time.Second):
        return errors.New("timeout waiting for value")
    }

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Sleep does not create a sender; deadlock still occurs and adds nondeterminism.

  2. 99% fail

    Deadlock is a fatal runtime error, not a panic; recover never runs.