go runtime_error ai_generated true

fatal error: all goroutines are asleep - deadlock! (WaitGroup)

ID: go/goroutine-leak-on-waitgroup

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active

Root Cause

WaitGroup的计数器未归零,主goroutine调用Wait,但子goroutine因阻塞而无法完成。

generic

中文

WaitGroup的计数器未归零,主goroutine调用Wait,但子goroutine因阻塞而无法完成。

Workarounds

  1. 85% success
    使用select和done通道通知退出
  2. 90% success
    ctx, cancel := context.WithCancel(context.Background()); go func(){ select { case <-ctx.Done(): return } }()

Dead Ends

Common approaches that don't work:

  1. 60% fail

    超时后goroutine仍在运行,程序可能退出但资源泄漏。

  2. 70% fail

    关闭通道可能引发其他panic,且未解决阻塞原因。