{
  "id": "python/starlette-sse-client-disconnect",
  "signature": "asyncio.CancelledError: Task was cancelled during StreamingResponse iteration",
  "signature_zh": "asyncio.CancelledError: StreamingResponse 迭代期间任务被取消",
  "regex": "asyncio\\.CancelledError:\\ Task\\ was\\ cancelled\\ during\\ StreamingResponse\\ iteration",
  "domain": "python",
  "category": "network_error",
  "subcategory": null,
  "root_cause": "An SSE StreamingResponse generator kept yielding after the client disconnected; the ASGI server cancelled the task.",
  "root_cause_type": "generic",
  "root_cause_zh": "SSE StreamingResponse 生成器在客户端断开后继续 yield；ASGI 服务器取消了该任务。",
  "versions": [
    {
      "version": "0.30+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "The connection is gone; yield raises again and the task leaks.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Clients still close tabs; cancellation is normal, not preventable.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "async def gen(request: Request):\n    while True:\n        if await request.is_disconnected():\n            break\n        yield f'data: {tick()}\\n\\n'\n        await asyncio.sleep(1)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "async def gen():\n    try:\n        while True:\n            yield data\n            await asyncio.sleep(1)\n    finally:\n        cleanup()",
      "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.83,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-11-26",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}