go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock!

ID: go/deadlock-on-unbuffered-channel-send

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.21 active

Root Cause

Sending on an unbuffered channel without a corresponding receiver in the same goroutine causes a deadlock.

generic

中文

在同一 goroutine 中向无缓冲通道发送数据而没有对应的接收者会导致死锁。

Workarounds

  1. 95% success Ensure a separate goroutine receives from the channel before sending
    go func() { <-ch }(); ch <- data
  2. 90% success Use a buffered channel with sufficient capacity
    ch := make(chan int, 1); ch <- 42

Dead Ends

Common approaches that don't work:

  1. Adding a small sleep before sending 90% fail

    Sleep does not create a receiver; the send still blocks indefinitely.

  2. Using a buffered channel with capacity 1 but still no receiver 80% fail

    Buffered channel only helps if capacity is not exceeded; if no receiver, the send blocks after buffer fills.