@@ -787,6 +787,7 @@ async def read_response(
787787 self ,
788788 disable_decoding : bool = False ,
789789 timeout : Optional [float ] = None ,
790+ disconnect_on_error : bool = True ,
790791 ):
791792 """Read the response from a previously sent command"""
792793 read_timeout = timeout if timeout is not None else self .socket_timeout
@@ -802,22 +803,24 @@ async def read_response(
802803 )
803804 except asyncio .TimeoutError :
804805 if timeout is not None :
805- # user requested timeout, return None
806+ # user requested timeout, return None. Operation can be retried
806807 return None
807808 # it was a self.socket_timeout error.
808- await self .disconnect (nowait = True )
809+ if disconnect_on_error :
810+ await self .disconnect (nowait = True )
809811 raise TimeoutError (f"Timeout reading from { self .host } :{ self .port } " )
810812 except OSError as e :
811- await self .disconnect (nowait = True )
813+ if disconnect_on_error :
814+ await self .disconnect (nowait = True )
812815 raise ConnectionError (
813816 f"Error while reading from { self .host } :{ self .port } : { e .args } "
814817 )
815- except asyncio . CancelledError :
816- # need this check for 3.7, where CancelledError
817- # is subclass of Exception, not BaseException
818- raise
819- except Exception :
820- await self .disconnect (nowait = True )
818+ except BaseException :
819+ # Also by default close in case of BaseException. A lot of code
820+ # relies on this behaviour when doing Command/Response pairs.
821+ # See #1128.
822+ if disconnect_on_error :
823+ await self .disconnect (nowait = True )
821824 raise
822825
823826 if self .health_check_interval :
0 commit comments