{
  "id": "python/asyncio-gather-cancelled-error",
  "signature": "asyncio.exceptions.CancelledError: Task was destroyed but it is pending!",
  "signature_zh": "asyncio.exceptions.CancelledError：任务被销毁但仍在等待中！",
  "regex": "asyncio\\.exceptions\\.CancelledError:\\ Task\\ was\\ destroyed\\ but\\ it\\ is\\ pending!",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "When using asyncio.gather() with return_exceptions=False, if one task raises CancelledError (e.g., due to timeout), the gather is cancelled and other tasks are not properly awaited, leading to pending task destruction warnings.",
  "root_cause_type": "generic",
  "root_cause_zh": "使用 asyncio.gather() 且 return_exceptions=False 时，如果某个任务因超时等原因抛出 CancelledError，gather 被取消，其他任务未被正确等待，导致待处理任务销毁警告。",
  "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": "CancelledError is not always raised synchronously; it can be raised during await, and ignoring it may leave tasks in inconsistent states.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Shield only protects against cancellation of the outer task, not the inner tasks; it doesn't solve the root issue of pending tasks.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "results = await asyncio.gather(*tasks, return_exceptions=True)\nfor r in results:\n    if isinstance(r, asyncio.CancelledError):\n        # handle cancellation gracefully",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)\nfor task in pending:\n    task.cancel()\nawait asyncio.gather(*pending, return_exceptions=True)",
      "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.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-03-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}