go runtime_error ai_generated true

致命错误:所有 goroutine 都处于休眠状态 - 死锁!(选择包含 nil 通道)

fatal error: all goroutines are asleep - deadlock! (select with nil channels)

ID: go/select-on-nil-channels

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

版本兼容性

版本状态引入弃用备注
1.0 active
1.1 active

根因分析

一个选择语句有多个 case,但所有通道都是 nil。在 Go 中,所有通道都是 nil 的选择会永远阻塞,因为没有 case 可以执行。

English

A select statement with multiple cases where all channels are nil. In Go, a select with all nil channels blocks forever because no case can proceed.

generic

解决方案

  1. 95% 成功率 Ensure all channels in select are non-nil
    ch1 := make(chan int)
    ch2 := make(chan int)
    select {
    case <-ch1:
    case <-ch2:
    }
  2. 90% 成功率 Use a helper function to filter out nil channels
    func nonNilChannels(chs ...chan int) []chan int {
        var result []chan int
        for _, ch := range chs {
            if ch != nil {
                result = append(result, ch)
            }
        }
        return result
    }

无效尝试

常见但无效的做法:

  1. Adding a default case to the select 80% 失败

    A default case will execute immediately if all channels are nil, but it doesn't solve the underlying issue of nil channels; it just avoids the block.

  2. Removing the select and using direct channel operations 95% 失败

    Direct operations on nil channels will also block forever, so this doesn't help.