{
  "id": "python/asyncio-task-cancellation-error",
  "signature": "asyncio.exceptions.CancelledError: CancelledError raised in task",
  "signature_zh": "asyncio 异常：任务中引发了 CancelledError",
  "regex": "asyncio\\.exceptions\\.CancelledError:\\ CancelledError\\ raised\\ in\\ task",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A task was cancelled externally (e.g., via task.cancel()), and the cancellation was not handled, causing it to propagate.",
  "root_cause_type": "generic",
  "root_cause_zh": "任务被外部取消（例如通过 task.cancel()），且未处理取消操作，导致其传播。",
  "versions": [
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Catching all exceptions except CancelledError",
      "why_fails": "CancelledError is a subclass of BaseException, so it may not be caught by except Exception.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Ignoring cancellation and continuing execution",
      "why_fails": "The event loop may be shutting down, and ignoring cancellation can cause resource leaks or hangs.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Catch CancelledError and perform cleanup, then re-raise",
      "success_rate": 0.9,
      "how": "try:\n    await long_running_coro()\nexcept asyncio.CancelledError:\n    # cleanup resources\n    raise",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use asyncio.shield() to protect critical sections",
      "success_rate": 0.85,
      "how": "await asyncio.shield(critical_coro())",
      "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.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-06-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}