{
  "id": "go/waitgroup-add-after-wait",
  "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 concurrently with wg.Wait(). The WaitGroup contract requires all Add calls that increment the counter to happen-before the Wait that observes them.",
  "root_cause_type": "generic",
  "root_cause_zh": "wg.Add() 与 wg.Wait() 并发调用。WaitGroup 约定要求所有增加计数器的 Add 调用必须先于观察到它们的 Wait。",
  "versions": [
    {
      "version": "1.0+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "The goroutine may start after Wait() already returned, so the counter never increments in time; still racy.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Timing-based synchronization is nondeterministic and fails under load or on fast machines.",
      "fail_rate": 0.95,
      "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.96,
      "how": "g, ctx := errgroup.WithContext(context.Background())\nfor _, item := range items {\n    item := item\n    g.Go(func() error { return process(ctx, item) })\n}\nif err := g.Wait(); err != nil { return err }",
      "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.89,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-03-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}