redis/redis-py · warning · 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 from the deprecated can_read_destructive() when the parser's can_read() raises an OSError. The connection is forcibly disconnected. This method is deprecated (use can_read() instead); the error indicates a read failure while probing the buffer for pending data.

Source

Thrown at redis/asyncio/connection.py:1228

            raise

    async def send_command(self, *args: Any, **kwargs: Any) -> None:
        """Pack and send a command to the Redis server"""
        await self.send_packed_command(
            self.pack_command(*args), check_health=kwargs.get("check_health", True)
        )

    @deprecated_function(
        version="8.0.0", reason="Use can_read() instead", name="can_read_destructive"
    )
    async def can_read_destructive(self) -> bool:
        """Check the socket to see if there's data loaded in the buffer."""
        try:
            return await self._parser.can_read()
        except OSError as e:
            await self.disconnect(nowait=True)
            host_error = self._host_error()
            raise ConnectionError(f"Error while reading from {host_error}: {e.args}")

    async def can_read(self) -> bool:
        """Check the socket to see if there's data loaded in the buffer."""
        # TODO: Rename this API; it detects pending data or dirty/closed
        # connection state, not only whether application data can be read.
        try:
            return await self._parser.can_read()
        except OSError as e:
            await self.disconnect(nowait=True)
            host_error = self._host_error()
            raise ConnectionError(f"Error while reading from {host_error}: {e.args}")

    async def read_response(
        self,
        disable_decoding: bool = False,
        timeout: float | None = None,
        *,
        disconnect_on_error: bool = True,

View on GitHub (pinned to da03cdc7e8)

Solutions

  1. Replace can_read_destructive() with can_read() - the deprecated form will be removed.
  2. Guard the call in try/except for ConnectionError and reconnect on failure.
  3. Use the higher-level PubSub.get_message(timeout=...) API instead of manual readiness probes.

Example fix

// before
if await conn.can_read_destructive():
    ...

// after
if await conn.can_read():
    ...
Defensive patterns

Strategy: validation

Validate before calling

import warnings
# Stop using the deprecated API entirely:
# replace can_read_destructive() with can_read().
warnings.filterwarnings("error", category=DeprecationWarning, module="redis")

Try / catch

from redis.exceptions import ConnectionError
try:
    ready = await conn.can_read()  # use the non-deprecated form
except ConnectionError:
    conn = await pool.get_connection()

Prevention

When it happens

Trigger: Calling the deprecated can_read_destructive() (emits a deprecation warning since 8.0.0) and the underlying parser.can_read() hits an OSError - typically a socket already closed by the peer or a half-open connection.

Common situations: Legacy code written against an older redis-py API; pubsub loops probing for data; a peer that closed the connection between the probe and the read.

Related errors


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