go runtime_error ai_generated true

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

ID: go/waitgroup-passed-by-value

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.14+ active

Root Cause

A sync.WaitGroup is passed by value to a function, so Add/Done operate on a copy and Wait() on the original never unblocks.

generic

中文

sync.WaitGroup 按值传递给函数,导致 Add/Done 作用于副本,原对象的 Wait() 永远无法解除阻塞。

Workarounds

  1. 95% success
    func worker(wg *sync.WaitGroup) {
        defer wg.Done()
        // ...
    }
  2. 92% success
    type Pool struct { wg sync.WaitGroup }
    func (p *Pool) Run() { p.wg.Add(1); go func(){ defer p.wg.Done() }() }

Dead Ends

Common approaches that don't work:

  1. 80% fail

    The copy bug remains; the timeout just papers over the deadlock without fixing counter propagation.

  2. 85% fail

    The counters are in different copies; adding to the original still does not match the copy's Done calls.