{
  "id": "python/asyncio-task-cancellation-not-handled",
  "signature": "asyncio.exceptions.CancelledError: Task was cancelled",
  "signature_zh": "asyncio.exceptions.CancelledError：任务被取消",
  "regex": "asyncio\\.exceptions\\.CancelledError:\\ Task\\ was\\ cancelled",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A coroutine that is cancelled while awaiting an operation does not handle CancelledError, causing the task to terminate abruptly and potentially leaving resources unclean.",
  "root_cause_type": "generic",
  "root_cause_zh": "协程在等待操作时被取消，但未处理 CancelledError，导致任务突然终止并可能使资源未清理。",
  "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"
    },
    {
      "version": "3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Ignoring cancellation can violate asyncio's cooperative cancellation model and lead to tasks that never truly cancel.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Finally runs but the exception propagates; if not caught, it may still terminate the task.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "try:\n    await long_operation()\nexcept asyncio.CancelledError:\n    # cleanup resources\n    raise  # re-raise to acknowledge cancellation",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.8,
      "how": "result = await asyncio.shield(critical_operation())",
      "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-03-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}