6464from pymongo .asynchronous .cursor import AsyncCursor
6565from pymongo .asynchronous .database import AsyncDatabase
6666from pymongo .asynchronous .mongo_client import AsyncMongoClient
67- from pymongo .asynchronous .pool import _configured_socket , _raise_connection_failure
67+ from pymongo .asynchronous .pool import (
68+ _configured_socket ,
69+ _get_timeout_details ,
70+ _raise_connection_failure ,
71+ )
6872from pymongo .common import CONNECT_TIMEOUT
6973from pymongo .daemon import _spawn_daemon
7074from pymongo .encryption_options import AutoEncryptionOpts , RangeOpts
8993if TYPE_CHECKING :
9094 from pymongocrypt .mongocrypt import MongoCryptKmsContext
9195
96+ from pymongo .typings import _Address
97+
9298
9399_IS_SYNC = False
94100
104110_KEY_VAULT_OPTS = CodecOptions (document_class = RawBSONDocument )
105111
106112
113+ async def _connect_kms (address : _Address , opts : PoolOptions ):
114+ try :
115+ return await _configured_socket (address , opts )
116+ except Exception as exc :
117+ _raise_connection_failure (address , exc , timeout_details = _get_timeout_details (opts ))
118+
119+
107120@contextlib .contextmanager
108121def _wrap_encryption_errors () -> Iterator [None ]:
109122 """Context manager to wrap encryption related errors."""
@@ -176,13 +189,13 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
176189 socket_timeout = connect_timeout ,
177190 ssl_context = ctx ,
178191 )
179- host , port = parse_host (endpoint , _HTTPS_PORT )
192+ address = parse_host (endpoint , _HTTPS_PORT )
180193 sleep_u = kms_context .usleep
181194 if sleep_u :
182195 sleep_sec = float (sleep_u ) / 1e6
183196 await asyncio .sleep (sleep_sec )
184197 try :
185- conn = await _configured_socket (( host , port ) , opts )
198+ conn = await _connect_kms ( address , opts )
186199 try :
187200 await async_sendall (conn , message )
188201 while kms_context .bytes_needed > 0 :
@@ -199,19 +212,21 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
199212 if not data :
200213 raise OSError ("KMS connection closed" )
201214 kms_context .feed (data )
202- except BLOCKING_IO_ERRORS :
203- raise socket .timeout ("timed out" ) from None
215+ except MongoCryptError :
216+ raise # Propagate MongoCryptError errors directly.
217+ except Exception as exc :
218+ # Wrap I/O errors in PyMongo exceptions.
219+ if isinstance (exc , BLOCKING_IO_ERRORS ):
220+ exc = socket .timeout ("timed out" )
221+ _raise_connection_failure (address , exc , timeout_details = _get_timeout_details (opts ))
204222 finally :
205223 conn .close ()
206224 except MongoCryptError :
207225 raise # Propagate MongoCryptError errors directly.
208226 except Exception as exc :
209227 remaining = _csot .remaining ()
210- if isinstance (exc , (socket .timeout , NetworkTimeout )) or (
211- remaining is not None and remaining <= 0
212- ):
213- # Wrap I/O errors in PyMongo exceptions.
214- _raise_connection_failure ((host , port ), exc )
228+ if isinstance (exc , NetworkTimeout ) or (remaining is not None and remaining <= 0 ):
229+ raise
215230 # Mark this attempt as failed and defer to libmongocrypt to retry.
216231 try :
217232 kms_context .fail ()
0 commit comments