{"id":"20e36bbddbdf5150","repo":"redis/redis-py","slug":"error-while-reading-from-host-error-e-args-20e36b","errorCode":null,"errorMessage":"Error while reading from {host_error} : {e.args}","messagePattern":"Error while reading from (.+?) : (.+?)","errorType":"exception","errorClass":"ConnectionError","httpStatus":null,"severity":"error","filePath":"redis/connection.py","lineNumber":1423,"sourceCode":"        try:\n            if self.protocol in [\"3\", 3]:\n                response = self._parser.read_response(\n                    disable_decoding=disable_decoding,\n                    push_request=push_request,\n                    timeout=timeout,\n                )\n            else:\n                response = self._parser.read_response(\n                    disable_decoding=disable_decoding, timeout=timeout\n                )\n        except socket.timeout:\n            if disconnect_on_error:\n                self.disconnect()\n            raise TimeoutError(f\"Timeout reading from {host_error}\")\n        except OSError as e:\n            if disconnect_on_error:\n                self.disconnect()\n            raise ConnectionError(f\"Error while reading from {host_error} : {e.args}\")\n        except BaseException:\n            # Also by default close in case of BaseException.  A lot of code\n            # relies on this behaviour when doing Command/Response pairs.\n            # See #1128.\n            if disconnect_on_error:\n                self.disconnect()\n            raise\n\n        if self.health_check_interval:\n            self.next_health_check = time.monotonic() + self.health_check_interval\n\n        if isinstance(response, ResponseError):\n            try:\n                raise response\n            finally:\n                del response  # avoid creating ref cycles\n        return response\n","sourceCodeStart":1405,"sourceCodeEnd":1441,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/connection.py#L1405-L1441","documentation":"Raised as a ConnectionError in read_response when an OSError (other than timeout) occurs while reading from the socket. The connection is disconnected (unless disconnect_on_error=False) and host_error plus e.args are embedded. This captures peer-reset / broken-pipe conditions on the read path.","triggerScenarios":"Issuing a command and calling read_response when the server or network drops the connection mid-response (ECONNRESET, EPIPE). Also fires when a parser-level read hits an OS error. Distinct from the timeout case (393).","commonSituations":"Server crash/restart during a response; firewall/NAT idle drop between command and response; proxy recycling upstream; OOM kill of the server; forked child reusing a parent socket.","solutions":["Rely on the connection pool to deliver a fresh connection on retry; wrap commands in retry_on_error=[ConnectionError].","Enable health_check_interval to prune dead connections proactively.","Examine e.args for the errno to distinguish reset from unreachable.","Stabilize the network/keepalive to reduce idle drops."],"exampleFix":"// before\nr = redis.Redis(host=h)\nval = r.get('k')\n// after\nr = redis.Redis(host=h, health_check_interval=30,\n                retry_on_error=[redis.ConnectionError])\nval = r.get('k')","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    val = r.get('k')\nexcept redis.exceptions.ConnectionError as e:\n    if 'while reading from' in str(e):\n        # peer reset mid-response; retry on a fresh connection\n        val = r.get('k')","preventionTips":["Configure retry_on_error=[ConnectionError] and health_check_interval.","Inspect e.args errno to diagnose resets vs unreachable.","Stabilize the network path and keepalive settings."],"tags":["network","connection-reset","read","transient"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}