# panic: sync: WaitGroup 在之前的 Wait 返回前被重用

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

## 根因

在另一个 goroutine 仍处于 wg.Wait 时再次调用 wg.Add。通常发生在循环中跨迭代复用同一个 WaitGroup 而未重置。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   for _, batch := range batches {
    var wg sync.WaitGroup
    for _, x := range batch {
        wg.Add(1)
        go func(x int) { defer wg.Done(); work(x) }(x)
    }
    wg.Wait()
}
   ```
2. **** (93% 成功率)
   ```
   for _, batch := range batches {
    g := new(errgroup.Group)
    for _, x := range batch { x := x; g.Go(func() error { return work(x) }) }
    if err := g.Wait(); err != nil { return err }
}
   ```

## 无效尝试

- **** — Reassigning while Wait is in flight races on the struct; still panics or corrupts state. (85% 失败率)
- **** — Sleep does not synchronize with Wait's completion; race remains. (80% 失败率)
