Skip to content

Commit f39ceb6

Browse files
committed
don't wait for disconnect() when handling errors (pr redis#2356)
This can result in other errors such as timeouts.
1 parent ab80350 commit f39ceb6

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

redis/asyncio/connection.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ async def on_connect(self) -> None:
825825
if str_if_bytes(await self.read_response()) != "OK":
826826
raise ConnectionError("Invalid Database")
827827

828-
async def disconnect(self) -> None:
828+
async def disconnect(self, nowait: bool = False) -> None:
829829
"""Disconnects from the Redis server"""
830830
try:
831831
async with async_timeout.timeout(self.socket_connect_timeout):
@@ -835,8 +835,9 @@ async def disconnect(self) -> None:
835835
try:
836836
if os.getpid() == self.pid:
837837
self._writer.close() # type: ignore[union-attr]
838-
# py3.6 doesn't have this method
839-
if hasattr(self._writer, "wait_closed"):
838+
# wait for close to finish, except when handling errors and
839+
# forcecully disconnecting.
840+
if not nowait:
840841
await self._writer.wait_closed() # type: ignore[union-attr]
841842
except OSError:
842843
pass
@@ -936,10 +937,10 @@ async def read_response(self, disable_decoding: bool = False):
936937
disable_decoding=disable_decoding
937938
)
938939
except asyncio.TimeoutError:
939-
await self.disconnect()
940+
await self.disconnect(nowait=True)
940941
raise TimeoutError(f"Timeout reading from {self.host}:{self.port}")
941942
except OSError as e:
942-
await self.disconnect()
943+
await self.disconnect(nowait=True)
943944
raise ConnectionError(
944945
f"Error while reading from {self.host}:{self.port} : {e.args}"
945946
)
@@ -948,7 +949,7 @@ async def read_response(self, disable_decoding: bool = False):
948949
# is subclass of Exception, not BaseException
949950
raise
950951
except Exception:
951-
await self.disconnect()
952+
await self.disconnect(nowait=True)
952953
raise
953954

954955
if self.health_check_interval:

0 commit comments

Comments
 (0)