go runtime_error ai_generated true

致命错误:所有协程都处于休眠状态 - 死锁!(所有通道都为 nil 的 select)

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

ID: go/goroutine-select-nil-channel

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

版本兼容性

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

根因分析

一个 select 语句,其中所有 case 都涉及 nil 通道,导致 select 永远阻塞。

English

A select statement where all cases involve nil channels, causing the select to block forever.

generic

解决方案

  1. 100% 成功率 Initialize all channels before select
    ch1 := make(chan int)
    ch2 := make(chan int)
    select {
    case <-ch1:
    case <-ch2:
    }
  2. 90% 成功率 Use a non-nil channel as a sentinel
    ch := make(chan int)
    close(ch) // closed channel always returns zero value
    select {
    case <-ch:
    }

无效尝试

常见但无效的做法:

  1. Add a default case to select 50% 失败

    Default case will execute immediately, which may not be desired; also doesn't fix the nil channel issue.

  2. Check each channel for nil before select 60% 失败

    Can become cumbersome; still need to handle nil channels properly.