go runtime_error ai_generated true

致命错误:所有协程都在休眠 - 死锁!

fatal error: all goroutines are asleep - deadlock!

ID: go/goroutine-leak-on-missed-channel

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

版本兼容性

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

根因分析

一个协程向无缓冲通道发送数据,但没有接收者读取,导致协程永久阻塞。主协程也在等待其他操作,引发死锁。

English

A goroutine sends to an unbuffered channel but no receiver ever reads, causing the goroutine to block forever. The main goroutine also waits on another operation, leading to a deadlock.

generic

解决方案

  1. 70% 成功率
    select { case ch <- v: default: /* 处理超时 */ }
  2. 90% 成功率
    ch := make(chan int, 10); go func(){ v := <-ch; /* 处理 */ }(); ch <- 42

无效尝试

常见但无效的做法:

  1. 80% 失败

    缓冲只能容纳一个值,如果接收者仍然缺失,发送第二个值时会再次阻塞。

  2. 90% 失败

    sleep只是暂时缓解,如果接收者一直不出现,最终仍会死锁。