redis/redis-py · error · ConnectionError

Error while reading from {host_error} : {e.args}

Error message

Error while reading from {host_error} : {e.args}

What it means

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.

Source

Thrown at redis/connection.py:1423

        try:
            if self.protocol in ["3", 3]:
                response = self._parser.read_response(
                    disable_decoding=disable_decoding,
                    push_request=push_request,
                    timeout=timeout,
                )
            else:
                response = self._parser.read_response(
                    disable_decoding=disable_decoding, timeout=timeout
                )
        except socket.timeout:
            if disconnect_on_error:
                self.disconnect()
            raise TimeoutError(f"Timeout reading from {host_error}")
        except OSError as e:
            if disconnect_on_error:
                self.disconnect()
            raise ConnectionError(f"Error while reading from {host_error} : {e.args}")
        except BaseException:
            # Also by default close in case of BaseException.  A lot of code
            # relies on this behaviour when doing Command/Response pairs.
            # See #1128.
            if disconnect_on_error:
                self.disconnect()
            raise

        if self.health_check_interval:
            self.next_health_check = time.monotonic() + self.health_check_interval

        if isinstance(response, ResponseError):
            try:
                raise response
            finally:
                del response  # avoid creating ref cycles
        return response

View on GitHub (pinned to da03cdc7e8)

Solutions

  1. Rely on the connection pool to deliver a fresh connection on retry; wrap commands in retry_on_error=[ConnectionError].
  2. Enable health_check_interval to prune dead connections proactively.
  3. Examine e.args for the errno to distinguish reset from unreachable.
  4. Stabilize the network/keepalive to reduce idle drops.

Example fix

// before
r = redis.Redis(host=h)
val = r.get('k')
// after
r = redis.Redis(host=h, health_check_interval=30,
                retry_on_error=[redis.ConnectionError])
val = r.get('k')
Defensive patterns

Strategy: retry

Try / catch

try:
    val = r.get('k')
except redis.exceptions.ConnectionError as e:
    if 'while reading from' in str(e):
        # peer reset mid-response; retry on a fresh connection
        val = r.get('k')

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04). Data as JSON: /data/errors/20e36bbddbdf5150.json. Report an issue: GitHub.