Skip to content

Commit 2338e56

Browse files
committed
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 <atharvachavhan18@gmail.com>
1 parent 72ac235 commit 2338e56

3 files changed

Lines changed: 57 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Unreleased
44
- 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.
55
- 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.
6+
- 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.
67

78
# 4.10.0 (2026-11-24)
89
- Upgrade Databricks SQL Kernel to 1.5.0, with lower overhead for buffered fetches and closing drained cursors, and Arrow and Quinn security updates.

‎src/databricks/sql/auth/retry.py‎

100755100644
Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ class DatabricksRetryPolicy(Retry):
6565
Float of seconds for the maximum delay between retries.
6666
6767
:param stop_after_attempts_count:
68-
Integer maximum number of attempts that will be retried. This is an alias for urllib3's
69-
`total`.
68+
Integer maximum number of attempts in total (the initial request plus
69+
retries). urllib3's `total` counts only retries after the first
70+
request, so it is set to one less than this value.
7071
7172
:param stop_after_attempts_duration:
7273
Float of maximum number of seconds within which a request may be retried starting from
@@ -114,14 +115,18 @@ def __init__(
114115
_history: Optional[Tuple[RequestHistory, ...]] = urllib3_kwargs.get("history")
115116

116117
if not _history:
117-
# no attempts were made so we can retry the current command as many times as specified
118-
# by the user
119-
_attempts_remaining = self.stop_after_attempts_count
118+
# No attempts have been made yet. urllib3's `total` counts retries
119+
# *after* the first request, while `stop_after_attempts_count` is
120+
# the total number of attempts (first request + retries), so the
121+
# initial `total` is one less than the configured count. With a
122+
# count of 1, `total` is 0 and urllib3 raises MaxRetryError after
123+
# the single attempt instead of retrying once more.
124+
_attempts_remaining = max(0, self.stop_after_attempts_count - 1)
120125
else:
121126
# at least one of our attempts has been consumed, and urllib3 will have set a total
122-
# `total` is a counter that begins equal to self.stop_after_attempts_count and is
123-
# decremented after each unsuccessful request. When `total` is zero, urllib3 raises a
124-
# MaxRetryError
127+
# `total` is a counter that begins equal to self.stop_after_attempts_count - 1 and is
128+
# decremented after each unsuccessful request. When `total` goes below zero, urllib3
129+
# raises a MaxRetryError
125130
_total: int = urllib3_kwargs.pop("total")
126131
_attempts_remaining = _total
127132

@@ -313,7 +318,12 @@ def get_backoff_time(self) -> float:
313318
:return:
314319
"""
315320

316-
current_attempt = self.stop_after_attempts_count - int(self.total or 0)
321+
# Index the backoff by the retries already performed (the length of
322+
# the retry history), not by the remaining `total`: `total` counts
323+
# retries after the first request, so deriving the attempt from it
324+
# would shift the backoff curve by one step now that the initial
325+
# `total` is one less than the configured attempt count.
326+
current_attempt = len(self.history or ())
317327
proposed_backoff = (2**current_attempt) * self.delay_min
318328

319329
library_version = version("urllib3")

‎tests/unit/test_retry.py‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ def test_sleep__no_retry_after(self, t_mock, retry_policy, error_history):
4949

5050
@patch("time.sleep")
5151
def test_sleep__no_retry_after_header__multiple_retries(self, t_mock, retry_policy):
52-
num_attempts = retry_policy.stop_after_attempts_count
52+
# With stop_after_attempts_count=3 the policy allows 3 attempts in
53+
# total, i.e. 2 (sleep, increment) pairs complete before the 3rd
54+
# increment exhausts it — see test_stop_after_attempts_count below.
55+
num_attempts = retry_policy.stop_after_attempts_count - 1
5356

5457
retry_policy._retry_start_time = time.time()
5558
retry_policy.command_type = CommandType.OTHER
@@ -77,8 +80,10 @@ def test_sleep__no_retry_after_header__multiple_retries(self, t_mock, retry_poli
7780

7881
@patch("time.sleep")
7982
def test_excessive_retry_attempts_error(self, t_mock, retry_policy):
80-
# Attempting more than stop_after_attempt_count
81-
num_attempts = retry_policy.stop_after_attempts_count + 1
83+
# Attempting stop_after_attempts_count times exhausts the policy:
84+
# that many attempts in total are allowed, and the next increment
85+
# raises MaxRetryError.
86+
num_attempts = retry_policy.stop_after_attempts_count
8287

8388
retry_policy._retry_start_time = time.time()
8489
retry_policy.command_type = CommandType.OTHER
@@ -89,6 +94,35 @@ def test_excessive_retry_attempts_error(self, t_mock, retry_policy):
8994
# Internally urllib3 calls the increment function generating a new instance for every retry
9095
retry_policy = retry_policy.increment()
9196

97+
@pytest.mark.parametrize("count", [1, 2, 3, 5, 30])
98+
def test_stop_after_attempts_count_is_total_attempts(self, count):
99+
# Regression test for #890 (ERRORRECOV-013): the knob counts total
100+
# attempts (first request + retries), not retries after the first
101+
# request. urllib3's `total` is one less than the configured count.
102+
policy = self._make_retry_policy(stop_after_attempts_count=count)
103+
policy._command_type = None
104+
policy._retry_start_time = None
105+
assert policy.total == count - 1
106+
107+
attempts = 1 # the initial request
108+
while True:
109+
try:
110+
policy = policy.increment()
111+
except MaxRetryError:
112+
break
113+
attempts += 1
114+
assert attempts == count
115+
116+
def test_stop_after_attempts_count_one_disables_retries(self):
117+
# With a count of 1, the first failure must raise MaxRetryError
118+
# immediately — no second wire attempt is made.
119+
policy = self._make_retry_policy(stop_after_attempts_count=1)
120+
policy._command_type = None
121+
policy._retry_start_time = None
122+
assert policy.total == 0
123+
with pytest.raises(MaxRetryError):
124+
policy.increment()
125+
92126
def test_respect_server_retry_after__retries_with_retry_after(self):
93127
"""429 + Retry-After header → should retry"""
94128
policy = self._make_retry_policy(respect_server_retry_after_header=True)

0 commit comments

Comments
 (0)