@@ -828,7 +828,7 @@ async def on_connect(self) -> None:
828
828
if str_if_bytes (await self .read_response ()) != "OK" :
829
829
raise ConnectionError ("Invalid Database" )
830
830
831
- async def disconnect (self ) -> None :
831
+ async def disconnect (self , nowait : bool = False ) -> None :
832
832
"""Disconnects from the Redis server"""
833
833
try :
834
834
async with async_timeout .timeout (self .socket_connect_timeout ):
@@ -838,8 +838,7 @@ async def disconnect(self) -> None:
838
838
try :
839
839
if os .getpid () == self .pid :
840
840
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 :
843
842
await self ._writer .wait_closed () # type: ignore[union-attr]
844
843
except OSError :
845
844
pass
@@ -894,10 +893,10 @@ async def send_packed_command(
894
893
self ._writer .writelines (command )
895
894
await self ._writer .drain ()
896
895
except asyncio .TimeoutError :
897
- await self .disconnect ()
896
+ await self .disconnect (nowait = True )
898
897
raise TimeoutError ("Timeout writing to socket" ) from None
899
898
except OSError as e :
900
- await self .disconnect ()
899
+ await self .disconnect (nowait = True )
901
900
if len (e .args ) == 1 :
902
901
err_no , errmsg = "UNKNOWN" , e .args [0 ]
903
902
else :
@@ -907,7 +906,7 @@ async def send_packed_command(
907
906
f"Error { err_no } while writing to socket. { errmsg } ."
908
907
) from e
909
908
except BaseException :
910
- await self .disconnect ()
909
+ await self .disconnect (nowait = True )
911
910
raise
912
911
913
912
async def send_command (self , * args : Any , ** kwargs : Any ) -> None :
@@ -923,7 +922,7 @@ async def can_read(self, timeout: float = 0):
923
922
try :
924
923
return await self ._parser .can_read (timeout )
925
924
except OSError as e :
926
- await self .disconnect ()
925
+ await self .disconnect (nowait = True )
927
926
raise ConnectionError (
928
927
f"Error while reading from { self .host } :{ self .port } : { e .args } "
929
928
)
@@ -974,15 +973,15 @@ async def read_response_without_lock(self, disable_decoding: bool = False):
974
973
disable_decoding = disable_decoding
975
974
)
976
975
except asyncio .TimeoutError :
977
- await self .disconnect ()
976
+ await self .disconnect (nowait = True )
978
977
raise TimeoutError (f"Timeout reading from { self .host } :{ self .port } " )
979
978
except OSError as e :
980
- await self .disconnect ()
979
+ await self .disconnect (nowait = True )
981
980
raise ConnectionError (
982
981
f"Error while reading from { self .host } :{ self .port } : { e .args } "
983
982
)
984
983
except BaseException :
985
- await self .disconnect ()
984
+ await self .disconnect (nowait = True )
986
985
raise
987
986
988
987
if self .health_check_interval :
0 commit comments