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 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