# panic: sync: negative WaitGroup counter

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

## Root Cause

Calling Done() on a WaitGroup more times than Add() was called, often due to mismatched goroutine lifecycle management.

## Version Compatibility

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

## Workarounds

1. **Ensure Add() is called exactly once per goroutine before starting it** (98% success)
   ```
   var wg sync.WaitGroup
for i := 0; i < n; i++ {
    wg.Add(1)
    go func() {
        defer wg.Done()
        // work
    }()
}
wg.Wait()
   ```
2. **Use atomic counter with careful increment/decrement** (80% success)
   ```
   var counter int64
atomic.AddInt64(&counter, 1)
go func() {
    // work
    atomic.AddInt64(&counter, -1)
}()
   ```

## Dead Ends

- **Adding recover() to catch the panic** — Recover does not fix the underlying counter mismatch; it only hides the symptom. (90% fail)
- **Increasing Add() calls arbitrarily** — This can lead to other goroutines waiting indefinitely if Done() is not called enough times. (85% fail)
