Skip to content

Commit c665bea

Browse files
committed
PYTHON-4292 Fix FaaS heartbeat tests
1 parent 003f309 commit c665bea

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

pymongo/network_layer.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -325,29 +325,30 @@ def receive_data(conn: Connection, length: int, deadline: Optional[float]) -> me
325325
# could close the socket but that does not reliably cancel recv() calls
326326
# on all OSes.
327327
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)
336328
try:
337329
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)
338338
try:
339339
chunk_length = conn.conn.recv_into(mv[bytes_read:])
340340
except BLOCKING_IO_ERRORS:
341+
if conn.cancel_context.cancelled:
342+
raise _OperationCancelled("operation cancelled") from None
343+
# We reached the true deadline.
341344
raise socket.timeout("timed out") from None
342345
except socket.timeout:
343-
if deadline is not None and time.monotonic() >= deadline:
344-
# We reached the true deadline.
345-
raise
346346
if conn.cancel_context.cancelled:
347347
raise _OperationCancelled("operation cancelled") from None
348-
# Intermediate timeout, keep trying.
349348
continue
350349
except OSError as exc:
350+
if conn.cancel_context.cancelled:
351+
raise _OperationCancelled("operation cancelled") from None
351352
if _errno_from_exception(exc) == errno.EINTR:
352353
continue
353354
raise
@@ -356,6 +357,6 @@ def receive_data(conn: Connection, length: int, deadline: Optional[float]) -> me
356357

357358
bytes_read += chunk_length
358359
finally:
359-
conn.conn.settimeout(orig_timeout)
360+
conn.set_conn_timeout(orig_timeout)
360361

361362
return mv

0 commit comments

Comments
 (0)