# 恐慌：sync: WaitGroup 在之前 Wait 返回前被重用

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

## 根因

在 Wait() 开始但尚未返回时调用了 WaitGroup.Add()，违反了 Add 不能与 Wait 并发的规则。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Ensure all Add calls happen before any Wait call. Use a single WaitGroup per logical group and don't reuse until Wait returns.
   ```
2. **** (92% 成功率)
   ```
   Use errgroup.Group which manages lifecycle safely:
g, ctx := errgroup.WithContext(ctx)
for ... { g.Go(...) }
return g.Wait()
   ```

## 无效尝试

- **** — Sleep does not guarantee Wait has returned; race still possible. (90% 失败率)
- **** — If batches overlap, the same issue can occur; requires careful lifecycle management. (50% 失败率)
