@@ -142,37 +142,6 @@ def __init__(
142
142
self .opts = opts
143
143
self ._spawned = False
144
144
145
- async def _async_kms_request (
146
- self ,
147
- kms_context : MongoCryptKmsContext ,
148
- host : str ,
149
- port : Optional [int ],
150
- opts : PoolOptions ,
151
- message : bytes ,
152
- ) -> None :
153
- from pymongo .network_layer import async_receive_data_socket # type: ignore[attr-defined]
154
-
155
- try :
156
- conn = await _configured_socket ((host , port ), opts )
157
- try :
158
- await async_sendall (conn , message )
159
- while kms_context .bytes_needed > 0 :
160
- # CSOT: update timeout.
161
- conn .settimeout (max (_csot .clamp_remaining (_KMS_CONNECT_TIMEOUT ), 0 ))
162
- data = await async_receive_data_socket (conn , kms_context .bytes_needed )
163
- kms_context .feed (data )
164
- except OSError as err :
165
- raise OSError ("KMS connection closed" ) from err
166
- except BLOCKING_IO_ERRORS :
167
- raise socket .timeout ("timed out" ) from None
168
- finally :
169
- conn .close ()
170
- except (PyMongoError , MongoCryptError ):
171
- raise # Propagate pymongo errors directly.
172
- except Exception as error :
173
- # Wrap I/O errors in PyMongo exceptions.
174
- _raise_connection_failure ((host , port ), error )
175
-
176
145
async def kms_request (self , kms_context : MongoCryptKmsContext ) -> None :
177
146
"""Complete a KMS request.
178
147
@@ -205,23 +174,30 @@ async def kms_request(self, kms_context: MongoCryptKmsContext) -> None:
205
174
)
206
175
host , port = parse_host (endpoint , _HTTPS_PORT )
207
176
try :
208
- if _IS_SYNC :
209
- conn = await _configured_socket (( host , port ), opts )
210
- try :
211
- await async_sendall ( conn , message )
212
- while kms_context . bytes_needed > 0 :
213
- # CSOT: update timeout.
214
- conn . settimeout ( max ( _csot . clamp_remaining ( _KMS_CONNECT_TIMEOUT ), 0 ))
177
+ conn = await _configured_socket (( host , port ), opts )
178
+ try :
179
+ await async_sendall ( conn , message )
180
+ while kms_context . bytes_needed > 0 :
181
+ # CSOT: update timeout.
182
+ conn . settimeout ( max ( _csot . clamp_remaining ( _KMS_CONNECT_TIMEOUT ), 0 ))
183
+ if _IS_SYNC :
215
184
data = conn .recv (kms_context .bytes_needed )
216
- if not data :
217
- raise OSError ("KMS connection closed" )
218
- kms_context .feed (data )
219
- except BLOCKING_IO_ERRORS :
220
- raise socket .timeout ("timed out" ) from None
221
- finally :
222
- conn .close ()
223
- else :
224
- await self ._async_kms_request (kms_context , host , port , opts , message )
185
+ else :
186
+ from pymongo .network_layer import (
187
+ async_receive_data_socket , # type: ignore[attr-defined]
188
+ )
189
+
190
+ data = await async_receive_data_socket (conn , kms_context .bytes_needed )
191
+ if not data :
192
+ raise OSError ("KMS connection closed" )
193
+ kms_context .feed (data )
194
+ # Async raises an OSError instead of returning empty bytes
195
+ except OSError as err :
196
+ raise OSError ("KMS connection closed" ) from err
197
+ except BLOCKING_IO_ERRORS :
198
+ raise socket .timeout ("timed out" ) from None
199
+ finally :
200
+ conn .close ()
225
201
except (PyMongoError , MongoCryptError ):
226
202
raise # Propagate pymongo errors directly.
227
203
except Exception as error :
0 commit comments