{"record":{"id":"11509ccf0e5b87d8","repo":"redis/redis-py","slug":"error-while-reading-from-host-error-e-args","errorCode":null,"errorMessage":"Error while reading from {host_error}: {e.args}","messagePattern":"Error while reading from (.+?): (.+?)","errorType":"exception","errorClass":"ConnectionError","httpStatus":null,"severity":"warning","filePath":"redis/asyncio/connection.py","lineNumber":1228,"sourceCode":"            raise\n\n    async def send_command(self, *args: Any, **kwargs: Any) -> None:\n        \"\"\"Pack and send a command to the Redis server\"\"\"\n        await self.send_packed_command(\n            self.pack_command(*args), check_health=kwargs.get(\"check_health\", True)\n        )\n\n    @deprecated_function(\n        version=\"8.0.0\", reason=\"Use can_read() instead\", name=\"can_read_destructive\"\n    )\n    async def can_read_destructive(self) -> bool:\n        \"\"\"Check the socket to see if there's data loaded in the buffer.\"\"\"\n        try:\n            return await self._parser.can_read()\n        except OSError as e:\n            await self.disconnect(nowait=True)\n            host_error = self._host_error()\n            raise ConnectionError(f\"Error while reading from {host_error}: {e.args}\")\n\n    async def can_read(self) -> bool:\n        \"\"\"Check the socket to see if there's data loaded in the buffer.\"\"\"\n        # TODO: Rename this API; it detects pending data or dirty/closed\n        # connection state, not only whether application data can be read.\n        try:\n            return await self._parser.can_read()\n        except OSError as e:\n            await self.disconnect(nowait=True)\n            host_error = self._host_error()\n            raise ConnectionError(f\"Error while reading from {host_error}: {e.args}\")\n\n    async def read_response(\n        self,\n        disable_decoding: bool = False,\n        timeout: float | None = None,\n        *,\n        disconnect_on_error: bool = True,","sourceCodeStart":1210,"sourceCodeEnd":1246,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/asyncio/connection.py#L1210-L1246","documentation":"Raised as ConnectionError from the deprecated can_read_destructive() when the parser's can_read() raises OSError. The host_error string comes from _host_error() (host:port for TCP, path for UDS). The connection is disconnected nowait before re-raising. This method is deprecated in favor of can_read().","triggerScenarios":"Calling can_read_destructive() (deprecated, removed in 8.0) on a connection whose socket is already dead; the underlying asyncio reader hits OSError (EBADF/ECONNRESET) probing the buffer.","commonSituations":"Legacy code paths still calling the deprecated API; pubsub readiness checks after a server reset; testing harnesses probing connection state.","solutions":["Migrate to can_read() (can_read_destructive is deprecated since 8.0).","Wrap the call in try/except ConnectionError and reconnect on failure.","Ensure you are not probing a connection that was already disconnected."],"exampleFix":"// before\nif await conn.can_read_destructive():\n    ...\n// after\nif await conn.can_read():\n    ...","handlingStrategy":"try-catch","validationCode":"null","typeGuard":"from redis.exceptions import ConnectionError\n\ndef is_can_read_error(exc: BaseException) -> bool:\n    return isinstance(exc, ConnectionError) and 'reading from' in str(exc).lower()","tryCatchPattern":"from redis.exceptions import ConnectionError\n\ntry:\n    ready = await conn.can_read_destructive()\nexcept ConnectionError:\n    conn = await pool.get_connection()\n    ready = await conn.can_read()  # migrated off deprecated API","preventionTips":["Migrate off can_read_destructive() (deprecated since 8.0).","Never probe a connection you have already disconnected.","Wrap probe calls; reconnect on ConnectionError."],"tags":["network","read","deprecated","oserror","async","host-error"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}