go runtime_error ai_generated true

fatal error:所有 goroutine 都休眠——死锁!(select 无就绪分支)

fatal error: all goroutines are asleep - deadlock! (select with no ready case)

ID: go/select-no-default-blocks

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

版本兼容性

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

根因分析

没有 default 的 select 语句会阻塞直到某个 case 就绪。如果没有任何 channel 就绪,程序死锁。

English

A select statement with no default blocks until one case becomes ready. If no channel ever becomes ready, the program deadlocks.

generic

解决方案

  1. 95% 成功率
    select {
    case v := <-ch:
        use(v)
    case <-time.After(5 * time.Second):
        return errTimeout
    }
  2. 96% 成功率
    select {
    case v := <-ch:
        use(v)
    case <-ctx.Done():
        return ctx.Err()
    }

无效尝试

常见但无效的做法:

  1. 70% 失败

    The select now spins in a busy loop consuming CPU without ever making progress.

  2. 80% 失败

    Creates a livelock if no case is ever ready; the program hangs forever.