Skip to content

Commit 0a2af6d

Browse files
committed
Don't wait for disconnect() when handling errors.
This can result in other errors such as timeouts.
1 parent b5ebada commit 0a2af6d

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

redis/asyncio/connection.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ async def on_connect(self) -> None:
828828
if str_if_bytes(await self.read_response()) != "OK":
829829
raise ConnectionError("Invalid Database")
830830

831-
async def disconnect(self) -> None:
831+
async def disconnect(self, nowait: bool = False) -> None:
832832
"""Disconnects from the Redis server"""
833833
try:
834834
async with async_timeout.timeout(self.socket_connect_timeout):
@@ -838,8 +838,7 @@ async def disconnect(self) -> None:
838838
try:
839839
if os.getpid() == self.pid:
840840
self._writer.close() # type: ignore[union-attr]
841-
# py3.6 doesn't have this method
842-
if hasattr(self._writer, "wait_closed"):
841+
if not nowait:
843842
await self._writer.wait_closed() # type: ignore[union-attr]
844843
except OSError:
845844
pass
@@ -894,10 +893,10 @@ async def send_packed_command(
894893
self._writer.writelines(command)
895894
await self._writer.drain()
896895
except asyncio.TimeoutError:
897-
await self.disconnect()
896+
await self.disconnect(nowait=True)
898897
raise TimeoutError("Timeout writing to socket") from None
899898
except OSError as e:
900-
await self.disconnect()
899+
await self.disconnect(nowait=True)
901900
if len(e.args) == 1:
902901
err_no, errmsg = "UNKNOWN", e.args[0]
903902
else:
@@ -907,7 +906,7 @@ async def send_packed_command(
907906
f"Error {err_no} while writing to socket. {errmsg}."
908907
) from e
909908
except BaseException:
910-
await self.disconnect()
909+
await self.disconnect(nowait=True)
911910
raise
912911

913912
async def send_command(self, *args: Any, **kwargs: Any) -> None:
@@ -923,7 +922,7 @@ async def can_read(self, timeout: float = 0):
923922
try:
924923
return await self._parser.can_read(timeout)
925924
except OSError as e:
926-
await self.disconnect()
925+
await self.disconnect(nowait=True)
927926
raise ConnectionError(
928927
f"Error while reading from {self.host}:{self.port}: {e.args}"
929928
)
@@ -974,15 +973,15 @@ async def read_response_without_lock(self, disable_decoding: bool = False):
974973
disable_decoding=disable_decoding
975974
)
976975
except asyncio.TimeoutError:
977-
await self.disconnect()
976+
await self.disconnect(nowait=True)
978977
raise TimeoutError(f"Timeout reading from {self.host}:{self.port}")
979978
except OSError as e:
980-
await self.disconnect()
979+
await self.disconnect(nowait=True)
981980
raise ConnectionError(
982981
f"Error while reading from {self.host}:{self.port} : {e.args}"
983982
)
984983
except BaseException:
985-
await self.disconnect()
984+
await self.disconnect(nowait=True)
986985
raise
987986

988987
if self.health_check_interval:

0 commit comments

Comments
 (0)