From 07e1f915c4e996030e2e303b46932110a582a411 Mon Sep 17 00:00:00 2001 From: Iris Ho Date: Thu, 8 Jan 2026 22:23:49 -0800 Subject: [PATCH 1/2] fix backoff calculation --- pymongo/asynchronous/client_session.py | 2 +- pymongo/synchronous/client_session.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pymongo/asynchronous/client_session.py b/pymongo/asynchronous/client_session.py index 27f26c35b2..545c8b247b 100644 --- a/pymongo/asynchronous/client_session.py +++ b/pymongo/asynchronous/client_session.py @@ -717,7 +717,7 @@ async def callback(session, custom_arg, custom_kwarg=None): while True: if retry: # Implement exponential backoff on retry. jitter = random.random() # noqa: S311 - backoff = jitter * min(_BACKOFF_INITIAL * (1.5**retry), _BACKOFF_MAX) + backoff = jitter * min(_BACKOFF_INITIAL * (1.5 ** (retry - 1)), _BACKOFF_MAX) if _would_exceed_time_limit(start_time, backoff): assert last_error is not None raise last_error diff --git a/pymongo/synchronous/client_session.py b/pymongo/synchronous/client_session.py index 28999bcd62..c4d02ded11 100644 --- a/pymongo/synchronous/client_session.py +++ b/pymongo/synchronous/client_session.py @@ -715,7 +715,7 @@ def callback(session, custom_arg, custom_kwarg=None): while True: if retry: # Implement exponential backoff on retry. jitter = random.random() # noqa: S311 - backoff = jitter * min(_BACKOFF_INITIAL * (1.5**retry), _BACKOFF_MAX) + backoff = jitter * min(_BACKOFF_INITIAL * (1.5 ** (retry - 1)), _BACKOFF_MAX) if _would_exceed_time_limit(start_time, backoff): assert last_error is not None raise last_error From 41a8638012910ba7ba39394844fd604db05fcbdf Mon Sep 17 00:00:00 2001 From: Iris Ho Date: Thu, 8 Jan 2026 22:23:59 -0800 Subject: [PATCH 2/2] fix test --- test/asynchronous/test_transactions.py | 4 +++- test/test_transactions.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/test/asynchronous/test_transactions.py b/test/asynchronous/test_transactions.py index 18f9778463..968594883e 100644 --- a/test/asynchronous/test_transactions.py +++ b/test/asynchronous/test_transactions.py @@ -677,7 +677,9 @@ async def callback(session): async with self.client.start_session() as s: await s.with_transaction(callback) end = time.monotonic() - self.assertLess(abs(end - start - (no_backoff_time + 2.2)), 1) # sum of 13 backoffs is 2.2 + self.assertLess( + abs(end - start - (no_backoff_time + 1.8)), 0.5 + ) # sum of 13 backoffs is 1.8 random.random = _original_random_random diff --git a/test/test_transactions.py b/test/test_transactions.py index 94d70396fc..0ceb456e58 100644 --- a/test/test_transactions.py +++ b/test/test_transactions.py @@ -661,7 +661,9 @@ def callback(session): with self.client.start_session() as s: s.with_transaction(callback) end = time.monotonic() - self.assertLess(abs(end - start - (no_backoff_time + 2.2)), 1) # sum of 13 backoffs is 2.2 + self.assertLess( + abs(end - start - (no_backoff_time + 1.8)), 0.5 + ) # sum of 13 backoffs is 1.8 random.random = _original_random_random