{"id":"5003235d250ece0e","repo":"aio-libs/aiohttp","slug":"connection-lost","errorCode":null,"errorMessage":"Connection lost","messagePattern":"Connection lost","errorType":"exception","errorClass":"ClientConnectionResetError","httpStatus":null,"severity":"error","filePath":"aiohttp/base_protocol.py","lineNumber":133,"sourceCode":"            return\n        waiter = self._drain_waiter\n        if waiter is None:\n            return\n        self._drain_waiter = None\n        if waiter.done():\n            return\n        if exc is None:\n            waiter.set_result(None)\n        else:\n            set_exception(\n                waiter,\n                ConnectionError(\"Connection lost\"),\n                exc,\n            )\n\n    async def _drain_helper(self) -> None:\n        if self.transport is None:\n            raise ClientConnectionResetError(\"Connection lost\")\n        if not self._paused:\n            return\n        waiter = self._drain_waiter\n        if waiter is None:\n            waiter = self._loop.create_future()\n            self._drain_waiter = waiter\n        await waiter\n","sourceCodeStart":115,"sourceCodeEnd":141,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/base_protocol.py#L115-L141","documentation":"Raised by BaseProtocol._drain_helper (base_protocol.py:133) when self.transport is None, i.e. the connection was already lost (connection_lost ran and nulled the transport) while a drain was pending. aiohttp surfaces this as ClientConnectionResetError('Connection lost'). It is the standard signal that writes are going to a dead connection.","triggerScenarios":"A coroutine awaits flow-control drain (implicitly during response body write / websocket send) on a connection whose transport has already been closed by connection_lost.","commonSituations":"Client writes after the server closed, response streaming into a dropped socket, websocket sends after a network reset, or cancelled tasks draining a closing protocol.","solutions":["Catch ClientConnectionResetError (or its parent ConnectionResetError/ClientConnectionError) around write/drain paths and abort the request cleanly.","Detect disconnection early via the receive side and stop the producer.","Use ClientTimeout and heartbeats so dead connections surface sooner."],"exampleFix":"# before\nawait resp.write(data)  # raises ClientConnectionResetError if socket gone\n\n# after\ntry:\n    await resp.write(data)\nexcept aiohttp.ClientConnectionResetError:\n    # client already disconnected; stop streaming\n    return","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    await resp.write(data)\nexcept aiohttp.ClientConnectionResetError:\n    return  # client already disconnected","preventionTips":["Detect disconnect on the read/receive side and stop producing","Use ClientTimeout and heartbeats to surface dead connections sooner"],"tags":["connection","connection-reset","transport","flow-control","client"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}