go runtime_error ai_generated true

致命错误:所有 goroutine 都处于休眠状态 - 死锁!(goroutine 1 [select(无 case)])

fatal error: all goroutines are asleep - deadlock! (goroutine 1 [select (no cases))])

ID: go/select-on-nil-channel

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

版本兼容性

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

根因分析

所有通道都是 nil 的 select 语句会永远阻塞,因为 nil 通道永远不会就绪。

English

A select statement with all nil channels blocks forever because nil channels are never ready.

generic

解决方案

  1. 95% 成功率
    ch1 := make(chan int)
    ch2 := make(chan int)
    select {
    case v := <-ch1:
        //
    case v := <-ch2:
        //
    }
  2. 90% 成功率
    select {
    case v := <-ch:
        //
    case <-time.After(time.Second):
        // timeout
    }

无效尝试

常见但无效的做法:

  1. 70% 失败

    Default case may execute incorrectly and not handle the intended communication.

  2. 90% 失败

    Sleeping doesn't resolve the block; the select still blocks.