go
runtime_error
ai_generated
true
fatal error: all goroutines are asleep - deadlock!
ID: go/deadlock-on-unbuffered-channel-send
80%Fix Rate
85%Confidence
0Evidence
2024-01-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
95% success Ensure a separate goroutine receives from the channel before sending
go func() { <-ch }(); ch <- data -
90% success Use a buffered channel with sufficient capacity
ch := make(chan int, 1); ch <- 42
Dead Ends
Common approaches that don't work:
-
Adding a small sleep before sending
90% fail
Sleep does not create a receiver; the send still blocks indefinitely.
-
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.