go
runtime_error
ai_generated
true
fatal error: all goroutines are asleep - deadlock! (unbuffered channel send)
ID: go/goroutine-unbuffered-channel-blocking
80%Fix Rate
87%Confidence
0Evidence
2024-09-18First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.20 | active | — | — | — |
| 1.21 | active | — | — | — |
Root Cause
An unbuffered channel send blocks until a receiver is ready; if no receiver exists, the goroutine deadlocks.
generic中文
无缓冲通道发送会阻塞直到有接收者就绪;如果没有接收者,协程会死锁。
Workarounds
-
95% success Use a buffered channel to allow sends without immediate receiver.
ch := make(chan int, 1) ch <- 42 // non-blocking val := <-ch fmt.Println(val)
-
100% success Ensure a receiver goroutine is started before sending.
ch := make(chan int) go func() { val := <-ch fmt.Println(val) }() ch <- 42 // now receiver is ready
Dead Ends
Common approaches that don't work:
-
Using a buffered channel but with size 0, which is unbuffered.
90% fail
make(chan int, 0) creates an unbuffered channel, still blocking.
-
Adding a receiver in the same goroutine after the send.
100% fail
The send blocks before the receiver is reached, causing deadlock.