go runtime_error ai_generated true

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

fatal error: all goroutines are asleep - deadlock!

ID: go/deadlock-unbuffered-channel-single-goroutine

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

版本兼容性

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

根因分析

在没有接收方的同一 goroutine 中向无缓冲通道发送数据会永久阻塞,导致死锁。

English

Sending on an unbuffered channel without a receiver in the same goroutine blocks forever, causing a deadlock.

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 70% 失败

    Only works if the sender doesn't need to wait for a receiver; if the buffer fills, the same deadlock occurs.

  2. 90% 失败

    Sleeping doesn't create a receiver; the goroutine still blocks indefinitely.

  3. 100% 失败

    The program terminates immediately; no recovery is possible.