Skip to content

Commit 37a1a15

Browse files
committed
Do not retry failing requests with status code 401
- Raises NonRecoverableNetworkError when request results in 401 status code Signed-off-by: Tor Hødnebø <[email protected]>
1 parent f53aa37 commit 37a1a15

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release History
22

3+
# x.x.x (TBD)
4+
5+
- Don't retry requests that fail with code 401
6+
37
# 3.2.0 (2024-06-06)
48

59
- Update proxy authentication (databricks/databricks-sql-python#354 by @amir-haroun)

src/databricks/sql/auth/retry.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ def should_retry(self, method: str, status_code: int) -> Tuple[bool, str]:
325325
default, this means ExecuteStatement is only retried for codes 429 and 503.
326326
This limit prevents automatically retrying non-idempotent commands that could
327327
be destructive.
328-
5. The request received a 403 response, because this can never succeed.
328+
5. The request received a 401 response, because this can never succeed.
329+
6. The request received a 403 response, because this can never succeed.
329330
330331
331332
Q: What about OSErrors and Redirects?
@@ -339,6 +340,11 @@ def should_retry(self, method: str, status_code: int) -> Tuple[bool, str]:
339340
if status_code == 200:
340341
return False, "200 codes are not retried"
341342

343+
if status_code == 401:
344+
raise NonRecoverableNetworkError(
345+
"Received 401 - UNAUTHORIZED. Confirm your authentication credentials."
346+
)
347+
342348
if status_code == 403:
343349
raise NonRecoverableNetworkError(
344350
"Received 403 - FORBIDDEN. Confirm your authentication credentials."

tests/e2e/common/retry_test_mixins.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,17 @@ def test_403_not_retried(self):
420420
with pytest.raises(RequestError) as cm:
421421
with self.connection(extra_params=self._retry_policy) as conn:
422422
pass
423-
assert isinstance(cm.value.args[1], NonRecoverableNetworkError)
423+
assert isinstance(cm.value.args[1], NonRecoverableNetworkError)
424+
425+
def test_401_not_retried(self):
426+
"""GIVEN the server returns a code 401
427+
WHEN the connector receives this response
428+
THEN nothing is retried and an exception is raised
429+
"""
430+
431+
# Code 401 is an Unauthorized error
432+
with mocked_server_response(status=401):
433+
with pytest.raises(RequestError) as cm:
434+
with self.connection(extra_params=self._retry_policy):
435+
pass
436+
assert isinstance(cm.value.args[1], NonRecoverableNetworkError)

0 commit comments

Comments
 (0)