go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock!

ID: go/channel-deadlock-no-receiver

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

Version Compatibility

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

Root Cause

Sending to an unbuffered channel with no concurrent receiver blocks the only goroutine, leaving no runnable goroutines and triggering the runtime deadlock detector.

generic

中文

向无缓冲 channel 发送数据但没有并发的接收方,导致唯一的 goroutine 阻塞,没有任何可运行的 goroutine,从而触发运行时死锁检测。

Workarounds

  1. 95% success
    ch := make(chan int)
    go func() { fmt.Println(<-ch) }()
    ch <- 42
  2. 90% success
    ch := make(chan int, 1)
    ch <- 42 // does not block
    fmt.Println(<-ch)

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Still no receiver ever reads, the spawned goroutine blocks forever and the main goroutine exits or deadlocks elsewhere.

  2. 85% fail

    Sleep does not create a receiver; if no goroutine reads, the send still blocks permanently.