From 2338e560476f0bdb4129d24c2a21ace057af0142 Mon Sep 17 00:00:00 2001 From: Atharva Chavhan Date: Thu, 3 Dec 2026 11:19:17 +0530 Subject: [PATCH] fix(auth): honor _retry_stop_after_attempts_count as total attempts DatabricksRetryPolicy mapped _retry_stop_after_attempts_count 1:1 onto urllib3's `total`, which counts retries after the first request, so the knob bought one extra wire attempt (count=1 still retried once). Set the initial `total` to one less than the configured count, and index the backoff by retries already performed so backoff timing is unchanged. Fixes #890 Signed-off-by: Atharva Chavhan --- CHANGELOG.md | 1 + src/databricks/sql/auth/retry.py | 28 +++++++++++++++------- tests/unit/test_retry.py | 40 +++++++++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 12 deletions(-) mode change 100755 => 100644 src/databricks/sql/auth/retry.py diff --git a/CHANGELOG.md b/CHANGELOG.md index c31d6927f..6a72e5c11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ # Unreleased - Honor `_port` and `_connection_uri` on kernel connections (`use_kernel=True`), as Thrift does. Requires a databricks-sql-kernel release that includes databricks/databricks-sql-kernel#505. - Speed up `fetchone()`, `fetchmany()` and `fetchall()` by building each result `Row` with fewer Python calls. The rows are unchanged. Large results and many concurrent cursors in one process benefit most. +- Honor `_retry_stop_after_attempts_count` as the total number of attempts (first request plus retries) instead of allowing one extra wire attempt: with a count of 1, a failed request is no longer retried. # 4.10.0 (2026-11-24) - Upgrade Databricks SQL Kernel to 1.5.0, with lower overhead for buffered fetches and closing drained cursors, and Arrow and Quinn security updates. diff --git a/src/databricks/sql/auth/retry.py b/src/databricks/sql/auth/retry.py old mode 100755 new mode 100644 index 676ca8fc1..0e1e9107c --- a/src/databricks/sql/auth/retry.py +++ b/src/databricks/sql/auth/retry.py @@ -65,8 +65,9 @@ class DatabricksRetryPolicy(Retry): Float of seconds for the maximum delay between retries. :param stop_after_attempts_count: - Integer maximum number of attempts that will be retried. This is an alias for urllib3's - `total`. + Integer maximum number of attempts in total (the initial request plus + retries). urllib3's `total` counts only retries after the first + request, so it is set to one less than this value. :param stop_after_attempts_duration: Float of maximum number of seconds within which a request may be retried starting from @@ -114,14 +115,18 @@ def __init__( _history: Optional[Tuple[RequestHistory, ...]] = urllib3_kwargs.get("history") if not _history: - # no attempts were made so we can retry the current command as many times as specified - # by the user - _attempts_remaining = self.stop_after_attempts_count + # No attempts have been made yet. urllib3's `total` counts retries + # *after* the first request, while `stop_after_attempts_count` is + # the total number of attempts (first request + retries), so the + # initial `total` is one less than the configured count. With a + # count of 1, `total` is 0 and urllib3 raises MaxRetryError after + # the single attempt instead of retrying once more. + _attempts_remaining = max(0, self.stop_after_attempts_count - 1) else: # at least one of our attempts has been consumed, and urllib3 will have set a total - # `total` is a counter that begins equal to self.stop_after_attempts_count and is - # decremented after each unsuccessful request. When `total` is zero, urllib3 raises a - # MaxRetryError + # `total` is a counter that begins equal to self.stop_after_attempts_count - 1 and is + # decremented after each unsuccessful request. When `total` goes below zero, urllib3 + # raises a MaxRetryError _total: int = urllib3_kwargs.pop("total") _attempts_remaining = _total @@ -313,7 +318,12 @@ def get_backoff_time(self) -> float: :return: """ - current_attempt = self.stop_after_attempts_count - int(self.total or 0) + # Index the backoff by the retries already performed (the length of + # the retry history), not by the remaining `total`: `total` counts + # retries after the first request, so deriving the attempt from it + # would shift the backoff curve by one step now that the initial + # `total` is one less than the configured attempt count. + current_attempt = len(self.history or ()) proposed_backoff = (2**current_attempt) * self.delay_min library_version = version("urllib3") diff --git a/tests/unit/test_retry.py b/tests/unit/test_retry.py index 7abab509d..63141875d 100644 --- a/tests/unit/test_retry.py +++ b/tests/unit/test_retry.py @@ -49,7 +49,10 @@ def test_sleep__no_retry_after(self, t_mock, retry_policy, error_history): @patch("time.sleep") def test_sleep__no_retry_after_header__multiple_retries(self, t_mock, retry_policy): - num_attempts = retry_policy.stop_after_attempts_count + # With stop_after_attempts_count=3 the policy allows 3 attempts in + # total, i.e. 2 (sleep, increment) pairs complete before the 3rd + # increment exhausts it — see test_stop_after_attempts_count below. + num_attempts = retry_policy.stop_after_attempts_count - 1 retry_policy._retry_start_time = time.time() retry_policy.command_type = CommandType.OTHER @@ -77,8 +80,10 @@ def test_sleep__no_retry_after_header__multiple_retries(self, t_mock, retry_poli @patch("time.sleep") def test_excessive_retry_attempts_error(self, t_mock, retry_policy): - # Attempting more than stop_after_attempt_count - num_attempts = retry_policy.stop_after_attempts_count + 1 + # Attempting stop_after_attempts_count times exhausts the policy: + # that many attempts in total are allowed, and the next increment + # raises MaxRetryError. + num_attempts = retry_policy.stop_after_attempts_count retry_policy._retry_start_time = time.time() retry_policy.command_type = CommandType.OTHER @@ -89,6 +94,35 @@ def test_excessive_retry_attempts_error(self, t_mock, retry_policy): # Internally urllib3 calls the increment function generating a new instance for every retry retry_policy = retry_policy.increment() + @pytest.mark.parametrize("count", [1, 2, 3, 5, 30]) + def test_stop_after_attempts_count_is_total_attempts(self, count): + # Regression test for #890 (ERRORRECOV-013): the knob counts total + # attempts (first request + retries), not retries after the first + # request. urllib3's `total` is one less than the configured count. + policy = self._make_retry_policy(stop_after_attempts_count=count) + policy._command_type = None + policy._retry_start_time = None + assert policy.total == count - 1 + + attempts = 1 # the initial request + while True: + try: + policy = policy.increment() + except MaxRetryError: + break + attempts += 1 + assert attempts == count + + def test_stop_after_attempts_count_one_disables_retries(self): + # With a count of 1, the first failure must raise MaxRetryError + # immediately — no second wire attempt is made. + policy = self._make_retry_policy(stop_after_attempts_count=1) + policy._command_type = None + policy._retry_start_time = None + assert policy.total == 0 + with pytest.raises(MaxRetryError): + policy.increment() + def test_respect_server_retry_after__retries_with_retry_after(self): """429 + Retry-After header → should retry""" policy = self._make_retry_policy(respect_server_retry_after_header=True)