@@ -325,29 +325,30 @@ def receive_data(conn: Connection, length: int, deadline: Optional[float]) -> me
325
325
# could close the socket but that does not reliably cancel recv() calls
326
326
# on all OSes.
327
327
orig_timeout = conn .conn .gettimeout ()
328
- if deadline is not None :
329
- # CSOT: Update timeout. When the timeout has expired perform one
330
- # final non-blocking recv. This helps avoid spurious timeouts when
331
- # the response is actually already buffered on the client.
332
- short_timeout = min (max (deadline - time .monotonic (), 0 ), _POLL_TIMEOUT )
333
- else :
334
- short_timeout = _POLL_TIMEOUT
335
- conn .conn .settimeout (short_timeout )
336
328
try :
337
329
while bytes_read < length :
330
+ if deadline is not None :
331
+ # CSOT: Update timeout. When the timeout has expired perform one
332
+ # final non-blocking recv. This helps avoid spurious timeouts when
333
+ # the response is actually already buffered on the client.
334
+ short_timeout = min (max (deadline - time .monotonic (), 0 ), _POLL_TIMEOUT )
335
+ else :
336
+ short_timeout = _POLL_TIMEOUT
337
+ conn .set_conn_timeout (short_timeout )
338
338
try :
339
339
chunk_length = conn .conn .recv_into (mv [bytes_read :])
340
340
except BLOCKING_IO_ERRORS :
341
+ if conn .cancel_context .cancelled :
342
+ raise _OperationCancelled ("operation cancelled" ) from None
343
+ # We reached the true deadline.
341
344
raise socket .timeout ("timed out" ) from None
342
345
except socket .timeout :
343
- if deadline is not None and time .monotonic () >= deadline :
344
- # We reached the true deadline.
345
- raise
346
346
if conn .cancel_context .cancelled :
347
347
raise _OperationCancelled ("operation cancelled" ) from None
348
- # Intermediate timeout, keep trying.
349
348
continue
350
349
except OSError as exc :
350
+ if conn .cancel_context .cancelled :
351
+ raise _OperationCancelled ("operation cancelled" ) from None
351
352
if _errno_from_exception (exc ) == errno .EINTR :
352
353
continue
353
354
raise
@@ -356,6 +357,6 @@ def receive_data(conn: Connection, length: int, deadline: Optional[float]) -> me
356
357
357
358
bytes_read += chunk_length
358
359
finally :
359
- conn .conn . settimeout (orig_timeout )
360
+ conn .set_conn_timeout (orig_timeout )
360
361
361
362
return mv
0 commit comments