go
runtime_error
ai_generated
true
fatal error: all goroutines are asleep - deadlock! (unbuffered channel send without receiver)
ID: go/unbuffered-channel-deadlock-single-goroutine
80%Fix Rate
85%Confidence
0Evidence
2024-10-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.1 | active | — | — | — |
Root Cause
A goroutine tries to send on an unbuffered channel without a corresponding receiver ready, causing it to block forever. If all goroutines are blocked, the program deadlocks.
generic中文
goroutine 尝试在无缓冲通道上发送,但没有相应的接收者准备好,导致其永远阻塞。如果所有 goroutine 都阻塞,程序将死锁。
Workarounds
-
95% success Ensure the receiver is ready before sending
ch := make(chan int) go func() { val := <-ch fmt.Println(val) }() ch <- 42 -
90% success Use a select with a default case to make send non-blocking
select { case ch <- 42: default: fmt.Println("no receiver, dropping message") }
Dead Ends
Common approaches that don't work:
-
Using a buffered channel with size 1 to avoid blocking
85% fail
A buffered channel can hold one value, but if the receiver never reads, the buffer will fill up and subsequent sends will block, leading to the same deadlock.
-
Adding a time.Sleep before sending to wait for receiver
90% fail
Sleep is unreliable; the receiver may not be ready in time, and it introduces unnecessary delays.