go runtime_error ai_generated true

致命错误: 所有 goroutine 都处于休眠状态 - 死锁!(空 select{})

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

ID: go/select-no-cases-blocks-forever

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

版本兼容性

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

根因分析

空 select{} 永久阻塞 goroutine。若为主 goroutine 且无其他进度,运行时会报告死锁。

English

An empty select{} blocks the goroutine forever. If it's the main goroutine and nothing else can make progress, the runtime reports deadlock.

generic

解决方案

  1. 90% 成功率
    Block on a signal channel instead of empty select:
    
    quit := make(chan os.Signal, 1)
    signal.Notify(quit, os.Interrupt)
    <-quit
  2. 92% 成功率
    Use sync.WaitGroup to wait for real work completion:
    
    wg.Wait()

无效尝试

常见但无效的做法:

  1. 70% 失败

    Still blocks; program never exits and deadlock detection is defeated.

  2. 60% 失败

    Goroutine leaks and main may exit before work completes.