go runtime_error ai_generated true

fatal error: 所有 goroutine 都休眠了 - 死锁!(向 nil channel 发送)

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

ID: go/send-on-nil-channel-blocks

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

版本兼容性

版本状态引入弃用备注
1.18 active
1.22 active

根因分析

向 nil channel 发送或接收会永久阻塞。如果所有 goroutine 都这样,运行时会报告死锁。

English

Sending to or receiving from a nil channel blocks forever. If all goroutines do this, the runtime reports deadlock.

generic

解决方案

  1. 95% 成功率
    ch := make(chan int)
  2. 85% 成功率
    if ch == nil { return }
    ch <- v

无效尝试

常见但无效的做法:

  1. 70% 失败

    Default turns a blocking send into a busy-loop or dropped message, not a fix for the nil channel.

  2. 85% 失败

    More goroutines blocked on nil channel just produce the same deadlock.