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

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

## 根因

在上一次 wg.Wait() 尚未返回时又调用 wg.Add()，这违反了 WaitGroup 的使用约定。

## 版本兼容性

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

## 解决方案

1. **** (93% 成功率)
   ```
   Create a fresh WaitGroup per batch: do not reuse the same wg across phases. var wg sync.WaitGroup once per logical group of goroutines.
   ```
2. **** (90% 成功率)
   ```
   Use golang.org/x/sync/errgroup where lifecycle is bounded by Wait() returning, and never call Go() after Wait() has been invoked.
   ```

## 无效尝试

- **** — The mutex serializes the calls but does not change the illegal reuse pattern; the panic still fires. (85% 失败率)
- **** — A naive channel close races with still-running goroutines and may close early, producing send-on-closed panics. (70% 失败率)
