{
  "id": "python/aiohttp-cannot-write-after-response-started",
  "signature": "RuntimeError: Cannot write to closing transport",
  "signature_zh": "RuntimeError: 无法写入正在关闭的传输",
  "regex": "RuntimeError:\\ Cannot\\ write\\ to\\ closing\\ transport",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "aiohttp server tried to write to a StreamResponse after the client disconnected or the response was already finalized.",
  "root_cause_type": "generic",
  "root_cause_zh": "客户端断开或响应已完成后，aiohttp 服务器仍尝试向 StreamResponse 写入数据。",
  "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"
    },
    {
      "version": "3.11+",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Silently drops data and may hide application logic bugs.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The transport is already closing; timeout is unrelated.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "if request.transport is None or request.transport.is_closing():\n    return\nawait resp.write(chunk)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "resp = web.StreamResponse()\nawait resp.prepare(request)\ntry:\n    async for chunk in source:\n        await resp.write(chunk)\nexcept (ConnectionResetError, RuntimeError):\n    pass\nfinally:\n    await resp.write_eof()",
      "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.82,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-11-14",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}