# panic: sync: negative WaitGroup counter

- **ID:** `go/sync-waitgroup-negative-counter`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

wg.Done() is called more times than wg.Add(n), often because Done is called in a goroutine that was never added, or Add is called after Wait has already returned.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.x | active | — | — |

## Workarounds

1. **** (97% success)
   ```
   for _, item := range items {
    wg.Add(1)
    go func(it Item) {
        defer wg.Done()
        process(it)
    }(item)
}
wg.Wait()
   ```
2. **** (95% success)
   ```
   g, ctx := errgroup.WithContext(ctx)
for _, it := range items {
    it := it
    g.Go(func() error { return process(ctx, it) })
}
if err := g.Wait(); err != nil { return err }
   ```

## Dead Ends

- **** — Race with wg.Wait(): Wait may return before Add runs, then Done panics or Wait returns early. (70% fail)
- **** — Hides the bug; the WaitGroup is now inconsistent and Wait may hang or return prematurely. (75% fail)
