BF: Add timeout to follow_redirect HEAD requests; retry on Timeout#1849
Merged
yarikoptic merged 1 commit intomasterfrom Apr 30, 2026
Merged
BF: Add timeout to follow_redirect HEAD requests; retry on Timeout#1849yarikoptic merged 1 commit intomasterfrom
yarikoptic merged 1 commit intomasterfrom
Conversation
`follow_redirect()` calls `requests.head(url, allow_redirects=True)` to resolve a redirect chain. No timeout was passed, so a stalled hop (e.g. a third-party shortener like bit.ly that accepts the TCP/TLS handshake but never sends a response) blocks the call indefinitely. This has been observed in `test_follow_redirect` on the Windows CI runner — pytest's fault-handler stack ends in `socket.recv_into() -> ssl.read()` inside `requests.sessions.resolve_redirects`. The retry wrapper around `requests.head()` only caught `requests.ConnectionError`, which does not include `requests.ReadTimeout` — so even adding a timeout would have surfaced as a hard failure rather than the existing transient-error retry path. Changes: - Add `REDIRECT_HEAD_TIMEOUT = 30` constant in `dandi/consts.py`. - Pass it as `timeout=` to the `requests.head()` call. - Catch `requests.Timeout` in addition to `requests.ConnectionError` inside the retry loop, so `ReadTimeout` and `ConnectTimeout` are now retried with the same exponential backoff. - Widen the `connection_error` annotation to `requests.RequestException | None` to accommodate both exception types. - Add `test_follow_redirect_retry_on_timeout` parametrized over `ReadTimeout` and `ConnectTimeout`, mirroring the existing `test_follow_redirect_retry_on_connection_error`. - Update mock signatures in the existing tests to accept the new `timeout=` kwarg. Co-Authored-By: Claude Code 2.1.121 / Claude Opus 4.7 (1M context) <noreply@anthropic.com>
| resp = requests.head(url, allow_redirects=True) | ||
| except requests.ConnectionError as e: | ||
| resp = requests.head( | ||
| url, allow_redirects=True, timeout=REDIRECT_HEAD_TIMEOUT |
Contributor
There was a problem hiding this comment.
Yes, I often use this myself when using the requests package - it is quite nice
CodyCBakerPhD
approved these changes
Apr 30, 2026
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1849 +/- ##
==========================================
+ Coverage 76.26% 76.29% +0.02%
==========================================
Files 87 87
Lines 12486 12505 +19
==========================================
+ Hits 9523 9541 +18
- Misses 2963 2964 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
🚀 PR was released in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original motivator from me: we had windows keep failing here and there with getting stuck on redirects in test_follow_redirect and potentially others. So it made sense to add explicit timeout and retry on it.
claude summary of agreed upon changes:
follow_redirect()callsrequests.head(url, allow_redirects=True)to resolve a redirect chain. No timeout was passed, so a stalled hop (e.g. a third-party shortener like bit.ly that accepts the TCP/TLS handshake but never sends a response) blocks the call indefinitely. This has been observed intest_follow_redirecton the Windows CI runner — pytest's fault-handler stack ends insocket.recv_into() -> ssl.read()insiderequests.sessions.resolve_redirects.The retry wrapper around
requests.head()only caughtrequests.ConnectionError, which does not includerequests.ReadTimeout— so even adding a timeout would have surfaced as a hard failure rather than the existing transient-error retry path.Changes:
REDIRECT_HEAD_TIMEOUT = 30constant indandi/consts.py.timeout=to therequests.head()call.requests.Timeoutin addition torequests.ConnectionErrorinside the retry loop, soReadTimeoutandConnectTimeoutare now retried with the same exponential backoff.connection_errorannotation torequests.RequestException | Noneto accommodate both exception types.test_follow_redirect_retry_on_timeoutparametrized overReadTimeoutandConnectTimeout, mirroring the existingtest_follow_redirect_retry_on_connection_error.timeout=kwarg.