{"record":{"id":"44b2c0ff2a6232e6","repo":"redis/redis-py","slug":"timeout-reading-from-host-error-44b2c0","errorCode":null,"errorMessage":"Timeout reading from {host_error}","messagePattern":"Timeout reading from (.+?)","errorType":"exception","errorClass":"TimeoutError","httpStatus":null,"severity":"error","filePath":"redis/connection.py","lineNumber":1419,"sourceCode":"        \"\"\"Read the response from a previously sent command\"\"\"\n\n        host_error = self._host_error()\n\n        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","sourceCodeStart":1401,"sourceCodeEnd":1437,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/connection.py#L1401-L1437","documentation":"Raised in read_response (connection.py:1416-1419) when socket.timeout fires while reading a reply. This is the read-side counterpart of the write timeout: the server did not produce a complete response within socket_timeout (or the per-call timeout). The connection is disconnected (unless disconnect_on_error=False) and redis.exceptions.TimeoutError is raised, including host:port.","triggerScenarios":"A command is sent and read_response is called with a socket_timeout that elapses before the full reply arrives — slow command (KEYS *, large ZRANGE, blocking BLPOP longer than timeout), server stalled, or an explicit per-call timeout passed to read_response that is shorter than the command duration.","commonSituations":"Blocking commands (BLPOP/BRPOP) with a socket_timeout shorter than the block duration; heavy O(N) commands (SORT, KEYS, SUNION); Lua scripts that run long; server under replication/AOF fsync stall; socket_timeout mis-sized for the workload.","solutions":["Use the per-call timeout for blocking commands: brpop('q', timeout=30) and read via a client that passes timeout.","Raise socket_timeout to exceed your slowest expected command.","Avoid expensive commands on hot paths; prefer SCAN over KEYS.","Set retry_on_timeout=True if timeouts are expected to be transient.","For blocking commands, ensure the timeout argument and socket_timeout are consistent."],"exampleFix":"# before\nr = redis.Redis(host=h, port=p, socket_timeout=1)\nr.brpop('queue', timeout=30)  # read times out at 1s\n# after\nr = redis.Redis(host=h, port=p, socket_timeout=35)\nr.brpop('queue', timeout=30)","handlingStrategy":"retry","validationCode":"# Align socket_timeout with your longest command/block\nslowest = 35  # e.g. BLPOP timeout=30 plus margin\nr = redis.Redis(\n    host=h, port=p,\n    socket_timeout=slowest,\n    retry_on_timeout=True,\n    retry=Retry(ExponentialBackoff(), 2),\n)","typeGuard":"null","tryCatchPattern":"from redis.exceptions import TimeoutError\nfor _ in range(2):\n    try:\n        return r.brpop('queue', timeout=30)\n    except TimeoutError as e:\n        if 'reading from' in str(e):\n            continue\n        raise","preventionTips":["For blocking commands set socket_timeout > the block timeout.","Avoid O(N) commands (KEYS, big SORT) on latency-sensitive paths.","Use retry_on_timeout=True for transient read stalls.","Pass per-call timeouts for one-off slow reads."],"tags":["timeout","network","read","blocking-commands"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}