go runtime_error ai_generated true

致命错误:所有 goroutine 都处于休眠状态 - 死锁!

fatal error: all goroutines are asleep - deadlock!

ID: go/channel-deadlock-no-receiver

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

版本兼容性

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

根因分析

向无缓冲 channel 发送数据但没有并发的接收方,导致唯一的 goroutine 阻塞,没有任何可运行的 goroutine,从而触发运行时死锁检测。

English

Sending to an unbuffered channel with no concurrent receiver blocks the only goroutine, leaving no runnable goroutines and triggering the runtime deadlock detector.

generic

解决方案

  1. 95% 成功率
    ch := make(chan int)
    go func() { fmt.Println(<-ch) }()
    ch <- 42
  2. 90% 成功率
    ch := make(chan int, 1)
    ch <- 42 // does not block
    fmt.Println(<-ch)

无效尝试

常见但无效的做法:

  1. 70% 失败

    Still no receiver ever reads, the spawned goroutine blocks forever and the main goroutine exits or deadlocks elsewhere.

  2. 85% 失败

    Sleep does not create a receiver; if no goroutine reads, the send still blocks permanently.