{
  "id": "go/goroutine-negative-waitgroup-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() on a WaitGroup more times than Add() was called, often due to mismatched goroutine lifecycle management.",
  "root_cause_type": "generic",
  "root_cause_zh": "对WaitGroup调用Done()的次数超过Add()调用的次数，通常是由于协程生命周期管理不匹配。",
  "versions": [
    {
      "version": "1.21",
      "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": "Adding recover() to catch the panic",
      "why_fails": "Recover does not fix the underlying counter mismatch; it only hides the symptom.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Increasing Add() calls arbitrarily",
      "why_fails": "This can lead to other goroutines waiting indefinitely if Done() is not called enough times.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure Add() is called exactly once per goroutine before starting it",
      "success_rate": 0.98,
      "how": "var wg sync.WaitGroup\nfor i := 0; i < n; i++ {\n    wg.Add(1)\n    go func() {\n        defer wg.Done()\n        // work\n    }()\n}\nwg.Wait()",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use atomic counter with careful increment/decrement",
      "success_rate": 0.8,
      "how": "var counter int64\natomic.AddInt64(&counter, 1)\ngo func() {\n    // work\n    atomic.AddInt64(&counter, -1)\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-04-12",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}