diff --git a/redis/asyncio/connection.py b/redis/asyncio/connection.py index f0e6d3da4f..91961ba674 100644 --- a/redis/asyncio/connection.py +++ b/redis/asyncio/connection.py @@ -911,7 +911,13 @@ async def can_read(self, timeout: float = 0): """Poll the socket to see if there's data that can be read.""" if not self.is_connected: await self.connect() - return await self._parser.can_read(timeout) + try: + return await self._parser.can_read(timeout) + except OSError as e: + await self.disconnect() + raise ConnectionError( + f"Error while reading from {self.host}:{self.port}: {e.args}" + ) async def read_response(self, disable_decoding: bool = False): """Read the response from a previously sent command""" diff --git a/redis/connection.py b/redis/connection.py index ecbd32fc41..e0dcfc6323 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -808,7 +808,13 @@ def can_read(self, timeout=0): sock = self._sock if not sock: self.connect() - return self._parser.can_read(timeout) + try: + return self._parser.can_read(timeout) + except OSError as e: + self.disconnect() + raise ConnectionError( + f"Error while reading from {self.host}:{self.port}: {e.args}" + ) def read_response(self, disable_decoding=False): """Read the response from a previously sent command"""