Skip to content

Studio: try every address a site resolves to when fetching a page - #10319

Merged
Lyxot merged 5 commits into
unslothai:mainfrom
NilayYadav:fix-web-fetch-address-fallback
Sep 9, 2026
Merged

Lyxot merged 5 commits into
unslothai:mainfrom
NilayYadav:fix-web-fetch-address-fallback

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

Web search and page fetch look up all the addresses for a site, then only ever try the first one. Browsers and ordinary Python code move on to the next address when the first one does not answer.

Most large sites return an IPv6 address first. If your network has IPv6 set up but not actually working, which happens with some VPNs and some providers, every fetch of those sites fails and keeps failing. The same thing happens when a site has several addresses and one of them is down.

Now it tries each address in turn before giving up, and runs the same safety checks on every one of them. Tested with real network calls. Before: "Connection refused" or "timed out". After: the page loads.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-09T11:43:56.289921Z e195035 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 9c36e02486

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

GitHub Actions reproduction, on the real network

No UI surface changes here, so this is the "real values" form: the probe drives the real
_fetch_page_text path against the real network. Nothing is monkeypatched — the only thing
arranged is the network condition, a configured-but-broken IPv6 route (dummy interface,
global source address, default route, ip6tables on TCP), which is what a VPN with dead
IPv6 looks like to getaddrinfo.

Two shapes of "unroutable" matter, and they are not equivalent:

  • reject — ICMP admin-prohibited, so connect() fails immediately
  • drop — the SYN is swallowed, so connect() fails only by running out its timeout

Run 33917516884 — six cells,
same workflow and probe throughout, only the implementation differs:

implementation first address outcome elapsed what the caller got
main rejects ❌ fails 1.06s Failed to fetch URL: <urlopen error [Errno 13] Permission denied>
main drops ❌ fails 20.07s Failed to fetch URL: <urlopen error timed out>
PR as first submitted rejects ✅ loads 2.16s 167 chars of the page
PR as first submitted drops ❌ fails 20.06s Failed to fetch URL: <urlopen error timed out>
PR at 9c36e02 rejects ✅ loads 2.12s 167 chars of the page
PR at 9c36e02 drops ✅ loads 10.09s 167 chars of the page

example.com resolved to four addresses on the runner, AAAA first, and the validator handed
them over in resolver order:

[0] AF_INET6 2606:4700:10::6814:179a     UNREACHABLE
[1] AF_INET6 2606:4700:10::ac42:93f3     UNREACHABLE
[2] AF_INET  104.20.23.154               REACHABLE
[3] AF_INET  172.66.147.243              REACHABLE

main:  pinned = '2606:4700:10::6814:179a'                (str, one address)
head:  pinned = ['2606:4700:10::6814:179a', '2606:4700:10::ac42:93f3',
                 '104.20.23.154', '172.66.147.243']      (list, resolver order)

The fourth row is why 9c36e02 exists. Walking the addresses is not enough on its own:
_fetch_page_text puts one wall-clock deadline on the whole fetch, and the walk handed the
first address all of it. A dropped SYN spends that budget, so every later address was opened
with the 0.001s floor and the fetch still failed — the walk reached the working address and
could not use it. That is the exact case in the PR description ("timed out"), so the first
version fixed only half of what it claimed.

9c36e02 divides the remaining budget by the number of addresses still to try, so a blackholed
address can spend its share and no more. The 10.09s in the last row is that arithmetic: 20/4 = 5s
on the first IPv6, 15/3 = 5s on the second, then the IPv4 connects instantly. A host that
resolves to a single address gets remaining / 1, i.e. exactly the budget it gets today —
verified separately: 29.999s granted for timeout=30.

Regression test: test_a_blackholed_first_address_leaves_the_working_one_a_usable_budget
fails on the previous head with assert 'Failed to fetch URL: timed out.' is None and passes at
9c36e02. tests/test_web_fetch_extraction.py is 1109 passed locally.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Studio before/after, in the chat UI

Two isolated installs, each stamped with its own SHA: BEFORE is the PR's merge base
e8a88546d, AFTER is the head 72555e2f7 (the Codex-approved 9c36e02 plus one
pre-commit.ci line-wrapping commit). Same frontend build on both sides — the diff is backend only —
so the only thing that can move the picture is what the backend makes of the address list.

The network condition is arranged for the launched Studio process alone and identically on both
sides: --studio-env PYTHONPATH=<dir> holding a sitecustomize.py that makes every connect() to
a global IPv6 address raise ENETUNREACH — what the kernel returns on a configured-but-dead
IPv6 default route, i.e. a VPN with broken IPv6. Resolution is untouched, so en.wikipedia.org
still resolves AAAA-first (2001:df2:e500:ed1a::1, then 103.102.166.224); loopback and IPv4 are
unaffected. The marker the sitecustomize prints is read back from each side's Studio log, and the
pid it names is the pid listening on that side's port, so the condition provably reached the
backend on both sides. The fetches themselves are real network calls.

A stand-in OpenAI-compatible provider on 127.0.0.1 emits the two web_search calls in a fixed
order and then quotes the tool results back, so the model's decision is not a variable and the
assistant bubble is the backend's own output:

  1. https://en.wikipedia.org/wiki/IPv6 — dual-stack, AAAA first, the subject
  2. https://httpbin.org/html — IPv4-only, the control, must load on both sides

PR 10319 Studio before/after

fact BEFORE e8a88546d AFTER 72555e2f7
backend_ipv6_dead_marker true true
urls_requested the two above, same order same
provider_completions / approvals_clicked 3 / 2 3 / 2
tool_group_label 2 tool calls 2 tool calls
dual_stack_url_failed true false
dual_stack_result_len 165 16037
dual_stack_result_head Failed to fetch URL: <urlopen error [Errno 51] Network is unreachable> # IPv6 From Wikipedia, the free encyclopedia …
control_result_len / control_has_page 3598 / true 3598 / true
page_shows_fetch_failure true false

Only the five predicted keys moved. The control returned the identical 3598-character excerpt on
both sides, which is what makes the wikipedia difference attributable to the address walk rather
than to the network.

The card on the left is the user-visible symptom on main today for anyone whose IPv6 is
configured but not working: every dual-stack site fails with that error while IPv4-only sites keep
working. The real-network reproduction of the same thing on GitHub Actions, including the
drop-the-SYN variant that 9c36e02 exists for, is in the comment above.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed main still pins only the first address in studio/backend/core/inference/tools.py, so a host whose first record is unroutable never gets retried, and the budget split in _fetch_hop_timeout covers the dropped-SYN case too. Will get this reviewed.

@Lyxot Lyxot self-assigned this Sep 9, 2026
Retrying `opener.open` once per address divided the caller's timeout between them, and `create_connection` leaves whatever timeout it was given on the socket it returns, so that division bounded time to first byte rather than just the connect. Measured against the merge base on a host whose first record is unroutable: main answered in 9.0s, the retry loop failed at 30.0s.

Walking inside `HTTPConnection._create_connection` instead keeps the fallback in the connect phase, so the caller's timeout still covers one whole response, and it reaches the Gemini image fetch through the same handler.

The walk spends one budget across two passes: a brief probe of every address, capped at 1s each and at a quarter of the timeout in total, so an address that answers neither way cannot hold up the reachable ones; then an equal share of what is left, so no address strands the ones behind it. Every address is dialled in both passes, a dial that overruns still leaves the rest an attempt, and with no timeout this collapses to the single uncapped pass `create_connection` performs. Only a direct request carries the addresses: a proxied one reaches the origin through the proxy.

Validated with a 24-mutation sweep over the walk, the handlers and the routing decision, each mutation caught by a named test, and with live fetches against hosts given unroutable addresses ahead of their real ones, asserting the walk actually dialled them: 30 blackholed addresses first still connects, in 7.7s over 31 dials.
@Lyxot

Lyxot commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

Pushed a commit that moves the fallback from the request layer down to the connect layer.

The problem with retrying opener.open per address is that the timeout it divides is also the response timeout: create_connection leaves whatever timeout it was given on the socket it returns, so dividing it bounds time to first byte, not just the connect. On a host whose first record is unroutable, main answered in 9.0s where the retry loop failed at 30.0s.

Walking inside HTTPConnection._create_connection keeps the fallback in the connect phase, so the caller's timeout still covers one whole response. It also fixes the Gemini image fetch for free, since that path goes through the same handler.

The walk spends one budget in two passes: a brief probe of every address, capped at 1s each and at a quarter of the timeout in total, so an address that answers neither way cannot hold up the reachable ones; then an equal share of what is left, so no address strands the ones behind it. Every address is dialled in both passes, a dial that overruns still leaves the rest an attempt, and with no timeout it collapses to the single uncapped pass create_connection does. Only a direct request carries the addresses -- a proxied one reaches the origin through the proxy.

Validation: a 24-mutation sweep over the walk, the handlers and the routing decision, each mutation caught by a named test; plus live fetches against real hosts given unroutable addresses ahead of their real ones, asserting the walk actually dialled them.

blackholed first address        t= 1.7s  OK   2 dials,  1 injected
three blackholed first          t= 3.6s  OK   4 dials,  3 injected
30 blackholed first             t= 7.7s  OK  31 dials, 30 injected
tight 5s timeout, blackholed    t= 1.1s  OK   2 dials,  1 injected
plain http, blackholed first    t= 1.5s  OK   2 dials,  1 injected

Two things worth knowing:

  • With N addresses the first one now gets roughly 0.75 * timeout / N instead of the whole timeout, so a first address slower than that fails where it used to succeed. Across ten real hosts the slowest fastest-connect measured was 22ms, against a 1s probe cap. Removing the trade entirely needs concurrent dialling.
  • A proxied request still has no fallback: the URL pins one address and the proxy is told to reach exactly that one. That is unchanged from main -- giving it a fallback means putting the hostname in the CONNECT, which today only happens under UNSLOTH_STUDIO_DISABLE_DNS_PINNING=1, and that is a separate decision about trusting the proxy to resolve.

@Lyxot

Lyxot commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: e195035e9e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. The bug is real -- pinning to one validated address silently removed the walk create_connection does for every other client, so a host whose first record is unroutable became unfetchable.

With the fallback moved to the connect layer, the caller's timeout still covers one whole response, and the Gemini image path gets the same behaviour through the same handler.

Verified on the head: full backend suite green, a 24-mutation sweep over the walk, the handlers and the routing decision with each mutation caught by a named test, and live fetches against hosts given unroutable addresses ahead of their real ones. Staging CI on the same commit came back clean apart from test_model_picker_contracts.py, which fails on main independently of this PR.

@Lyxot
Lyxot merged commit c12a37f into unslothai:main Sep 9, 2026
30 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants