Skip to content

Commit 5517ea2

Browse files
hive-botclaude
andcommitted
fix: never retry proxy failures
Every requests ProxyError, including a failure to connect to the proxy, raises ConnectionException with should_retry False. create_developer_key() no longer retries a DNS failure that the proxy reports. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 00ca727 commit 5517ea2

5 files changed

Lines changed: 30 additions & 38 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
230230
- HTTP 3xx responses that are not followed as redirects (e.g. 300, 304, or a
231231
redirect without `Location`) now raise `DeepLException` with
232232
`http_status_code`, instead of being parsed as a successful response.
233-
- TLS and certificate failures, and errors reported by a proxy, are no longer
234-
retried and raise `ConnectionException` with `should_retry` `False`. A
235-
failure to connect to the proxy is still retried.
233+
- TLS and certificate failures, and proxy failures (including a failure to
234+
connect to the proxy), are no longer retried and raise `ConnectionException`
235+
with `should_retry` `False`.
236236
- Query string values encode a space as `%20` instead of `+`.
237237
- HTTP 401 responses now raise `AuthorizationException`, like 403. Before they
238238
raised `DeepLException` with "Unexpected status code: 401".

‎README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,9 +1574,10 @@ option, see the [documentation for requests][requests-verify-ssl-docs].
15741574
This SDK will automatically retry failed HTTP requests if the failures could
15751575
be transient: HTTP 429 and 5xx status codes, and network failures such as
15761576
refused or reset connections, DNS failures and timeouts. TLS and certificate
1577-
failures, and errors reported by a proxy, are not retried. By default a
1578-
request is retried up to 5 times, and each attempt has a timeout of at least
1579-
10 seconds. You can change both per client with the `max_retries` and `timeout_s` options when
1577+
failures, and proxy failures (including a failure to connect to the proxy),
1578+
are not retried. By default a request is retried up to 5 times, and each
1579+
attempt has a timeout of at least 10 seconds. You can change both per client
1580+
with the `max_retries` and `timeout_s` options when
15801581
constructing a `deepl.DeepLClient`:
15811582

15821583
```python

‎deepl/http_client.py‎

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,8 @@ def _internal_request(
238238
message = f"TLS connection failed: {e}"
239239
raise ConnectionException(message, should_retry=False) from e
240240
except requests.exceptions.ProxyError as e:
241-
# Only a failure to reach the proxy is transient; an error the
242-
# proxy reports (e.g. 407, refused tunnel) is not.
243241
message = f"Proxy connection failed: {e}"
244-
raise ConnectionException(
245-
message, should_retry=_is_connect_failure(e)
246-
) from e
242+
raise ConnectionException(message, should_retry=False) from e
247243
except requests.exceptions.ConnectionError as e:
248244
message = f"Connection failed: {e}"
249245
raise ConnectionException(message, should_retry=True) from e
@@ -270,7 +266,9 @@ def _should_retry(
270266

271267
if response is None:
272268
if only_unprocessed:
273-
return _is_name_resolution_error(exception)
269+
return exception.should_retry and _is_name_resolution_error(
270+
exception
271+
)
274272
return exception.should_retry
275273

276274
status_code, _ = response
@@ -325,32 +323,14 @@ def _prepare_request(
325323
def _is_name_resolution_error(exception: BaseException) -> bool:
326324
"""Returns True if the exception was caused by a failure to resolve the
327325
host name, in which case the request was never sent."""
328-
return _is_caused_by(exception, _NAME_RESOLUTION_ERRORS)
329-
330-
331-
_CONNECT_FAILURES = (
332-
socket.gaierror,
333-
ConnectionRefusedError,
334-
requests.packages.urllib3.exceptions.NewConnectionError, # type: ignore
335-
requests.packages.urllib3.exceptions.ConnectTimeoutError, # type: ignore
336-
)
337-
338-
339-
def _is_connect_failure(exception: BaseException) -> bool:
340-
"""Returns True if the exception was caused by a failure to open the
341-
connection: DNS failure, refused connection or connect timeout."""
342-
return _is_caused_by(exception, _CONNECT_FAILURES)
343-
344-
345-
def _is_caused_by(exception: BaseException, types: tuple) -> bool:
346326
pending: List[Optional[BaseException]] = [exception]
347327
seen = set()
348328
while pending:
349329
e = pending.pop()
350330
if e is None or id(e) in seen:
351331
continue
352332
seen.add(id(e))
353-
if isinstance(e, types):
333+
if isinstance(e, _NAME_RESOLUTION_ERRORS):
354334
return True
355335
pending.extend(
356336
[e.__cause__, e.__context__, getattr(e, "reason", None)]

‎tests/offline/test_admin_client.py‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def test_create_developer_key_is_not_retried_after_possible_processing(
429429
assert len(mocked.calls) == 1
430430

431431

432-
def _dns_failure():
432+
def _dns_failure(error_class=requests.exceptions.ConnectionError):
433433
gaierror = socket.gaierror(8, "not known")
434434
if hasattr(urllib3.exceptions, "NameResolutionError"):
435435
reason = urllib3.exceptions.NameResolutionError(
@@ -438,10 +438,20 @@ def _dns_failure():
438438
else:
439439
reason = urllib3.exceptions.NewConnectionError(None, "not known")
440440
reason.__context__ = gaierror
441-
return requests.exceptions.ConnectionError(
442-
urllib3.exceptions.MaxRetryError(None, "/", reason)
441+
return error_class(urllib3.exceptions.MaxRetryError(None, "/", reason))
442+
443+
444+
def test_create_developer_key_does_not_retry_dns_failure_of_proxy(mocked):
445+
mocked.post(
446+
url("v2/admin/developer-keys"),
447+
body=_dns_failure(requests.exceptions.ProxyError),
443448
)
444449

450+
with pytest.raises(deepl.ConnectionException):
451+
make_admin_client().create_developer_key()
452+
453+
assert len(mocked.calls) == 1
454+
445455

446456
@pytest.mark.parametrize(
447457
"failure", [{"status": 429}, {"body": _dns_failure()}]

‎tests/offline/test_http_client.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,17 @@ def test_does_not_retry_tls_or_proxy_failure(mocked, error):
187187
assert len(mocked.calls) == 1
188188

189189

190-
def test_retries_failure_to_connect_to_proxy(mocked):
190+
def test_does_not_retry_a_failure_to_connect_to_the_proxy(mocked):
191191
refused = urllib3.exceptions.NewConnectionError(None, "refused")
192192
mocked.get(
193193
url("v2/usage"),
194194
body=requests.exceptions.ProxyError(
195195
"Unable to connect to proxy", refused
196196
),
197197
)
198-
mocked.get(url("v2/usage"), json=USAGE_JSON)
199198

200-
make_translator().get_usage()
199+
with pytest.raises(deepl.ConnectionException) as exc_info:
200+
make_translator().get_usage()
201201

202-
assert len(mocked.calls) == 2
202+
assert not exc_info.value.should_retry
203+
assert len(mocked.calls) == 1

0 commit comments

Comments
 (0)