Skip to content

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.

Studio diffusion: one bounded Hub probe per process when the Hub is unreachable (#15039)

Daniel Han5b2bc06ffb85
diff --git a/studio/backend/core/inference/diffusion_prequant_cache.py b/studio/backend/core/inference/diffusion_prequant_cache.py
index 7fce4eea92a..637deb9e536 100644
--- a/studio/backend/core/inference/diffusion_prequant_cache.py
+++ b/studio/backend/core/inference/diffusion_prequant_cache.py
@@ -585,6 +585,37 @@ _REMOTE_TTL_SECONDS = 300.0
_REMOTE_TIMEOUT_SECONDS = 5.0
_REMOTE_MEMO: dict[tuple, tuple] = {}
_GONE = "gone"
+# Endpoint -> monotonic time its connection failed; later probes answer "unknown" for the TTL instead of timing out.
+_UNREACHABLE: dict[str, float] = {}
+
+
+def _connection_failed(exc: BaseException) -> bool:
+ """Connect / timeout failure only; HTTP answers are per repo and proxy errors are not cache fallbacks in HF."""
+ try:
+ import httpx
+ if isinstance(exc, httpx.ProxyError):
+ return False
+ if isinstance(exc, (httpx.ConnectError, httpx.TimeoutException)):
+ return True
+ except ImportError:
+ pass
+ try:
+ import requests
+ if isinstance(exc, requests.exceptions.ProxyError):
+ return False
+ if isinstance(exc, (requests.exceptions.ConnectionError, requests.exceptions.Timeout)):
+ return True
+ except ImportError:
+ pass
+ return isinstance(exc, (OSError, TimeoutError)) and not hasattr(exc, "response")
+
+
+def _endpoint() -> str:
+ try:
+ from huggingface_hub import constants
+ return str(constants.ENDPOINT)
+ except Exception: # noqa: BLE001
+ return ""
def _hub_coords(path: Any) -> Optional[tuple[str, str, str, str]]:
@@ -637,6 +668,11 @@ def _remote_etag(
hit = _REMOTE_MEMO.get(memo)
if hit is not None and time.monotonic() - hit[0] < _REMOTE_TTL_SECONDS:
return hit[1]
+ endpoint = _endpoint()
+ with _lock:
+ down_at = _UNREACHABLE.get(endpoint)
+ if down_at is not None and time.monotonic() - down_at < _REMOTE_TTL_SECONDS:
+ return None
try:
from huggingface_hub import get_hf_file_metadata, hf_hub_url
from huggingface_hub.utils import EntryNotFoundError
@@ -656,9 +692,13 @@ def _remote_etag(
value = str(etag) if etag else None # already normalized (no quotes, no W/ prefix)
except EntryNotFoundError:
value = _GONE
- except Exception: # noqa: BLE001 - offline, gated without a token, rate limited: unanswerable
+ except Exception as exc: # noqa: BLE001 - offline, gated without a token, rate limited: unanswerable
+ if _connection_failed(exc):
+ with _lock:
+ _UNREACHABLE[endpoint] = time.monotonic()
return None
with _lock:
+ _UNREACHABLE.pop(endpoint, None)
_REMOTE_MEMO[memo] = (time.monotonic(), value)
return value
diff --git a/studio/backend/tests/test_diffusion_prequant_cache.py b/studio/backend/tests/test_diffusion_prequant_cache.py
index b6644b941c9..9bd52aab9c4 100644
--- a/studio/backend/tests/test_diffusion_prequant_cache.py
+++ b/studio/backend/tests/test_diffusion_prequant_cache.py
@@ -98,6 +98,7 @@ def env(tmp_path, monkeypatch):
monkeypatch.delenv(C.ENV_MAX_GB, raising = False)
monkeypatch.delenv("UNSLOTH_ALLOW_LOCAL_PREQUANT_PATH", raising = False)
C._HEADER_MEMO.clear()
+ getattr(C, "_UNREACHABLE", {}).clear()
return root
@@ -1112,6 +1113,114 @@ def test_the_freshness_check_of_a_gated_base_carries_the_loads_token(env, monkey
assert seen == ["hf_request_token"]
+def _sharded_hub_snapshot(tmp_path: Path, n: int) -> list[str]:
+ import hashlib
+
+ repo_dir = tmp_path / "hub" / "models--org--sharded"
+ (repo_dir / "blobs").mkdir(parents = True)
+ snap = repo_dir / "snapshots" / ("c" * 40) / "transformer"
+ snap.mkdir(parents = True)
+ links = []
+ for i in range(n):
+ data = f"shard {i}".encode()
+ blob = repo_dir / "blobs" / hashlib.sha256(data).hexdigest()
+ blob.write_bytes(data)
+ link = snap / f"diffusion_pytorch_model-{i + 1:05d}-of-{n:05d}.safetensors"
+ os.symlink(blob, link)
+ links.append(str(link))
+ return links
+
+
+def blackholed_hub(monkeypatch):
+ """Every Hub request times out via a mocked transport; yields the requested URLs."""
+ import huggingface_hub
+
+ seen: list = []
+ monkeypatch.setattr(huggingface_hub.constants, "HF_HUB_OFFLINE", False)
+ C._REMOTE_MEMO.clear()
+ getattr(C, "_UNREACHABLE", {}).clear()
+ set_factory = getattr(huggingface_hub, "set_client_factory", None)
+ if set_factory is None: # older huggingface_hub (requests)
+ import requests
+
+ def metadata(url, **_k):
+ seen.append(str(url))
+ raise requests.exceptions.ReadTimeout("read timed out")
+
+ monkeypatch.setattr(huggingface_hub, "get_hf_file_metadata", metadata)
+ yield seen
+ return
+ import httpx
+
+ def handler(request):
+ seen.append(str(request.url))
+ raise httpx.ReadTimeout("timed out", request = request)
+
+ set_factory(lambda: httpx.Client(transport = httpx.MockTransport(handler)))
+ try:
+ yield seen
+ finally:
+ from huggingface_hub.utils._http import default_client_factory
+ set_factory(default_client_factory)
+
+
+def test_a_blackholed_hub_costs_one_probe_per_process_not_one_per_shard_per_lookup(
+ env, tmp_path, blackholed_hub
+):
+ """Unreachable but not flagged offline: 4 shards x 3 lookups used to cost 12 timeouts."""
+ shards = _sharded_hub_snapshot(tmp_path, 4)
+ for _ in range(3):
+ assert C._hub_source_stale(shards) is False
+ assert len(blackholed_hub) == 1
+ assert "org/sharded" in blackholed_hub[0]
+
+
+def test_an_http_answer_is_not_taken_for_an_unreachable_hub(env, tmp_path, monkeypatch):
+ """A gated / 5xx answer is per repo: it must not silence other probes."""
+ import huggingface_hub
+ from huggingface_hub.errors import HfHubHTTPError
+
+ monkeypatch.setattr(huggingface_hub.constants, "HF_HUB_OFFLINE", False)
+ C._REMOTE_MEMO.clear()
+ calls = []
+
+ def metadata(url, **_k):
+ calls.append(url)
+ raise HfHubHTTPError("503 Service Unavailable")
+
+ monkeypatch.setattr(huggingface_hub, "get_hf_file_metadata", metadata)
+ shards = _sharded_hub_snapshot(tmp_path, 2)
+ assert C._hub_source_stale(shards) is False
+ assert len(calls) == 2 and not C._UNREACHABLE
+
+
+def test_the_hub_is_asked_again_once_the_unreachable_verdict_expires(env, tmp_path, monkeypatch):
+ import huggingface_hub
+
+ shards = _sharded_hub_snapshot(tmp_path, 2)
+ blob = os.path.basename(os.path.realpath(shards[0]))
+ monkeypatch.setattr(huggingface_hub.constants, "HF_HUB_OFFLINE", False)
+ C._REMOTE_MEMO.clear()
+ calls = []
+
+ def down(url, **_k):
+ calls.append(url)
+ raise OSError("network unreachable")
+
+ monkeypatch.setattr(huggingface_hub, "get_hf_file_metadata", down)
+ assert C._hub_source_stale(shards) is False
+ assert len(calls) == 1
+ C._UNREACHABLE[C._endpoint()] -= C._REMOTE_TTL_SECONDS + 1
+ monkeypatch.setattr(
+ huggingface_hub,
+ "get_hf_file_metadata",
+ lambda url, **_k: calls.append(url) or types.SimpleNamespace(etag = "f" * 64),
+ )
+ assert C._hub_source_stale(shards) is True
+ assert len(calls) == 2 and blob != "f" * 64
+
+
def test_an_entry_the_loader_rejected_is_a_miss_until_replaced(env, tmp_path, scheme):
base, model = _write_base(tmp_path)
_quantize(model, scheme)