# fatal error: all goroutines are asleep - deadlock! (wg.Wait returns before goroutines start)

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

## Root Cause

wg.Add(1) is called inside the goroutine rather than before it, so Wait() can return early or block forever depending on timing.

## Version Compatibility

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

## Workarounds

1. **** (98% success)
   ```
   wg.Add(1)
go func() {
    defer wg.Done()
    work()
}()
wg.Wait()
   ```
2. **** (90% success)
   ```
   done := make(chan struct{}, n)
for i := 0; i < n; i++ {
    go func() { work(); done <- struct{}{} }()
}
for i := 0; i < n; i++ { <-done }
   ```

## Dead Ends

- **** — Sleeping only hides the race; under load the goroutine may still not have run Add in time. (80% fail)
- **** — Serialization does not guarantee the goroutine has executed Add before Wait checks the counter. (75% fail)
