{
  "id": "go/waitgroup-misuse-add-inside-goroutine",
  "signature": "panic: sync: WaitGroup is reused before previous Wait has returned",
  "signature_zh": "panic：sync：WaitGroup 在上一次 Wait 返回前被重用",
  "regex": "panic:\\ sync:\\ WaitGroup\\ is\\ reused\\ before\\ previous\\ Wait\\ has\\ returned",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "wg.Add() was called concurrently with wg.Wait(), or the WaitGroup was reused before all Wait calls returned. This is a race between Add and Wait.",
  "root_cause_type": "generic",
  "root_cause_zh": "wg.Add() 与 wg.Wait() 并发执行，或在所有 Wait 返回前重用了 WaitGroup。这是 Add 与 Wait 之间的竞态。",
  "versions": [
    {
      "version": "1.18",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Sleep doesn't establish happens-before ordering; the race can still occur and only masks the bug intermittently.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Sharing one WaitGroup across independent operations increases the chance of Add/Wait overlap, worsening the race.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.92,
      "how": "wg.Add(n)\nfor i := 0; i < n; i++ {\n    go worker(&wg)\n}\nwg.Wait()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "for _, batch := range batches {\n    var wg sync.WaitGroup\n    wg.Add(len(batch))\n    for _, item := range batch {\n        go func(it Item) { defer wg.Done(); process(it) }(item)\n    }\n    wg.Wait()\n}",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-09",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}