go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock!

ID: go/goroutine-leak-on-channel-send

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.21 active
1.22 active

Root Cause

A goroutine is blocked trying to send on an unbuffered channel with no receiver, causing a deadlock when the main goroutine also blocks.

generic

中文

一个 goroutine 尝试向无缓冲通道发送数据,但没有接收者,当主 goroutine 也阻塞时导致死锁。

Workarounds

  1. 95% success
    go func() { ch := make(chan int); go func() { <-ch }(); ch <- 42 }()
  2. 85% success
    select { case ch <- 42: default: }

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Sleep doesn't create a receiver; the channel send still blocks indefinitely.

  2. 70% fail

    If the buffer is full and no receiver, send still blocks after filling buffer.

  3. 90% fail

    Sending on a closed channel causes a panic, not a deadlock.