Skip to content

Commit fe9316e

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

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
@@ -839,7 +839,7 @@ async def on_connect(self) -> None:
839839
if str_if_bytes(await self.read_response()) != "OK":
840840
raise ConnectionError("Invalid Database")
841841

842-
async def disconnect(self) -> None:
842+
async def disconnect(self, nowait: bool = False) -> None:
843843
"""Disconnects from the Redis server"""
844844
try:
845845
async with async_timeout.timeout(self.socket_connect_timeout):
@@ -849,8 +849,9 @@ async def disconnect(self) -> None:
849849
try:
850850
if os.getpid() == self.pid:
851851
self._writer.close() # type: ignore[union-attr]
852-
# py3.6 doesn't have this method
853-
if hasattr(self._writer, "wait_closed"):
852+
# wait for close to finish, except when handling errors and
853+
# forcecully disconnecting.
854+
if not nowait:
854855
await self._writer.wait_closed() # type: ignore[union-attr]
855856
except OSError:
856857
pass
@@ -905,10 +906,10 @@ async def send_packed_command(
905906
self._writer.writelines(command)
906907
await self._writer.drain()
907908
except asyncio.TimeoutError:
908-
await self.disconnect()
909+
await self.disconnect(nowait=True)
909910
raise TimeoutError("Timeout writing to socket") from None
910911
except OSError as e:
911-
await self.disconnect()
912+
await self.disconnect(nowait=True)
912913
if len(e.args) == 1:
913914
err_no, errmsg = "UNKNOWN", e.args[0]
914915
else:
@@ -918,7 +919,7 @@ async def send_packed_command(
918919
f"Error {err_no} while writing to socket. {errmsg}."
919920
) from e
920921
except BaseException:
921-
await self.disconnect()
922+
await self.disconnect(nowait=True)
922923
raise
923924

924925
async def send_command(self, *args: Any, **kwargs: Any) -> None:
@@ -934,7 +935,7 @@ async def can_read(self, timeout: float = 0):
934935
try:
935936
return await self._parser.can_read(timeout)
936937
except OSError as e:
937-
await self.disconnect()
938+
await self.disconnect(nowait=True)
938939
raise ConnectionError(
939940
f"Error while reading from {self.host}:{self.port}: {e.args}"
940941
)
@@ -985,15 +986,15 @@ async def read_response_without_lock(self, disable_decoding: bool = False):
985986
disable_decoding=disable_decoding
986987
)
987988
except asyncio.TimeoutError:
988-
await self.disconnect()
989+
await self.disconnect(nowait=True)
989990
raise TimeoutError(f"Timeout reading from {self.host}:{self.port}")
990991
except OSError as e:
991-
await self.disconnect()
992+
await self.disconnect(nowait=True)
992993
raise ConnectionError(
993994
f"Error while reading from {self.host}:{self.port} : {e.args}"
994995
)
995996
except BaseException:
996-
await self.disconnect()
997+
await self.disconnect(nowait=True)
997998
raise
998999

9991000
if self.health_check_interval:

0 commit comments

Comments
 (0)