72
72
EncryptedCollectionError ,
73
73
EncryptionError ,
74
74
InvalidOperation ,
75
- PyMongoError ,
76
75
ServerSelectionTimeoutError ,
77
76
)
78
77
from pymongo .network_layer import BLOCKING_IO_ERRORS , async_sendall
@@ -176,6 +175,9 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
176
175
ssl_context = ctx ,
177
176
)
178
177
host , port = parse_host (endpoint , _HTTPS_PORT )
178
+ sleep_usec = kms_context .usleep
179
+ if sleep_usec :
180
+ await asyncio .sleep (float (sleep_usec ) / 1e6 )
179
181
try :
180
182
conn = await _configured_socket ((host , port ), opts )
181
183
try :
@@ -201,13 +203,15 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
201
203
raise socket .timeout ("timed out" ) from None
202
204
finally :
203
205
conn .close ()
204
- except (PyMongoError , MongoCryptError ):
205
- raise # Propagate pymongo errors directly.
206
- except asyncio .CancelledError :
207
- raise
208
- except Exception as error :
209
- # Wrap I/O errors in PyMongo exceptions.
210
- _raise_connection_failure ((host , port ), error )
206
+ except MongoCryptError :
207
+ raise # Propagate MongoCryptError errors directly.
208
+ except Exception as exc :
209
+ remaining = _csot .remaining ()
210
+ if remaining is not None and remaining <= 0 :
211
+ # Wrap I/O errors in PyMongo exceptions.
212
+ _raise_connection_failure ((host , port ), exc )
213
+ # Mark this attempt as failed and defer to libmongocrypt to retry.
214
+ kms_context .fail ()
211
215
212
216
async def collection_info (self , database : str , filter : bytes ) -> Optional [bytes ]:
213
217
"""Get the collection info for a namespace.
0 commit comments