go runtime_error ai_generated true

致命错误:所有 goroutine 都在休眠 - 死锁!(阻塞在 select {} 中)

fatal error: all goroutines are asleep - deadlock! (blocked in select {})

ID: go/select-no-cases-blocks

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

版本兼容性

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

根因分析

空的 select{} 会永久阻塞当前 goroutine,若它是唯一 goroutine,运行时判定死锁。

English

An empty select{} blocks the current goroutine forever, and if it is the only goroutine, the runtime deadlocks.

generic

解决方案

  1. 95% 成功率
    ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
    defer stop()
    <-ctx.Done()
  2. 90% 成功率
    done := make(chan struct{})
    go func(){ /* work */ close(done) }()
    <-done
  3. 93% 成功率
    for {
        select {
        case <-ctx.Done(): return
        case msg := <-msgs: handle(msg)
        }
    }

无效尝试

常见但无效的做法:

  1. 85% 失败

    Busy-waits and burns CPU; still no way to exit cleanly.

  2. 95% 失败

    Same deadlock, just a spin loop; runtime still aborts if no other goroutine.

  3. 80% 失败

    Moves the block elsewhere; leaked goroutine and no shutdown path.