{"record":{"id":"19fb788e66e5dc54","repo":"python/cpython","slug":"connection-closed-by-peer","errorCode":null,"errorMessage":"Connection closed by peer","messagePattern":"Connection closed by peer","errorType":"exception","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"Lib/asyncio/base_events.py","lineNumber":227,"sourceCode":"\nclass _SendfileFallbackProtocol(protocols.Protocol):\n    def __init__(self, transp):\n        if not isinstance(transp, transports._FlowControlMixin):\n            raise TypeError(\"transport should be _FlowControlMixin instance\")\n        self._transport = transp\n        self._proto = transp.get_protocol()\n        self._should_resume_reading = transp.is_reading()\n        self._should_resume_writing = transp._protocol_paused\n        transp.pause_reading()\n        transp.set_protocol(self)\n        if self._should_resume_writing:\n            self._write_ready_fut = self._transport._loop.create_future()\n        else:\n            self._write_ready_fut = None\n\n    async def drain(self):\n        if self._transport.is_closing():\n            raise ConnectionError(\"Connection closed by peer\")\n        fut = self._write_ready_fut\n        if fut is None:\n            return\n        await fut\n\n    def connection_made(self, transport):\n        raise RuntimeError(\"Invalid state: \"\n                           \"connection should have been established already.\")\n\n    def connection_lost(self, exc):\n        if self._write_ready_fut is not None:\n            # Never happens if peer disconnects after sending the whole content\n            # Thus disconnection is always an exception from user perspective\n            if exc is None:\n                self._write_ready_fut.set_exception(\n                    ConnectionError(\"Connection is closed by peer\"))\n            else:\n                self._write_ready_fut.set_exception(exc)","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/asyncio/base_events.py#L209-L245","documentation":"Error \"Connection closed by peer\" thrown in python/cpython.","triggerScenarios":"Raised in asyncio.base_events when the connection is closed by the remote peer while reading or writing.","commonSituations":"See trigger scenarios.","solutions":["Handle the disconnection gracefully: catch the exception around the await point and reconnect or clean up resources.","Verify the remote peer is not closing the connection prematurely; check server-side logs and timeouts.","Wrap reads/writes in try/except ConnectionResetError and treat it as end-of-stream."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}