# panic：sync：WaitGroup 在上一次 Wait 返回前被复用

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

## 根因

在上一轮 Wait() 尚未返回时就再次调用了 Add() 复用 WaitGroup，sync 包明确禁止这种用法。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.18 | active | — | — |
| 1.22 | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   for _, batch := range batches {
    var wg sync.WaitGroup
    for _, it := range batch {
        wg.Add(1)
        go func(it Item) { defer wg.Done(); process(it) }(it)
    }
    wg.Wait()
}
   ```
2. **** (92% 成功率)
   ```
   for _, stage := range stages {
    g := new(errgroup.Group)
    for _, t := range stage { g.Go(t.Run) }
    if err := g.Wait(); err != nil { return err }
}
   ```

## 无效尝试

- **** — Recover leaves the WaitGroup in an inconsistent state and the concurrent Wait may return early or hang. (85% 失败率)
- **** — Two Waits on the same group do not resolve the reuse race; it can still panic. (80% 失败率)
- **** — Timing-based, non-deterministic, and fails under load. (90% 失败率)
