go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! (main exits without waiting)

ID: go/goroutine-not-joining

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.22 active

Root Cause

Main function exits while other goroutines are still running, but they are blocked on channels or other operations, causing deadlock detection.

generic

中文

主函数退出时其他 goroutine 仍在运行,但它们阻塞在通道或其他操作上,导致死锁检测。

Workarounds

  1. 95% success
    Use sync.WaitGroup to wait for all goroutines to finish before returning from main.
  2. 90% success
    Use a done channel and close it when all work is done; main blocks on <-done.

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Sleep is nondeterministic; goroutines may finish later or never, and it's inefficient.

  2. 90% fail

    Program exits prematurely, possibly leaving resources unclean or missing results.