Skip to content

Studio: give web search engines a connect timeout so a blocked network fails fast (#15143) - #15161

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
debojyoti-dev:fix/web-search-connect-timeout-15143
Oct 25, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
debojyoti-dev:fix/web-search-connect-timeout-15143

Conversation

@debojyoti-dev

@debojyoti-dev debojyoti-dev commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #15143

Before: on a network where the approved search engines are blocked (the report is from mainland China without a proxy), web_search hung for minutes and then said "Search failed: the search engines did not respond within 300s." Every engine request waited the whole 300s tool budget for a connection that never came. ddgs runs a tier's engines in batches and waits once per batch, so tier 1 alone went past the budget, and tier 2 and the Wikipedia fallback were skipped because no time was left.

After: an engine that can't be reached gives up after 10s, so tier 2 and the Wikipedia fallback still get their turn and the search finishes in about a minute. The error now says the engines "could not be reached within 10s, so they may be blocked on this network", and tells the model the user can start Studio with HTTPS_PROXY set, or read a known page with {"url": "<URL>"}. That second option is the workaround the reporter found.

A slow engine that does connect still has the whole budget to answer. test_primary_late_success_is_not_replaced relies on that, and it still passes. Only the connect step is capped.

How:

  • _install_ddgs_connect_timeout() runs next to _install_ddgs_http1_retry() in _web_search and _image_search, and only does its work once.
  • primp: ddgs's http_client module now reaches primp through _PrimpWithConnectTimeout. That wrapper passes through every attribute and adds connect_timeout to each Client(...). primp 0.15, which the ddgs 9.8.0 / Python 3.9 pin installs, rejects the keyword with TypeError, so the client is built without it, exactly as before.
  • httpx: HttpClient2 (the DuckDuckGo engine) keeps its read, write and pool timeouts and gets connect=10.
  • The connect timeout is min(budget, 10). When the budget is already shorter than 10s, the connect step never gets more time than the request.
  • _search_failure_message tells a connect timeout apart from a read timeout by the client's wording ("connection timed out" from primp, ConnectTimeout from httpx). A read timeout still reports "did not respond within {budget}s".

Testing (Windows 11, Python 3.12, ddgs 9.14.4 / primp 2.0.1):

  • I reproduced the report by sending every ddgs engine request to the unroutable address 10.255.255.1, which looks like an IP block or DNS poisoning, and called _web_search with the default 300s budget. Unmodified main took 112s and returned a raw os error 10060 message. Windows gives up on its own after about 21s per connect, while Linux keeps retrying for about 127s, which is why the reporter saw the full 300s. With this change it took 66s and returned the new message. With Wikipedia reachable it returned the Wikipedia results in 61s. On an open network a normal search came back in 4.6s, as before.
  • Checked against the real libraries: primp 2.0.1 with connect_timeout=3 fails at 3.0s against a dead address and through a proxy that never answers. A server that is connected but slow still completes after 7s.
  • New studio/backend/tests/test_web_search_connect_timeout.py (10 tests). They cover the primp and httpx clients getting the capped connect timeout while keeping the read budget (300s, 3s and no limit), the install running only once, a primp without connect_timeout still building its client, and both failure messages. 9 of them fail without the source change. The tenth pins the unchanged read-timeout message.
  • I ran these one file per process on this branch and on unmodified main: test_web_access_policy, test_web_fetch_bot_check, test_web_fetch_extraction, test_web_search_fallback, test_web_search_http1_retry, test_web_search_tiers, test_web_search_yahoo_layout, test_search_images, test_secure_tools_execute, test_anthropic_web_search_errors, test_anthropic_messages, test_anthropic_web_fetch, test_research_mcp_sources, test_research_runs_storage, test_studio_tool_loop, test_no_progress_tool_results and tests/kaggle/test_studio_web_search.py. Results are identical. The same 8 tests fail on main too, and they come from this environment: the 8-thread fallback test timing out, the pinned-opener test, and 6 MCP discovery tests.
  • I also ran the search tests against the old pin, ddgs 9.8.0 with primp 0.15.0. The new tests pass, and the 3 primp ones skip because that primp has no connect_timeout. The tiers, HTTP/1.1 retry and policy tests pass, and a live search works.

I don't have a machine in mainland China, so the reproduction above simulates the blocked network rather than using the reporter's.

Follow-up: keep a slower engine's answer when another engine fails fast

ddgs waits on each batch of engines with FIRST_EXCEPTION, and after its last batch it never reads the engines that are still running (its executor joins them, then the results are discarded). With the 10s connect cap, a blocked engine failing at 10s could therefore throw away a reachable engine that answered at 12s, which is a realistic shape on a network like the reporter's, where the few reachable engines are slow.

  • ddgs's batch wait is wrapped but called unchanged, so its scheduling is exactly as before (a failed slot is still refilled at once). The wrapper only records which engines were still pending after the last batch.
  • _ddgs_text calls client.text and, if the sweep came back short or raised, adds the answers of those pending engines (already joined by ddgs's executor, so no extra wait), deduplicated by URL.
  • Tests: test_an_engine_failing_fast_does_not_drop_a_slower_engines_answer goes through _web_search and the real ddgs scheduler and fails without the change; test_ddgs_still_moves_on_at_the_first_failure pins the unchanged scheduling.

Re-checked on Linux (ddgs 9.14.4 / primp 2.0.1): every engine sent to an unroutable address took 288.8s on main and 57.6s here, with the new message; with Wikipedia reachable, 222.9s on main and 60.6s here; an open-network search is unchanged. A local slow engine (12s) beside a blocked one is now returned instead of "No results found". HTTPS_PROXY and https_proxy are both honoured by every engine. Python 3.9 with ddgs 9.8.0 / primp 0.15 still searches normally.

…k fails fast (unslothai#15143)

On a network where the approved engines are blocked, each engine request
waited the whole tool budget (300s) to connect, ddgs waits once per batch of
engines, so tier 1 alone ran past the budget, tier 2 and the Wikipedia
fallback never ran, and the chat hung for minutes before "did not respond
within 300s".

ddgs's primp and httpx clients now get a 10s connect timeout (or less when
the budget left is shorter). The read keeps the caller's timeout, so an
engine that connected can still answer late. A connect timeout is reported
as "could not be reached", with a hint to start Studio with HTTPS_PROXY or
read a known page with {"url": ...}. primp 0.15 (the ddgs 9.8.0 pin) has no
connect_timeout and builds its client as before.
danielhanchen added a commit that referenced this pull request Oct 24, 2026
…15165)

## Summary

`tests/test_studio_root_resilience.py` fails in the `Repo tests (CPU,
rest)` shard (#15159, #15161) but passes on its own:

```
studio/backend/loggers/handlers.py:22: in <module>
    from utils.native_path_leases import redact_native_paths
E   ModuleNotFoundError: No module named 'utils.native_path_leases'
```

`storage_roots.py` imports the backend's `loggers` and `utils` by bare
name. A dozen test modules in `tests/` (the `test_grpo_*` family,
`test_nvfp4_loader.py` and others) put `tests/` at `sys.path[0]` when
they are imported. After that, any bare `import utils` in the worker
resolves to `tests/utils`. Logging `sys.modules["utils"]` after every
test of a local run of the shard, on the same command line as CI (`-n 4
--dist loadgroup`), showed it switching to `tests/utils/__init__.py`
partway through on two workers. Every later `from
utils.native_path_leases import ...` then fails. The local run
reproduced all three CI failures. The product code is fine. The test
inherited whatever `utils` the worker had cached.

## Change

- `_load()` now takes `monkeypatch`. Before loading `storage_roots.py`
it puts `studio/backend` first on `sys.path` and drops any cached
`utils*` / `loggers*` module that does not live under `studio/backend`.
`monkeypatch` puts everything back after the test, so the rest of the
worker sees what it had before.
- `test_a_foreign_utils_left_by_another_test_does_not_answer`: registers
a stray `utils` package the way the shard does and checks that
`storage_roots.py` still loads against the backend's own `utils`.

## Verification

- With this change: `8 passed` alone, and `17 passed` with
`test_cli_export_unpacking.py` run between two copies of the file.
- Sabotage: skipping the new isolation step makes the new test fail with
CI's exact error (`ModuleNotFoundError: No module named
'utils.native_path_leases'`).
- The whole rest shard, run locally the way CI runs it, reproduced the
three failures before the change.
@danielhanchen danielhanchen self-assigned this Oct 25, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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 ⚠️ Failed 2026-10-25T12:38:54.605162Z
We couldn’t complete this review. Comment @codex review to try again.
6fcafd3 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: Something went wrong. Try again later by commenting “@codex review”.

An unknown error occurred
ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

An unknown error occurred
ℹ️ 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".

@danielhanchen
danielhanchen merged commit d53a090 into unslothai:main Oct 25, 2026
34 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 在中国大陆AI无法使用联网搜索功能

2 participants