{
  "id": "python/asyncio-task-destroyed-pending",
  "signature": "asyncio.exceptions.CancelledError: Task was destroyed but it is pending",
  "signature_zh": "asyncio 异常：任务被销毁但仍在挂起状态",
  "regex": "asyncio\\.exceptions\\.CancelledError:\\ Task\\ was\\ destroyed\\ but\\ it\\ is\\ pending",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A task is garbage collected while it is still pending (not completed), often due to losing a reference to the task.",
  "root_cause_type": "generic",
  "root_cause_zh": "任务在仍处于挂起状态时被垃圾回收，通常是因为丢失了对任务的引用。",
  "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": "Ignoring the warning and letting the task run",
      "why_fails": "The task may be cancelled abruptly, causing incomplete operations and resource leaks.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using asyncio.ensure_future() without storing the task",
      "why_fails": "ensure_future() returns a task, but if not stored, it may be garbage collected while pending.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Store the task in a list or variable to keep a reference",
      "success_rate": 0.9,
      "how": "tasks = []\ntask = asyncio.create_task(coro())\ntasks.append(task)\nawait asyncio.gather(*tasks)",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use asyncio.gather() directly to manage tasks",
      "success_rate": 0.95,
      "how": "results = await asyncio.gather(coro1(), coro2())",
      "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.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-01-10",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}