{
  "id": "go/context-cancellation-race",
  "signature": "panic: sync: WaitGroup misuse: Add called concurrently with Wait",
  "signature_zh": "panic: sync: WaitGroup 误用: Add 与 Wait 并发调用",
  "regex": "panic:\\ sync:\\ WaitGroup\\ misuse:\\ Add\\ called\\ concurrently\\ with\\ Wait",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Calling Add() on a WaitGroup concurrently with Wait(), often due to canceling context that triggers new goroutines while waiting.",
  "root_cause_type": "generic",
  "root_cause_zh": "在 Wait() 并发调用 Add()，通常是因为取消 context 导致在等待时启动了新的 goroutine。",
  "versions": [
    {
      "version": "1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Doesn't prevent Add from being called after Wait has started.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "If goroutines are started from a context cancellation callback, it may be too late.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Ensure all Add calls happen before Wait by using a separate goroutine to start workers: \n\nvar wg sync.WaitGroup\nfor i := 0; i < n; i++ {\n    wg.Add(1)\n    go func() { defer wg.Done(); /* work */ }()\n}\nwg.Wait()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Use a channel to signal completion instead of WaitGroup when dynamic goroutine creation is needed.",
      "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.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-09-02",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}