{
  "id": "go/panic-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 is called after wg.Wait has returned, or Add is called inside a goroutine racing with Wait.",
  "root_cause_type": "generic",
  "root_cause_zh": "在 wg.Wait 返回后又调用 wg.Add，或 Add 在 goroutine 内与 Wait 竞态调用。",
  "versions": [
    {
      "version": "1.0+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Races with Wait; Wait may observe zero counter and return early.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "WaitGroup is not designed for reuse until Wait returns; runtime panics.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "wg.Add(len(items))\nfor _, it := range items {\n    go func(x Item) { defer wg.Done(); process(x) }(it)\n}\nwg.Wait()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.93,
      "how": "g := new(errgroup.Group)\nfor _, it := range items {\n    it := it\n    g.Go(func() error { return process(it) })\n}\nreturn g.Wait()",
      "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-08-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}