{"record":{"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/6a6b581b48225afa0b76912d1028c6035baee932/redis/connection.py#L1405-L1441","documentation":"Raised in read_response (connection.py:1420-1423) as the generic OSError branch (after socket.timeout) while reading a reply. The underlying OS error args are included with host:port. Like the timeout case the connection is disconnected unless disconnect_on_error=False. It typically reflects a reset/closed peer or a corrupt read rather than a timeout.","triggerScenarios":"read_response encounters socket read error other than timeout — peer ECONNRESET mid-response, EPIPE, EBADF, truncated frame after a server-side disconnect, or a corrupt RESP stream that surfaces as an OSError in the low-level read.","commonSituations":"Server restarted/failed over; OOM kill of Redis dropping connections; network partition mid-response; maxclients eviction; a proxy closing the upstream after its own timeout.","solutions":["Catch ConnectionError and retry via the pool (idempotent commands only).","Use Sentinel/cluster for automatic failover to a healthy node.","Enable health_check_interval + socket_keepalive to detect drops earlier.","Check server INFO / dmesg for OOM or maxclients events.","For non-idempotent ops, use MULTI/EXEC or a dedupe key before retrying."],"exampleFix":"# before\nval = r.get('k')  # raises mid-response\n# after\nfrom redis.exceptions import ConnectionError\nfor _ in range(3):\n    try:\n        val = r.get('k'); break\n    except ConnectionError:\n        pass","handlingStrategy":"try-catch","validationCode":"# Resilient defaults + automatic failover\nr = redis.Redis(\n    host=h, port=p,\n    health_check_interval=30,\n    socket_keepalive=True,\n    socket_timeout=5,\n    retry_on_error=[redis.ConnectionError],\n)\n# Prefer Sentinel/cluster for HA so failed nodes are skipped\nsentinel = redis.Sentinel([(h, 26379)], socket_timeout=0.5)\nmaster = sentinel.master_for('mymaster')","typeGuard":"null","tryCatchPattern":"from redis.exceptions import ConnectionError\nfor _ in range(3):\n    try:\n        return r.get('k')  # idempotent read\n    except ConnectionError:\n        continue\nraise ConnectionError('Redis unavailable after retries')","preventionTips":["Use Sentinel/cluster for automatic failover.","Only retry idempotent commands, or guard non-idempotent ones with a dedupe key.","Monitor INFO/dmesg for OOM and maxclients drops.","Enable health checks to surface dead sockets."],"tags":["network","read","connection-reset","oserror"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}