# fatal error: all goroutines are asleep - deadlock! (WaitGroup.Wait returned before Add)

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

## Root Cause

wg.Add(1) is called inside the spawned goroutine instead of before go, so wg.Wait() can observe a zero counter and return, or the program deadlocks.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.0+ | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   for _, item := range items {
    wg.Add(1)
    go func(item Item) {
        defer wg.Done()
        process(item)
    }(item)
}
wg.Wait()
   ```
2. **** (93% success)
   ```
   g, ctx := errgroup.WithContext(context.Background())
for _, item := range items {
    item := item
    g.Go(func() error { return process(ctx, item) })
}
return g.Wait()
   ```

## Dead Ends

- **** — Sleep is a race; under load the goroutine still may not have run Add before Wait. (75% fail)
- **** — Doubling the count causes Wait to block forever or triggers a negative counter panic. (80% fail)
