{
  "id": "go/waitgroup-added-after-wait",
  "signature": "panic: sync: WaitGroup is reused before previous Wait has returned",
  "signature_zh": "panic: 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 from a goroutine while another goroutine was blocked in wg.Wait(), or the WaitGroup was reused across batches without synchronization.",
  "root_cause_type": "generic",
  "root_cause_zh": "在一个 goroutine 阻塞于 wg.Wait() 期间，另一个 goroutine 调用了 wg.Add()，或 WaitGroup 在未同步的情况下跨批次重用。",
  "versions": [
    {
      "version": "1.0+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "The ordering of Add vs Wait is the actual constraint; moving Wait just changes where the race manifests and can cause premature return.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "No happens-before guarantee; under load the Add still races with Wait.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.97,
      "how": "var wg sync.WaitGroup\nfor _, item := range items {\n    wg.Add(1)\n    go func(it Item) {\n        defer wg.Done()\n        process(it)\n    }(item)\n}\nwg.Wait()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.93,
      "how": "for _, batch := range batches {\n    var wg sync.WaitGroup\n    for _, x := range batch {\n        wg.Add(1)\n        go func(v X) { defer wg.Done(); work(v) }(x)\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-04-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}