Skip to content

Commit

Permalink
Close a Connection whenever an exception is raised for send_command()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kristjanvalur committed Dec 15, 2022
1 parent c3d8a7c commit 1985884
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion redis/asyncio/connection.py
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion redis/connection.py
Expand Up @@ -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

Expand Down

0 comments on commit 1985884

Please sign in to comment.