From 3f0c0158746d0b90f33625860f5d1820fa0dadd3 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 1/2] 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 4f19153318..168ee4d7de 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 dce0735450..4ace49cb25 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 From e80f72e08f41d4b447af2883e956cda678d09813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Thu, 15 Dec 2022 13:10:39 +0000 Subject: [PATCH 2/2] add CHANGES --- CHANGES | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES b/CHANGES index b16fbce464..29af2c8ff5 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,4 @@ + * Close Connection on all send_command() errors (#2516) * Documentation fix: password protected socket connection (#2374) * Allow `timeout=None` in `PubSub.get_message()` to wait forever * add `nowait` flag to `asyncio.Connection.disconnect()`