# panic: WaitGroup 在上次 Wait 返回前被重用

- **ID:** `go/waitgroup-added-after-wait`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在一个 goroutine 阻塞于 wg.Wait() 期间，另一个 goroutine 调用了 wg.Add()，或 WaitGroup 在未同步的情况下跨批次重用。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.0+ | active | — | — |

## 解决方案

1. **** (97% 成功率)
   ```
   var wg sync.WaitGroup
for _, item := range items {
    wg.Add(1)
    go func(it Item) {
        defer wg.Done()
        process(it)
    }(item)
}
wg.Wait()
   ```
2. **** (93% 成功率)
   ```
   for _, batch := range batches {
    var wg sync.WaitGroup
    for _, x := range batch {
        wg.Add(1)
        go func(v X) { defer wg.Done(); work(v) }(x)
    }
    wg.Wait()
}
   ```

## 无效尝试

- **** — The ordering of Add vs Wait is the actual constraint; moving Wait just changes where the race manifests and can cause premature return. (75% 失败率)
- **** — No happens-before guarantee; under load the Add still races with Wait. (80% 失败率)
