From 6d5c1d409f86a84eb7767c87f5e7a891cf0ceae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Mon, 25 Apr 2022 14:49:35 +0000 Subject: [PATCH] Replace OSError exceptions from `can_read` with `redis.ConnectionError` --- redis/asyncio/connection.py | 8 +++++++- redis/connection.py | 8 ++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/redis/asyncio/connection.py b/redis/asyncio/connection.py index f0e6d3da4f..7b1463be4d 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 0f97361e6e..c5ebfd084d 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -808,7 +808,11 @@ 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 {hosterr}" f" : {e.args}") def read_response(self, disable_decoding=False): """Read the response from a previously sent command""" @@ -1282,7 +1286,7 @@ class initializer. In the case of conflicting arguments, querystring def __init__( self, connection_class=Connection, max_connections=None, **connection_kwargs ): - max_connections = max_connections or 2 ** 31 + max_connections = max_connections or 2**31 if not isinstance(max_connections, int) or max_connections < 0: raise ValueError('"max_connections" must be a positive integer')