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 can_read() when an OSError occurs while polling the parser/socket for pending data. The connection is disconnected and the OS error args are included. can_read is used to detect pending data (e.g. for pubsub invalidation drains) before a blocking read.

Source

Thrown at redis/connection.py:1391

            check_health=kwargs.get("check_health", True),
        )

    def can_read(self, timeout: float = 0) -> bool:
        """Poll the socket to see if there's data that can be read."""
        # TODO: Rename this API; it detects pending data or dirty/closed
        # connection state, not only whether application data can be read.
        sock = self._sock
        if not sock:
            self.connect()

        host_error = self._host_error()

        try:
            return self._parser.can_read(timeout)

        except OSError as e:
            self.disconnect()
            raise ConnectionError(f"Error while reading from {host_error}: {e.args}")

    def read_response(
        self,
        disable_decoding=False,
        *,
        timeout: Union[float, object] = SENTINEL,
        disconnect_on_error=True,
        push_request=False,
    ):
        """Read the response from a previously sent command"""

        host_error = self._host_error()

        try:
            if self.protocol in ["3", 3]:
                response = self._parser.read_response(
                    disable_decoding=disable_decoding,
                    push_request=push_request,

View on GitHub (pinned to da03cdc7e8)

Solutions

  1. Catch ConnectionError around pubsub/invalidation loops and reconnect.
  2. Ensure health_check_interval is set so dead connections are detected proactively.
  3. Avoid sharing connections across forked processes; create a new client after fork.
  4. Inspect e.args for the OS errno to diagnose reset vs. timeout vs. unreachable.

Example fix

// before
msg = pubsub.get_message()
// after
try:
    msg = pubsub.get_message()
except redis.ConnectionError:
    pubsub.reset()
    msg = pubsub.get_message()
Defensive patterns

Strategy: try-catch

Try / catch

try:
    msg = pubsub.get_message()
except redis.exceptions.ConnectionError as e:
    if 'while reading from' in str(e):
        pubsub.reset()
        msg = pubsub.get_message()

Prevention

When it happens

Trigger: Calling can_read() (used by pubsub get_message, client-side caching invalidation processing, and health probing) on a socket that raises OSError, e.g. the peer reset the connection between commands. The error embeds host_error and e.args.

Common situations: Pubsub subscriber whose connection was killed by the server; client-side caching draining invalidations on a dead socket; idle connection dropped by a NAT then polled; forked process using a stale socket.

Related errors


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