go build_error ai_generated true

error:go vet:赋值复制了锁值:sync.WaitGroup 包含 sync.noCopy

error: go vet: assignment copies lock value: sync.WaitGroup contains sync.noCopy

ID: go/waitgroup-copied-by-value

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

版本兼容性

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

根因分析

按值传递 sync.WaitGroup 会复制其内部状态;副本的 Done 不会影响原始的 Wait,导致 Wait 挂起或 panic。

English

Passing a sync.WaitGroup by value copies its internal state; the copy's Done never reaches the original's Wait, so Wait hangs or panics.

generic

解决方案

  1. 98% 成功率
    func worker(wg *sync.WaitGroup) {
        defer wg.Done()
    }
  2. 95% 成功率
    type Pool struct { wg sync.WaitGroup }
    func (p *Pool) Add() { p.wg.Add(1) }

无效尝试

常见但无效的做法:

  1. 90% 失败

    The copy shares no state; Done on the copy doesn't decrement the original counter, so Wait blocks forever.

  2. 85% 失败

    The state is still split across two structs; the mutex does not merge them.