{
  "id": "go/goroutine-waitgroup-negative-counter",
  "signature": "panic: sync: negative WaitGroup counter",
  "signature_zh": "恐慌：同步：负数WaitGroup计数器",
  "regex": "panic:\\ sync:\\ negative\\ WaitGroup\\ counter",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Calling Done() more times than Add(), causing WaitGroup counter to go negative.",
  "root_cause_type": "generic",
  "root_cause_zh": "调用Done()的次数超过Add()，导致WaitGroup计数器变为负数。",
  "versions": [
    {
      "version": "1.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Using recover() to catch panic and continue",
      "why_fails": "Panic indicates misuse; recovering doesn't fix the counter imbalance.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Adding extra Add() calls to compensate",
      "why_fails": "Race condition may cause unpredictable counter values; proper accounting is needed.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure Add is called exactly once per goroutine before Done",
      "success_rate": 0.95,
      "how": "var wg sync.WaitGroup\nfor i := 0; i < 10; i++ {\n    wg.Add(1)\n    go func() {\n        defer wg.Done()\n        // work\n    }()\n}\nwg.Wait()",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use defer wg.Done() immediately after wg.Add()",
      "success_rate": 0.98,
      "how": "wg.Add(1)\ngo func() {\n    defer wg.Done()\n    // work\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.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-08-30",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}