go runtime_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-07-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —
1.22 active — — —

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 70% fail

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

  2. 60% fail

    Goroutine leaks and main may exit before work completes.