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: share one in-flight media model index build so the startup checks do not scan twice (#16075)

Daniel Hanbaa84e60b460
diff --git a/studio/backend/core/inference/media_model_index.py b/studio/backend/core/inference/media_model_index.py
index 310df430a63..5871fb0d0d1 100644
--- a/studio/backend/core/inference/media_model_index.py
+++ b/studio/backend/core/inference/media_model_index.py
@@ -342,26 +342,82 @@ def _mark_ambiguous_builds(index: dict[str, MediaModelPick]) -> dict[str, MediaM
}
+# Far above a full scan: past it a waiter scans on its own instead of hanging on a stuck build.
+_JOIN_TIMEOUT_S = 120.0
+
+
+class _Build:
+ __slots__ = ("epoch", "owner", "done", "result")
+
+ def __init__(self, epoch: tuple[int, Optional[int]]) -> None:
+ self.epoch = epoch
+ self.owner = threading.get_ident()
+ self.done = threading.Event()
+ self.result: Optional[dict[str, MediaModelPick]] = None
+
+
+_building: dict[tuple[str, str], _Build] = {}
+_invalidations = 0
+
+
+def _hf_cache_epoch() -> Optional[int]:
+ """Bumped by downloads, deletes and cache setting changes; takes its own lock, so never call under _index_lock."""
+ try:
+ from hub.utils.inventory_scan import hf_cache_scans_epoch
+ return hf_cache_scans_epoch()
+ except Exception: # noqa: BLE001 -- no inventory module: only invalidate_index() counts
+ return None
+
+
def _cached_index(task: str) -> dict[str, MediaModelPick]:
from utils.account_context import current_account_id
-
key = (current_account_id(), task)
- now = time.monotonic()
- with _index_lock:
- hit = _index.get(key)
- if hit is not None and now - hit[0] < _INDEX_TTL_S:
- return hit[1]
- built = _mark_ambiguous_builds(_build_index(task))
- with _index_lock:
- # stamped after the scan, so one slower than the ttl is not already expired
- _index[key] = (time.monotonic(), built)
- return built
+ while True:
+ now = time.monotonic()
+ cache_epoch = _hf_cache_epoch()
+ with _index_lock:
+ hit = _index.get(key)
+ if hit is not None and now - hit[0] < _INDEX_TTL_S:
+ return hit[1]
+ epoch = (_invalidations, cache_epoch)
+ build = _building.get(key)
+ if build is None or build.epoch != epoch or build.owner == threading.get_ident():
+ build = _Build(epoch)
+ _building[key] = build
+ owner = True
+ else:
+ owner = False
+ if not owner:
+ # Started no earlier than the last invalidation, finishes after this call: no older than a ttl hit.
+ if not build.done.wait(_JOIN_TIMEOUT_S):
+ # A builder waiting on this thread would deadlock: scan here instead.
+ return _mark_ambiguous_builds(_build_index(task))
+ if build.result is not None:
+ return build.result
+ continue # that build raised
+ try:
+ built = _mark_ambiguous_builds(_build_index(task))
+ cache_epoch = _hf_cache_epoch()
+ with _index_lock:
+ # Overtaken by an invalidation: its waiters still get it, the cache keeps the fresher scan.
+ if (_invalidations, cache_epoch) == build.epoch:
+ # stamped after the scan, so one slower than the ttl is not already expired
+ _index[key] = (time.monotonic(), built)
+ build.result = built
+ return built
+ finally:
+ with _index_lock:
+ if _building.get(key) is build:
+ del _building[key]
+ build.done.set()
def invalidate_index() -> None:
"""Drop the cached scan. For tests and anything that changes what is downloaded."""
+ global _invalidations
with _index_lock:
_index.clear()
+ _invalidations += 1
def resolve_local_media_model(name: str, *, task: str) -> Optional[MediaModelPick]:
diff --git a/studio/backend/tests/test_media_model_index_single_flight.py b/studio/backend/tests/test_media_model_index_single_flight.py
new file mode 100644
index 00000000000..703fcbea29a
--- /dev/null
+++ b/studio/backend/tests/test_media_model_index_single_flight.py
@@ -0,0 +1,267 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Callers asking for the same media index while one is being built wait for that build instead of
+walking every model root again: at startup the early quant probe and the post-warm diffusers gate
+ask at once, and each walk is a full local model scan."""
+
+from __future__ import annotations
+
+import threading
+
+import pytest
+
+import core.inference.media_model_index as index
+from hub.utils import inventory_scan
+from utils.account_context import AccountContext, run_as
+
+TASK = index.IMAGE_TASK
+ALICE = AccountContext("a" * 32, "alice")
+
+
+class _CountingEvent(threading.Event):
+ waiting = 0
+ _lock = threading.Lock()
+
+ def wait(self, timeout = None):
+ with _CountingEvent._lock:
+ _CountingEvent.waiting += 1
+ return super().wait(timeout)
+
+
[email protected](autouse = True)
+def counted_waits(monkeypatch):
+ """Count callers parked on an in-flight build (absent on a tree without single-flight)."""
+ _CountingEvent.waiting = 0
+ build_cls = getattr(index, "_Build", None)
+ if build_cls is not None:
+ original_init = build_cls.__init__
+
+ def init(self, epoch):
+ original_init(self, epoch)
+ self.done = _CountingEvent()
+
+ monkeypatch.setattr(build_cls, "__init__", init)
+
+
+def _until(predicate):
+ for _ in range(5000):
+ if predicate():
+ return
+ threading.Event().wait(0.002)
+ raise AssertionError("timed out")
+
+
+def gated_build(monkeypatch):
+ """_build_index that blocks until released and counts its calls."""
+ calls = []
+ started = threading.Event()
+ release = threading.Event()
+
+ def fake_build(task):
+ calls.append((threading.get_ident(), task))
+ started.set()
+ assert release.wait(10)
+ return {"flux-dev": index.MediaModelPick("flux-dev", "/models/flux-dev", None, None)}
+
+ monkeypatch.setattr(index, "_build_index", fake_build)
+ index.invalidate_index()
+ yield calls, started, release
+ release.set()
+ index.invalidate_index()
+
+
+def _ids_in_thread(results, account = None):
+ def run():
+ if account is None:
+ results.append(index.available_media_model_ids(TASK))
+ else:
+ results.append(run_as(account, index.available_media_model_ids, TASK))
+
+ thread = threading.Thread(target = run)
+ thread.start()
+ return thread
+
+
+def test_concurrent_callers_share_one_build(gated_build):
+ calls, started, release = gated_build
+ results = []
+ first = _ids_in_thread(results)
+ assert started.wait(10)
+ second = _ids_in_thread(results)
+ third = _ids_in_thread(results)
+ # Joined (parked on the first build) or, without single-flight, walking on their own.
+ _until(lambda: _CountingEvent.waiting + len(calls) >= 3)
+ release.set()
+ for thread in (first, second, third):
+ thread.join(10)
+ assert results == [["flux-dev"]] * 3
+ assert len(calls) == 1
+
+
+def test_a_build_started_before_an_invalidation_is_not_joined(gated_build):
+ calls, started, release = gated_build
+ results = []
+ first = _ids_in_thread(results)
+ assert started.wait(10)
+ # A download or delete landed after that walk began: the next caller must walk again.
+ inventory_scan.invalidate_hf_cache_scans(sizes = False)
+ second = _ids_in_thread(results)
+ _until(lambda: len(calls) >= 2 or _CountingEvent.waiting >= 1)
+ release.set()
+ first.join(10)
+ second.join(10)
+ assert len(calls) == 2
+
+
+def test_invalidate_index_also_starts_a_new_build(gated_build):
+ calls, started, release = gated_build
+ results = []
+ first = _ids_in_thread(results)
+ assert started.wait(10)
+ index.invalidate_index()
+ second = _ids_in_thread(results)
+ _until(lambda: len(calls) >= 2 or _CountingEvent.waiting >= 1)
+ release.set()
+ first.join(10)
+ second.join(10)
+ assert len(calls) == 2
+
+
+def test_another_account_never_joins(gated_build):
+ calls, started, release = gated_build
+ results = []
+ first = _ids_in_thread(results)
+ assert started.wait(10)
+ second = _ids_in_thread(results, ALICE)
+ _until(lambda: len(calls) >= 2 or _CountingEvent.waiting >= 1)
+ release.set()
+ first.join(10)
+ second.join(10)
+ assert len(calls) == 2
+
+
+def test_a_failed_build_is_retried_by_its_waiters(monkeypatch):
+ calls = []
+ started = threading.Event()
+ release = threading.Event()
+
+ def flaky_build(task):
+ calls.append(task)
+ if len(calls) == 1:
+ started.set()
+ assert release.wait(10)
+ raise OSError("root vanished mid-walk")
+ return {"flux-dev": index.MediaModelPick("flux-dev", "/models/flux-dev", None, None)}
+
+ monkeypatch.setattr(index, "_build_index", flaky_build)
+ index.invalidate_index()
+ errors, results = [], []
+
+ def owner():
+ try:
+ index.available_media_model_ids(TASK)
+ except OSError as exc:
+ errors.append(exc)
+
+ first = threading.Thread(target = owner)
+ first.start()
+ assert started.wait(10)
+ second = _ids_in_thread(results)
+ _until(lambda: _CountingEvent.waiting + len(calls) >= 2)
+ release.set()
+ first.join(10)
+ second.join(10)
+ index.invalidate_index()
+ assert len(errors) == 1
+ assert results == [["flux-dev"]]
+ assert len(calls) == 2
+
+
+def test_a_reentrant_call_from_the_building_thread_does_not_deadlock(monkeypatch):
+ depth = []
+
+ def nested_build(task):
+ depth.append(task)
+ if len(depth) == 1:
+ # Same thread, same key, while its own build is in flight.
+ index.available_media_model_ids(TASK)
+ return {}
+
+ monkeypatch.setattr(index, "_build_index", nested_build)
+ index.invalidate_index()
+ done = []
+ thread = threading.Thread(target = lambda: done.append(index.available_media_model_ids(TASK)))
+ thread.start()
+ thread.join(10)
+ index.invalidate_index()
+ assert not thread.is_alive()
+ assert done == [[]]
+ assert len(depth) == 2
+
+
+def test_a_fresh_entry_still_answers_without_a_build(gated_build):
+ calls, started, release = gated_build
+ release.set()
+ assert index.available_media_model_ids(TASK) == ["flux-dev"]
+ assert index.available_media_model_ids(TASK) == ["flux-dev"]
+ assert len(calls) == 1
+
+
[email protected]("invalidate", ["index", "hf_cache"])
+def test_a_build_overtaken_by_an_invalidation_is_not_cached(monkeypatch, invalidate):
+ """A walk that began before a download finishes after the fresh walk: its answer must not shadow the fresh one."""
+ calls = []
+ started = threading.Event()
+ release = threading.Event()
+
+ def build(task):
+ calls.append(task)
+ if len(calls) == 1:
+ started.set()
+ assert release.wait(10)
+ return {"old": index.MediaModelPick("old", "/models/old", None, None)}
+ return {"new": index.MediaModelPick("new", "/models/new", None, None)}
+
+ monkeypatch.setattr(index, "_build_index", build)
+ index.invalidate_index()
+ results = []
+ first = _ids_in_thread(results)
+ assert started.wait(10)
+ if invalidate == "index":
+ index.invalidate_index()
+ else:
+ inventory_scan.invalidate_hf_cache_scans(sizes = False)
+ assert index.available_media_model_ids(TASK) == ["new"]
+ release.set()
+ first.join(10)
+ index_after = index.available_media_model_ids(TASK)
+ index.invalidate_index()
+ assert results == [["old"]]
+ assert index_after == ["new"]
+ assert len(calls) == 2
+
+
+def test_a_build_waiting_on_a_thread_that_asks_the_index_does_not_deadlock(monkeypatch):
+ """A waiter gives up on a build that never finishes and scans on its own, so a builder that hands work to a
+ helper thread asking the index cannot lock both threads."""
+ monkeypatch.setattr(index, "_JOIN_TIMEOUT_S", 0.2)
+ calls = []
+
+ def build(task):
+ calls.append(1)
+ if len(calls) == 1:
+ box = []
+ helper = threading.Thread(
+ target = lambda: box.append(index.available_media_model_ids(TASK))
+ )
+ helper.start()
+ helper.join(5)
+ assert not helper.is_alive() and box == [["helper"]]
+ return {"helper": index.MediaModelPick("helper", "/models/helper", None, None)}
+
+ monkeypatch.setattr(index, "_build_index", build)
+ index.invalidate_index()
+ assert index.available_media_model_ids(TASK) == ["helper"]
+ assert len(calls) == 2