go runtime_error ai_generated true

fatal error: 所有 goroutine 都休眠 - 死锁!(nil channel 接收)

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

ID: go/fatal-error-all-goroutines-are-asleep-deadlock-nil-chan

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-01-17首次发现

版本兼容性

版本状态引入弃用备注
1.16 active — — —
1.19 active — — —
1.22 active — — —

根因分析

在 nil channel 上接收或发送会永久阻塞;声明但未用 make 初始化的 channel 即为 nil。

English

Receiving or sending on a nil channel blocks forever; a channel declared but never initialized with make is nil.

generic

解决方案

  1. 98% 成功率
    ch := make(chan int, 1)
    ch <- 1
    v := <-ch
  2. 90% 成功率
    if ch == nil {
        return errors.New("channel not initialized")
    }

无效尝试

常见但无效的做法:

  1. 80% 失败

    A nil channel case never becomes ready, so select with default just spins or returns default, never delivering data.

  2. 95% 失败

    Closing a nil channel panics with 'close of nil channel'.