From 1985884e635a87c9e072fa5bc12f14da563b5641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Thu, 15 Dec 2022 13:01:47 +0000 Subject: [PATCH] Close a Connection whenever an exception is raised for send_command() The api does not allow for a "resume", e.g. on a timeout, because an unknown number of bytes has been sent and an internal send state is not maintained. Therefore, there is no point in keeping the connection open. --- redis/asyncio/connection.py | 6 +++++- redis/connection.py | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/redis/asyncio/connection.py b/redis/asyncio/connection.py index 49d08a1c68..aea28a55f1 100644 --- a/redis/asyncio/connection.py +++ b/redis/asyncio/connection.py @@ -763,7 +763,11 @@ async def send_packed_command( raise ConnectionError( f"Error {err_no} while writing to socket. {errmsg}." ) from e - except Exception: + except BaseException: + # The send_packed_command api does not support re-trying a partially + # sent message, so there is no point in keeping the connection open. + # An unknown number of bytes has been sent and the connection is therefore + # unusable. await self.disconnect(nowait=True) raise diff --git a/redis/connection.py b/redis/connection.py index 73d10f64a7..2bbe683c77 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -778,7 +778,11 @@ def send_packed_command(self, command, check_health=True): errno = e.args[0] errmsg = e.args[1] raise ConnectionError(f"Error {errno} while writing to socket. {errmsg}.") - except Exception: + except BaseException: + # The send_packed_command api does not support re-trying a partially + # sent message, so there is no point in keeping the connection open. + # An unknown number of bytes has been sent and the connection is therefore + # unusable. self.disconnect() raise