Skip to content

Commit 795a5e6

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

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

redis/asyncio/connection.py

Lines changed: 11 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,9 @@ 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+
# wait for close to finish, except when handling errors and
842+
# forcecully disconnecting.
843+
if not nowait:
843844
await self._writer.wait_closed() # type: ignore[union-attr]
844845
except OSError:
845846
pass
@@ -894,10 +895,10 @@ async def send_packed_command(
894895
self._writer.writelines(command)
895896
await self._writer.drain()
896897
except asyncio.TimeoutError:
897-
await self.disconnect()
898+
await self.disconnect(nowait=True)
898899
raise TimeoutError("Timeout writing to socket") from None
899900
except OSError as e:
900-
await self.disconnect()
901+
await self.disconnect(nowait=True)
901902
if len(e.args) == 1:
902903
err_no, errmsg = "UNKNOWN", e.args[0]
903904
else:
@@ -907,7 +908,7 @@ async def send_packed_command(
907908
f"Error {err_no} while writing to socket. {errmsg}."
908909
) from e
909910
except BaseException:
910-
await self.disconnect()
911+
await self.disconnect(nowait=True)
911912
raise
912913

913914
async def send_command(self, *args: Any, **kwargs: Any) -> None:
@@ -923,7 +924,7 @@ async def can_read(self, timeout: float = 0):
923924
try:
924925
return await self._parser.can_read(timeout)
925926
except OSError as e:
926-
await self.disconnect()
927+
await self.disconnect(nowait=True)
927928
raise ConnectionError(
928929
f"Error while reading from {self.host}:{self.port}: {e.args}"
929930
)
@@ -974,15 +975,15 @@ async def read_response_without_lock(self, disable_decoding: bool = False):
974975
disable_decoding=disable_decoding
975976
)
976977
except asyncio.TimeoutError:
977-
await self.disconnect()
978+
await self.disconnect(nowait=True)
978979
raise TimeoutError(f"Timeout reading from {self.host}:{self.port}")
979980
except OSError as e:
980-
await self.disconnect()
981+
await self.disconnect(nowait=True)
981982
raise ConnectionError(
982983
f"Error while reading from {self.host}:{self.port} : {e.args}"
983984
)
984985
except BaseException:
985-
await self.disconnect()
986+
await self.disconnect(nowait=True)
986987
raise
987988

988989
if self.health_check_interval:

0 commit comments

Comments
 (0)