{
  "id": "go/goroutine-leak-on-ctx-cancel",
  "signature": "error: context deadline exceeded (goroutine blocked on <-ch after cancel)",
  "signature_zh": "错误: 上下文截止时间已过（取消后 goroutine 阻塞在 <-ch）",
  "regex": "error:\\ context\\ deadline\\ exceeded\\ \\(goroutine\\ blocked\\ on\\ <\\-ch\\ after\\ cancel\\)",
  "domain": "go",
  "category": "resource_error",
  "subcategory": null,
  "root_cause": "A goroutine waits on a channel that is only written on success. When ctx is cancelled the sender returns early and never sends, leaving the receiver blocked forever.",
  "root_cause_type": "generic",
  "root_cause_zh": "goroutine 等待一个仅在成功时写入的 channel。ctx 取消后发送者提前返回从不发送，接收者永远阻塞。",
  "versions": [
    {
      "version": "1.18",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.20",
      "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"
    },
    {
      "version": "1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Delays the leak; cancel still leaves the receiver blocked.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "default makes the loop spin hot and still does not deliver the value.",
      "fail_rate": 0.65,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Select on ctx.Done() alongside the channel:\n\nselect {\ncase v := <-ch:\n    return v, nil\ncase <-ctx.Done():\n    return nil, ctx.Err()\n}",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Ensure producers always send or close on exit:\n\ndefer close(ch)\nfor _, item := range items {\n    select {\n    case ch <- item:\n    case <-ctx.Done():\n        return\n    }\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-09",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}