# panic: sync: WaitGroup is reused before previous Wait has returned

- **ID:** `go/waitgroup-misuse-add-inside-goroutine`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

wg.Add() was called concurrently with wg.Wait(), or the WaitGroup was reused before all Wait calls returned. This is a race between Add and Wait.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.18 | active | — | — |
| 1.22 | active | — | — |

## Workarounds

1. **** (92% success)
   ```
   wg.Add(n)
for i := 0; i < n; i++ {
    go worker(&wg)
}
wg.Wait()
   ```
2. **** (90% success)
   ```
   for _, batch := range batches {
    var wg sync.WaitGroup
    wg.Add(len(batch))
    for _, item := range batch {
        go func(it Item) { defer wg.Done(); process(it) }(item)
    }
    wg.Wait()
}
   ```

## Dead Ends

- **** — Sleep doesn't establish happens-before ordering; the race can still occur and only masks the bug intermittently. (85% fail)
- **** — Sharing one WaitGroup across independent operations increases the chance of Add/Wait overlap, worsening the race. (80% fail)
