{
  "id": "go/context-canceled-unexpected",
  "signature": "error: context canceled",
  "signature_zh": "error: context 已取消",
  "regex": "error:\\ context\\ canceled",
  "domain": "go",
  "category": "network_error",
  "subcategory": null,
  "root_cause": "A context was canceled (via cancel() or parent cancellation) while a goroutine or HTTP/database call was still using it, causing the operation to fail with context.Canceled.",
  "root_cause_type": "generic",
  "root_cause_zh": "在 goroutine 或 HTTP/数据库调用仍在使用 context 时，context 被取消（通过 cancel() 或父级取消），导致操作以 context.Canceled 失败。",
  "versions": [
    {
      "version": "1.19",
      "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": "If the context is canceled, the retry uses the same canceled context and fails instantly, creating a tight loop.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Loses timeout and cancellation propagation, causing goroutine and connection leaks.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.93,
      "how": "if err := doWork(ctx); err != nil {\n    if errors.Is(err, context.Canceled) {\n        return nil // graceful shutdown\n    }\n    return err\n}",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "for i := 0; i < 3; i++ {\n    ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)\n    err := call(ctx)\n    cancel()\n    if err == nil { return nil }\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-05-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}