Skip to content

Commit c25be04

Browse files
Replace OSError exceptions from can_read with redis.ConnectionError (#2140)
* Replace OSError exceptions from `can_read` with `redis.ConnectionError` * Fix formatting * Revert unintended formatting change
1 parent 963843b commit c25be04

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

redis/asyncio/connection.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,13 @@ async def can_read(self, timeout: float = 0):
911911
"""Poll the socket to see if there's data that can be read."""
912912
if not self.is_connected:
913913
await self.connect()
914-
return await self._parser.can_read(timeout)
914+
try:
915+
return await self._parser.can_read(timeout)
916+
except OSError as e:
917+
await self.disconnect()
918+
raise ConnectionError(
919+
f"Error while reading from {self.host}:{self.port}: {e.args}"
920+
)
915921

916922
async def read_response(self, disable_decoding: bool = False):
917923
"""Read the response from a previously sent command"""

redis/connection.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,13 @@ def can_read(self, timeout=0):
808808
sock = self._sock
809809
if not sock:
810810
self.connect()
811-
return self._parser.can_read(timeout)
811+
try:
812+
return self._parser.can_read(timeout)
813+
except OSError as e:
814+
self.disconnect()
815+
raise ConnectionError(
816+
f"Error while reading from {self.host}:{self.port}: {e.args}"
817+
)
812818

813819
def read_response(self, disable_decoding=False):
814820
"""Read the response from a previously sent command"""

0 commit comments

Comments
 (0)