go runtime_error ai_generated true

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

panic: sync: WaitGroup is reused before previous Wait has returned

ID: go/panic-sync-waitgroup-is-reused-before-previous-wait-has-re

其他格式: JSON · Markdown 中文 · English
80%修复率
89%置信度
0证据数
2024-09-19首次发现

版本兼容性

版本状态引入弃用备注
1.18 active — — —
1.20 active — — —
1.23 active — — —

根因分析

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

English

wg.Add() is called again while a previous wg.Wait() is still in progress, which is explicitly disallowed by the WaitGroup contract.

generic

解决方案

  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.

无效尝试

常见但无效的做法:

  1. 85% 失败

    The mutex serializes the calls but does not change the illegal reuse pattern; the panic still fires.

  2. 70% 失败

    A naive channel close races with still-running goroutines and may close early, producing send-on-closed panics.