Skip to content

Commit b0b5800

Browse files
committed
update backoff criteria
1 parent 7e9f19f commit b0b5800

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

pymongo/asynchronous/pool.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,11 @@ def _handle_connection_error(self, error: Exception, phase: str) -> None:
10331033
# Handle system overload condition. When the base AutoReconnect is
10341034
# raised and we are not an sdam pool, add to backoff and add the
10351035
# appropriate error label.
1036-
if not self.is_sdam and "connection reset by peer" in str(error).lower():
1036+
if (
1037+
not self.is_sdam
1038+
and "connection reset by peer" in str(error).lower()
1039+
or ("connection closed" in str(error).lower() and self._backoff)
1040+
):
10371041
self._backoff += 1
10381042
error._add_error_label("SystemOverloaded")
10391043
error._add_error_label("Retryable")

pymongo/network_layer.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ class PyMongoBaseProtocol(Protocol):
254254
def __init__(self, timeout: Optional[float] = None):
255255
self.transport: Transport = None # type: ignore[assignment]
256256
self._timeout = timeout
257-
self._closing_error = asyncio.get_running_loop().create_future()
258257
self._closed = asyncio.get_running_loop().create_future()
259258
self._connection_lost = False
259+
self._closing_exception = None
260260

261261
def settimeout(self, timeout: float | None) -> None:
262262
self._timeout = timeout
@@ -270,11 +270,11 @@ def close(self, exc: Optional[Exception] = None) -> None:
270270
self.transport.abort()
271271
self._resolve_pending(exc)
272272
self._connection_lost = True
273+
self._closing_exception = exc
273274

274275
def connection_lost(self, exc: Optional[Exception] = None) -> None:
275-
if exc is not None and not self._closing_error.done():
276-
self._closing_error.set_exception(exc)
277276
self._resolve_pending(exc)
277+
self._closing_exception = exc
278278
if not self._closed.done():
279279
self._closed.set_result(None)
280280

@@ -325,7 +325,6 @@ def connection_made(self, transport: BaseTransport) -> None:
325325
"""
326326
self.transport = transport # type: ignore[assignment]
327327
self.transport.set_write_buffer_limits(MAX_MESSAGE_SIZE, MAX_MESSAGE_SIZE)
328-
super().connection_made(self)
329328

330329
async def read(self, request_id: Optional[int], max_message_size: int) -> tuple[bytes, int]:
331330
"""Read a single MongoDB Wire Protocol message from this connection."""
@@ -339,8 +338,6 @@ async def read(self, request_id: Optional[int], max_message_size: int) -> tuple[
339338
if self._done_messages:
340339
message = await self._done_messages.popleft()
341340
else:
342-
if self.transport and self.transport.is_closing():
343-
return await self._closing_error
344341
read_waiter = asyncio.get_running_loop().create_future()
345342
self._pending_messages.append(read_waiter)
346343
try:
@@ -478,6 +475,7 @@ def _resolve_pending(self, exc: Optional[Exception] = None) -> None:
478475
else:
479476
msg.set_exception(exc)
480477
self._done_messages.append(msg)
478+
self._pending_messages.clear()
481479

482480

483481
class PyMongoKMSProtocol(PyMongoBaseProtocol):
@@ -493,7 +491,6 @@ def connection_made(self, transport: BaseTransport) -> None:
493491
The transport argument is the transport representing the write side of the connection.
494492
"""
495493
self.transport = transport # type: ignore[assignment]
496-
super().connection_made(self)
497494

498495
def data_received(self, data: bytes) -> None:
499496
if self._connection_lost:

pymongo/synchronous/pool.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,11 @@ def _handle_connection_error(self, error: Exception, phase: str) -> None:
10291029
# Handle system overload condition. When the base AutoReconnect is
10301030
# raised and we are not an sdam pool, add to backoff and add the
10311031
# appropriate error label.
1032-
if not self.is_sdam and "connection reset by peer" in str(error).lower():
1032+
if (
1033+
not self.is_sdam
1034+
and "connection reset by peer" in str(error).lower()
1035+
or ("connection closed" in str(error).lower() and self._backoff)
1036+
):
10331037
self._backoff += 1
10341038
error._add_error_label("SystemOverloaded")
10351039
error._add_error_label("Retryable")

0 commit comments

Comments
 (0)