go runtime_error ai_generated true

panic: sync: WaitGroup is reused before previous Wait has returned (racy close)

ID: go/goroutine-racy-close-waitgroup

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2026-05-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.21 active

Root Cause

Calling WaitGroup.Wait() and then Add() concurrently from multiple goroutines.

generic

中文

从多个协程并发调用WaitGroup.Wait()然后Add()。

Workarounds

  1. 95% success Use separate WaitGroup for each batch
    for batch := 0; batch < 3; batch++ {
        var wg sync.WaitGroup
        for i := 0; i < 5; i++ {
            wg.Add(1)
            go func() { defer wg.Done(); /* work */ }()
        }
        wg.Wait()
    }
  2. 85% success Use channel-based synchronization
    ch := make(chan struct{})
    for i := 0; i < 5; i++ {
        go func() { /* work */; ch <- struct{}{} }()
    }
    for i := 0; i < 5; i++ {
        <-ch
    }

Dead Ends

Common approaches that don't work:

  1. Using recover() to catch panic 100% fail

    Panic is unrecoverable; program crashes.

  2. Adding time.Sleep between Wait and Add 90% fail

    Race condition still exists; timing-dependent.