diff options
author | Kristján Valur Jónsson <sweskman@gmail.com> | 2022-09-29 11:23:07 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-29 14:23:07 +0300 |
commit | 652ca790b0bbaefa78278ddf57074316a3fb21bd (patch) | |
tree | 6c8b6a942851b79ab1f184b27a5e9a537d81807a /redis | |
parent | 9fe836698ca5930e39633b86839b6c1bae07237e (diff) | |
download | redis-py-652ca790b0bbaefa78278ddf57074316a3fb21bd.tar.gz |
Add `nowait` flag to `asyncio.Connection.disconnect()` (#2356)
* Don't wait for disconnect() when handling errors.
This can result in other errors such as timeouts.
* add CHANGES
* Update redis/asyncio/connection.py
Co-authored-by: Aarni Koskela <akx@iki.fi>
* await a task to try to diagnose unittest failures in CI
Co-authored-by: Aarni Koskela <akx@iki.fi>
Diffstat (limited to 'redis')
-rw-r--r-- | redis/asyncio/connection.py | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/redis/asyncio/connection.py b/redis/asyncio/connection.py index a470b6f..64848f4 100644 --- a/redis/asyncio/connection.py +++ b/redis/asyncio/connection.py @@ -836,7 +836,7 @@ class Connection: if str_if_bytes(await self.read_response()) != "OK": raise ConnectionError("Invalid Database") - async def disconnect(self) -> None: + async def disconnect(self, nowait: bool = False) -> None: """Disconnects from the Redis server""" try: async with async_timeout.timeout(self.socket_connect_timeout): @@ -846,8 +846,9 @@ class Connection: try: if os.getpid() == self.pid: self._writer.close() # type: ignore[union-attr] - # py3.6 doesn't have this method - if hasattr(self._writer, "wait_closed"): + # wait for close to finish, except when handling errors and + # forcefully disconnecting. + if not nowait: await self._writer.wait_closed() # type: ignore[union-attr] except OSError: pass @@ -902,10 +903,10 @@ class Connection: self._writer.writelines(command) await self._writer.drain() except asyncio.TimeoutError: - await self.disconnect() + await self.disconnect(nowait=True) raise TimeoutError("Timeout writing to socket") from None except OSError as e: - await self.disconnect() + await self.disconnect(nowait=True) if len(e.args) == 1: err_no, errmsg = "UNKNOWN", e.args[0] else: @@ -915,7 +916,7 @@ class Connection: f"Error {err_no} while writing to socket. {errmsg}." ) from e except Exception: - await self.disconnect() + await self.disconnect(nowait=True) raise async def send_command(self, *args: Any, **kwargs: Any) -> None: @@ -931,7 +932,7 @@ class Connection: try: return await self._parser.can_read(timeout) except OSError as e: - await self.disconnect() + await self.disconnect(nowait=True) raise ConnectionError( f"Error while reading from {self.host}:{self.port}: {e.args}" ) @@ -949,15 +950,15 @@ class Connection: disable_decoding=disable_decoding ) except asyncio.TimeoutError: - await self.disconnect() + await self.disconnect(nowait=True) raise TimeoutError(f"Timeout reading from {self.host}:{self.port}") except OSError as e: - await self.disconnect() + await self.disconnect(nowait=True) raise ConnectionError( f"Error while reading from {self.host}:{self.port} : {e.args}" ) except Exception: - await self.disconnect() + await self.disconnect(nowait=True) raise if self.health_check_interval: |