go runtime_error ai_generated true

恐慌:同步:WaitGroup计数器为负

panic: sync: negative WaitGroup counter

ID: go/waitgroup-negative-counter

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

版本兼容性

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

根因分析

调用Done()的次数多于Add(),导致WaitGroup计数器变为负数。

English

Calling Done() more times than Add(), causing the WaitGroup counter to go negative.

generic

解决方案

  1. 95% 成功率
    Ensure Add is called before starting goroutines and Done is called exactly once per goroutine.
  2. 90% 成功率
    Use a defer statement right after Add to guarantee Done is called.

无效尝试

常见但无效的做法:

  1. 100% 失败

    The panic will crash the program; ignoring is not possible.

  2. 80% 失败

    This masks the bug and may cause Wait() to return prematurely.

  3. 70% 失败

    If Add is not called, Done will still decrement and may go negative.