go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! (receive from closed channel)

ID: go/channel-receive-from-closed-channel-ok

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-05-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active — — —

Root Cause

A goroutine is blocked receiving from a channel that is closed and empty, but other goroutines are also blocked, leading to a deadlock.

generic

中文

某个 goroutine 阻塞在从已关闭且为空的 channel 接收数据,而其他 goroutine 也处于阻塞状态,导致死锁。

Workarounds

  1. 95% success
    v, ok := <-ch
    if !ok {
        return
    }
  2. 98% success
    for v := range ch {
        // process v
    }

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Sleep does not unblock a deadlock; it only delays the inevitable fatal error

  2. 90% fail

    The deadlock is logical, not related to parallelism