Repository navigation
Studio: give web search engines a connect timeout so a blocked network fails fast (#15143) - #15161
Conversation
…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.
…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.
|
@codex review |
1 similar comment
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…ls its connect cap [skip ci]
… instead [skip ci]
Fixes #15143
Before: on a network where the approved search engines are blocked (the report is from mainland China without a proxy),
web_searchhung 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_PROXYset, 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_replacedrelies 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_searchand_image_search, and only does its work once.http_clientmodule now reaches primp through_PrimpWithConnectTimeout. That wrapper passes through every attribute and addsconnect_timeoutto eachClient(...). primp 0.15, which the ddgs 9.8.0 / Python 3.9 pin installs, rejects the keyword withTypeError, so the client is built without it, exactly as before.HttpClient2(the DuckDuckGo engine) keeps its read, write and pool timeouts and getsconnect=10.min(budget, 10). When the budget is already shorter than 10s, the connect step never gets more time than the request._search_failure_messagetells a connect timeout apart from a read timeout by the client's wording ("connection timed out" from primp,ConnectTimeoutfrom 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):
_web_searchwith the default 300s budget. Unmodified main took 112s and returned a rawos error 10060message. 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.connect_timeout=3fails 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.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 withoutconnect_timeoutstill building its client, and both failure messages. 9 of them fail without the source change. The tenth pins the unchanged read-timeout message.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_resultsandtests/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.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.waitis 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_textcallsclient.textand, 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.test_an_engine_failing_fast_does_not_drop_a_slower_engines_answergoes through_web_searchand the real ddgs scheduler and fails without the change;test_ddgs_still_moves_on_at_the_first_failurepins 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_PROXYandhttps_proxyare both honoured by every engine. Python 3.9 with ddgs 9.8.0 / primp 0.15 still searches normally.