Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: retry web search over HTTP/1.1 when the connection is reset (#13032)
Daniel Han96f333eaf3fe
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.pyindex f1469b6edce..f64e36ddd54 100644--- a/studio/backend/core/inference/tools.py+++ b/studio/backend/core/inference/tools.py@@ -115,6 +115,15 @@ EMPTY_SEARCH_RESULTS = ()# ddgs signals an empty sweep by raising rather than returning []._DDGS_EMPTY_SWEEP = "No results found"+# Not "connection error": DNS failures and refused connections are not resets (#12638).+_DDGS_RESET_MARKERS = (+ "connection reset",+ "h2 connection driver error",+ "server disconnected",+ "broken pipe",+ "forcibly closed", # Windows WSAECONNRESET (10054)+)+_DDGS_HTTP1_RETRY_LOCK = threading.Lock()# Tier 2 is only asked when tier 1 found nothing. Naming is the only way ddgs reaches an engine, so# an engine in neither tier (yandex, bing, the mullvad_* mirrors) is never contacted.@@ -17000,6 +17009,122 @@ def _install_yahoo_layout_parser(text_engines) -> None:text_engines["yahoo"] = _Yahoo+def _is_connection_reset(exc) -> bool:+ return any(marker in f"{type(exc).__name__}: {exc}".lower() for marker in _DDGS_RESET_MARKERS)+++def _ddgs_http1_replay(args, kwargs, config):+ import httpx++ verify = config["verify"]+ if isinstance(verify, str):+ verify = ssl.create_default_context(cafile = verify)+ with httpx.Client(+ headers = config["headers"],+ cookies = config["cookies"],+ proxy = config["proxy"],+ timeout = config["timeout"],+ verify = verify,+ follow_redirects = config["follow_redirects"],+ http1 = True,+ http2 = False,+ ) as client:+ resp = client.request(*args, **kwargs)+ resp.read()+ return resp+++def _install_ddgs_http1_retry() -> None:+ """Replay a ddgs request once over plain HTTP/1.1 after a connection reset (#12638): primp has+ no HTTP/1.1-only mode. Successful requests are untouched; wraps each class once; never raises."""+ try:+ import inspect++ from ddgs import http_client+ from ddgs.exceptions import DDGSException++ def _wrapper(response_cls):+ # ddgs 9.14's primp Response wraps the raw response; 9.8.0's and HttpClient2's take fields.+ if "status_code" not in inspect.signature(response_cls).parameters:+ return response_cls+ return lambda resp: response_cls(+ status_code = resp.status_code, content = resp.content, text = resp.text+ )++ targets = [(http_client.HttpClient, _wrapper(http_client.Response), True)]+ try:+ from ddgs import http_client2+ except ImportError:+ http_client2 = None+ if http_client2 is not None and hasattr(http_client2, "HttpClient2"):+ # HttpClient2 does not follow redirects; primp does.+ targets.append((http_client2.HttpClient2, _wrapper(http_client2.Response), False))++ with _DDGS_HTTP1_RETRY_LOCK:+ for cls, wrap, follow_redirects in targets:+ if cls.__dict__.get("_unsloth_http1_retry"):+ continue+ _wrap_ddgs_client(+ cls, wrap, follow_redirects, inspect.signature(cls.__init__), DDGSException+ )+ except Exception: # noqa: BLE001 - the retry is a hardening layer, never a reason to fail a search+ logger.debug("ddgs HTTP/1.1 retry not installed", exc_info = True)+++def _wrap_ddgs_client(cls, wrap, follow_redirects, signature, ddgs_exception) -> None:+ orig_init, orig_request = cls.__init__, cls.request++ @functools.wraps(orig_init)+ def __init__(self, *args, **kwargs):+ orig_init(self, *args, **kwargs)+ try:+ bound = signature.bind(self, *args, **kwargs)+ bound.apply_defaults()+ config = dict(bound.arguments)+ config.pop("self", None) # no client -> config -> client cycle+ self._unsloth_http1_config = config+ except TypeError:+ pass++ @functools.wraps(orig_request)+ def request(self, *args, **kwargs):+ start = time.monotonic()+ try:+ return orig_request(self, *args, **kwargs)+ except Exception as exc:+ config = getattr(self, "_unsloth_http1_config", None)+ if config is None or not _is_connection_reset(exc):+ raise+ timeout = config.get("timeout")+ if timeout:+ timeout -= time.monotonic() - start+ if timeout <= 0:+ raise+ client = getattr(self, "client", None)+ # httpx's jar only: primp 0.15 (ddgs 9.8.0) get_cookies aborts the process on a miss.+ cookies = getattr(client, "cookies", None)+ # httpx may lack primp's zstd decoder, so let it pick accept-encoding.+ session = getattr(client, "headers", None) or {}+ headers = {k: v for k, v in dict(session).items() if k.lower() != "accept-encoding"}+ replay = {+ "headers": headers,+ "cookies": cookies,+ "proxy": config.get("proxy"),+ "timeout": timeout,+ "verify": config.get("verify", True),+ "follow_redirects": follow_redirects,+ }+ try:+ return wrap(_ddgs_http1_replay(args, kwargs, replay))+ except Exception as retry_exc:+ raise ddgs_exception(+ f"{exc}; HTTP/1.1 retry failed: {type(retry_exc).__name__}: {retry_exc}"+ ) from retry_exc++ cls.__init__, cls.request = __init__, request+ cls._unsloth_http1_retry = True++def _image_search_or_none(subjects: list, timeout, cancel_event, website_policy) -> "str | None":"""``_image_search`` that reports a failure as None instead of raising. Every caller sits inside``_web_search``'s own ``except``, which would turn a raise into Search failed: ... and throw@@ -17145,6 +17270,7 @@ def _web_search(text_engines = ENGINES.get("text") or {}_install_yahoo_layout_parser(text_engines)+ _install_ddgs_http1_retry()engine_tiers = _resolve_engine_tiers(text_engines)if not engine_tiers:raise RuntimeError("no approved search engine is available.")@@ -17324,6 +17450,7 @@ def _image_search(from .web_access_policy import scope_search_queryexcept Exception as e:return _search_failure_message(e, timeout)+ _install_ddgs_http1_retry()if not callable(getattr(DDGS, "images", None)):return "Image search is unavailable in this install."diff --git a/studio/backend/tests/test_web_search_http1_retry.py b/studio/backend/tests/test_web_search_http1_retry.pynew file mode 100644index 00000000000..784377b801f--- /dev/null+++ b/studio/backend/tests/test_web_search_http1_retry.py@@ -0,0 +1,259 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""ddgs requests replay once over HTTP/1.1 after a connection reset (unslothai/unsloth#12638)."""++from __future__ import annotations++import sys+import types++import pytest++from core.inference import tools+++class FakeDDGSException(Exception):+ pass+++class FieldsResponse:+ """ddgs HttpClient2's Response, and ddgs 9.8.0's primp one."""++ def __init__(self, status_code, content, text):+ self.status_code, self.content, self.text = status_code, content, text+++class WrappingResponse:+ """ddgs 9.14's primp Response, which wraps the raw response."""++ def __init__(self, resp):+ self.status_code, self.content, self.text = resp.status_code, resp.content, resp.text+++class FakeReplayResponse:+ status_code = 200+ content = b"ok"+ text = "ok"+++RESET = FakeDDGSException(+ "RequestError: RequestError('error sending request for url (https://x/) > connection reset')"+)+++def _install_fake_ddgs(monkeypatch, primp_response):+ """Fake ddgs modules whose client ``request`` raises ``state["error"]`` when set."""+ state = {"error": None, "replay_error": None, "calls": 0}++ class Session:+ headers = {"user-agent": "UA", "accept-encoding": "gzip, br, zstd"}++ def get_cookies(self, url):+ raise AssertionError("primp 0.15 aborts the process when no cookie matches")++ class Session2:+ headers = {"user-agent": "DDG"}+ cookies = {"kl": "us-en"}++ class HttpClient:+ def __init__(+ self,+ proxy = None,+ timeout = 10,+ *,+ verify = True,+ ):+ self.client = Session()++ def request(self, *args, **kwargs):+ state["calls"] += 1+ if state["error"] is not None:+ raise state["error"]+ return "primary"++ class HttpClient2:+ def __init__(+ self,+ headers = None,+ proxy = None,+ timeout = 10,+ *,+ verify = True,+ ):+ self.client = Session2()++ request = HttpClient.request++ pkg = types.ModuleType("ddgs")+ http_client = types.ModuleType("ddgs.http_client")+ http_client.HttpClient, http_client.Response = HttpClient, primp_response+ http_client2 = types.ModuleType("ddgs.http_client2")+ http_client2.HttpClient2, http_client2.Response = HttpClient2, FieldsResponse+ exceptions = types.ModuleType("ddgs.exceptions")+ exceptions.DDGSException = FakeDDGSException++ class DDGS:+ def __init__(self, timeout = None):+ self.timeout = timeout++ def text(+ self,+ query,+ max_results = 5,+ backend = "auto",+ ):+ resp = HttpClient(timeout = self.timeout).request(+ "GET", "https://html.duckduckgo.com/html/"+ )+ return [{"title": "Weather", "href": "https://example.com/weather", "body": resp.text}]++ engines = types.ModuleType("ddgs.engines")+ engines.ENGINES = {"text": {"duckduckgo": object}}+ pkg.DDGS = DDGS+ pkg.http_client, pkg.http_client2, pkg.exceptions = http_client, http_client2, exceptions+ pkg.engines = engines+ for name, module in {+ "ddgs": pkg,+ "ddgs.http_client": http_client,+ "ddgs.http_client2": http_client2,+ "ddgs.exceptions": exceptions,+ "ddgs.engines": engines,+ }.items():+ monkeypatch.setitem(sys.modules, name, module)++ replays = []++ def fake_replay(args, kwargs, config):+ replays.append((args, kwargs, config))+ if state["replay_error"] is not None:+ raise state["replay_error"]+ return FakeReplayResponse()++ monkeypatch.setattr(tools, "_ddgs_http1_replay", fake_replay, raising = False)+ return types.SimpleNamespace(+ state = state, replays = replays, HttpClient = HttpClient, HttpClient2 = HttpClient2+ )++[email protected](params = [WrappingResponse, FieldsResponse], ids = ["ddgs-9.14", "ddgs-9.8"])+def fake_ddgs(monkeypatch, request):+ fake = _install_fake_ddgs(monkeypatch, request.param)+ tools._install_ddgs_http1_retry()+ return fake+++def test_web_search_returns_results_after_a_connection_reset(monkeypatch):+ fake = _install_fake_ddgs(monkeypatch, WrappingResponse)+ fake.state["error"] = RESET++ def no_wikipedia(*args, **kwargs):+ raise RuntimeError("offline")++ monkeypatch.setattr(tools, "_wikipedia_search", no_wikipedia)+ out = tools._web_search("weather", timeout = 5)+ assert "Search failed" not in out+ assert "https://example.com/weather" in out+ assert len(fake.replays) == 1+++def test_reset_detection():+ assert tools._is_connection_reset(RESET)+ assert tools._is_connection_reset(Exception("h2 connection driver error: connection reset"))+ assert tools._is_connection_reset(Exception("RemoteProtocolError('Server disconnected')"))+ assert tools._is_connection_reset(+ FakeDDGSException(+ "RequestError: error sending request > stream closed because of a broken pipe"+ )+ )+ assert tools._is_connection_reset(+ FakeDDGSException(+ "ReadError: [WinError 10054] An existing connection was forcibly closed by the remote host"+ )+ )+ for message in (+ "ConnectError: [Errno -2] Name or service not known",+ "ConnectError: [Errno 111] Connection refused",+ "No results found.",+ "Request timed out",+ ):+ assert not tools._is_connection_reset(FakeDDGSException(message))+++def test_install_is_idempotent(fake_ddgs):+ request, init = fake_ddgs.HttpClient.request, fake_ddgs.HttpClient2.__init__+ tools._install_ddgs_http1_retry()+ tools._install_ddgs_http1_retry()+ assert fake_ddgs.HttpClient.request is request+ assert fake_ddgs.HttpClient2.__init__ is init+ fake_ddgs.state["error"] = RESET+ fake_ddgs.HttpClient().request("GET", "https://x/")+ assert len(fake_ddgs.replays) == 1+++def test_install_never_raises_without_ddgs(monkeypatch):+ monkeypatch.setitem(sys.modules, "ddgs", None)+ tools._install_ddgs_http1_retry()++[email protected]("cls_name", ["HttpClient", "HttpClient2"])+def test_healthy_request_is_untouched(fake_ddgs, cls_name):+ assert getattr(fake_ddgs, cls_name)().request("GET", "https://x/") == "primary"+ assert fake_ddgs.replays == []++[email protected]("cls_name", ["HttpClient", "HttpClient2"])+def test_other_failures_are_not_replayed(fake_ddgs, cls_name):+ fake_ddgs.state["error"] = FakeDDGSException("ConnectError: Name or service not known")+ with pytest.raises(FakeDDGSException, match = "Name or service"):+ getattr(fake_ddgs, cls_name)().request("GET", "https://x/")+ assert fake_ddgs.replays == []+++def test_primp_reset_replays_once_with_constructor_settings(fake_ddgs):+ fake_ddgs.state["error"] = RESET+ client = fake_ddgs.HttpClient("socks5://p:1", 4, verify = False)+ assert "self" not in client._unsloth_http1_config+ resp = client.request("POST", "https://x/", data = {"q": "weather"})+ assert (resp.status_code, resp.text) == (200, "ok")+ assert fake_ddgs.state["calls"] == 1+ [(args, kwargs, config)] = fake_ddgs.replays+ assert args == ("POST", "https://x/") and kwargs == {"data": {"q": "weather"}}+ assert 0 < config.pop("timeout") <= 4+ assert config == {+ "headers": {"user-agent": "UA"},+ "cookies": None,+ "proxy": "socks5://p:1",+ "verify": False,+ "follow_redirects": True,+ }+++def test_duckduckgo_reset_replays_without_following_redirects(fake_ddgs):+ fake_ddgs.state["error"] = FakeDDGSException("WriteError: [Errno 104] Connection reset by peer")+ client = fake_ddgs.HttpClient2(headers = {"User-Agent": "DDG"}, timeout = 6)+ resp = client.request(method = "POST", url = "https://html.duckduckgo.com/html/")+ assert (resp.status_code, resp.text) == (200, "ok")+ [(_, kwargs, config)] = fake_ddgs.replays+ assert kwargs == {"method": "POST", "url": "https://html.duckduckgo.com/html/"}+ assert 0 < config["timeout"] <= 6 and config["follow_redirects"] is False+ assert config["cookies"] == {"kl": "us-en"}+++def test_reset_after_the_timeout_is_spent_is_not_replayed(fake_ddgs, monkeypatch):+ fake_ddgs.state["error"] = RESET+ clock = iter([100.0, 104.5])+ monkeypatch.setattr(tools.time, "monotonic", lambda: next(clock))+ with pytest.raises(FakeDDGSException, match = "connection reset"):+ fake_ddgs.HttpClient(timeout = 4).request("GET", "https://x/")+ assert fake_ddgs.replays == []+++def test_failed_replay_raises_ddgs_exception_with_both_errors(fake_ddgs):+ fake_ddgs.state["error"] = RESET+ fake_ddgs.state["replay_error"] = OSError("Connection reset by peer")+ with pytest.raises(FakeDDGSException) as info:+ fake_ddgs.HttpClient().request("GET", "https://x/")+ assert "connection reset" in str(info.value)+ assert "HTTP/1.1 retry failed" in str(info.value)+ assert len(fake_ddgs.replays) == 1