{
  "id": "go/waitgroup-add-inside-goroutine-race",
  "signature": "fatal error: all goroutines are asleep - deadlock! (wg.Wait returns before goroutines start)",
  "signature_zh": "fatal error: 所有 goroutine 都处于休眠状态 - 死锁！（wg.Wait 在 goroutine 启动前返回）",
  "regex": "fatal\\ error:\\ all\\ goroutines\\ are\\ asleep\\ \\-\\ deadlock!\\ \\(wg\\.Wait\\ returns\\ before\\ goroutines\\ start\\)",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "wg.Add(1) is called inside the goroutine rather than before it, so Wait() can return early or block forever depending on timing.",
  "root_cause_type": "generic",
  "root_cause_zh": "wg.Add(1) 在 goroutine 内部调用而不是之前，导致 Wait() 可能提前返回或永久阻塞，取决于时序。",
  "versions": [
    {
      "version": "1.18",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.21",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Sleeping only hides the race; under load the goroutine may still not have run Add in time.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Serialization does not guarantee the goroutine has executed Add before Wait checks the counter.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.98,
      "how": "wg.Add(1)\ngo func() {\n    defer wg.Done()\n    work()\n}()\nwg.Wait()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "done := make(chan struct{}, n)\nfor i := 0; i < n; i++ {\n    go func() { work(); done <- struct{}{} }()\n}\nfor i := 0; i < n; i++ { <-done }",
      "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-03-08",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}