@@ -559,7 +559,7 @@ async def command(
559559 )
560560 except (OperationFailure , NotPrimaryError ):
561561 raise
562- # Catch socket.error, KeyboardInterrupt, etc. and close ourselves.
562+ # Catch socket.error, KeyboardInterrupt, CancelledError, etc. and close ourselves.
563563 except BaseException as error :
564564 self ._raise_connection_failure (error )
565565
@@ -576,6 +576,7 @@ async def send_message(self, message: bytes, max_doc_size: int) -> None:
576576
577577 try :
578578 await async_sendall (self .conn , message )
579+ # Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
579580 except BaseException as error :
580581 self ._raise_connection_failure (error )
581582
@@ -586,6 +587,7 @@ async def receive_message(self, request_id: Optional[int]) -> Union[_OpReply, _O
586587 """
587588 try :
588589 return await receive_message (self , request_id , self .max_message_size )
590+ # Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
589591 except BaseException as error :
590592 self ._raise_connection_failure (error )
591593
@@ -1269,6 +1271,7 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
12691271
12701272 try :
12711273 sock = await _configured_socket (self .address , self .opts )
1274+ # Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
12721275 except BaseException as error :
12731276 async with self .lock :
12741277 self .active_contexts .discard (tmp_context )
@@ -1308,6 +1311,7 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
13081311 handler .contribute_socket (conn , completed_handshake = False )
13091312
13101313 await conn .authenticate ()
1314+ # Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
13111315 except BaseException :
13121316 async with self .lock :
13131317 self .active_contexts .discard (conn .cancel_context )
@@ -1369,6 +1373,7 @@ async def checkout(
13691373 async with self .lock :
13701374 self .active_contexts .add (conn .cancel_context )
13711375 yield conn
1376+ # Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
13721377 except BaseException :
13731378 # Exception in caller. Ensure the connection gets returned.
13741379 # Note that when pinned is True, the session owns the
@@ -1515,6 +1520,7 @@ async def _get_conn(
15151520 async with self ._max_connecting_cond :
15161521 self ._pending -= 1
15171522 self ._max_connecting_cond .notify ()
1523+ # Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
15181524 except BaseException :
15191525 if conn :
15201526 # We checked out a socket but authentication failed.
0 commit comments