go runtime_error ai_generated true

恐慌:同步:WaitGroup在之前的Wait返回之前被重用(竞争关闭)

panic: sync: WaitGroup is reused before previous Wait has returned (racy close)

ID: go/goroutine-racy-close-waitgroup

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
0证据数
2026-05-22首次发现

版本兼容性

版本状态引入弃用备注
1.0 active
1.21 active

根因分析

从多个协程并发调用WaitGroup.Wait()然后Add()。

English

Calling WaitGroup.Wait() and then Add() concurrently from multiple goroutines.

generic

解决方案

  1. 95% 成功率 Use separate WaitGroup for each batch
    for batch := 0; batch < 3; batch++ {
        var wg sync.WaitGroup
        for i := 0; i < 5; i++ {
            wg.Add(1)
            go func() { defer wg.Done(); /* work */ }()
        }
        wg.Wait()
    }
  2. 85% 成功率 Use channel-based synchronization
    ch := make(chan struct{})
    for i := 0; i < 5; i++ {
        go func() { /* work */; ch <- struct{}{} }()
    }
    for i := 0; i < 5; i++ {
        <-ch
    }

无效尝试

常见但无效的做法:

  1. Using recover() to catch panic 100% 失败

    Panic is unrecoverable; program crashes.

  2. Adding time.Sleep between Wait and Add 90% 失败

    Race condition still exists; timing-dependent.