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: keep folder identity lookups inside the scan budget (follow-up to #14588) (#14638)

Michael Han5f2700229cfb
diff --git a/studio/backend/hub/services/models/local_inventory.py b/studio/backend/hub/services/models/local_inventory.py
index 7931e21f225..99c28fb0d4f 100644
--- a/studio/backend/hub/services/models/local_inventory.py
+++ b/studio/backend/hub/services/models/local_inventory.py
@@ -1526,11 +1526,22 @@ def _promote_to_custom_source(model: LocalModelInfo) -> LocalModelInfo:
)
+def custom_folder_identities(folders: list[dict], identity) -> list[str]:
+ """``identity(path)`` per folder under the scan budget, else the raw path: realpath stats every component, so
+ on a hung mount it parks the caller as long as the scan would."""
+ identities = []
+ for folder in folders:
+ raw = str(folder.get("path", ""))
+ resolved = run_custom_folder_bounded_sync(raw, lambda raw = raw: identity(raw))
+ identities.append(os.path.normcase(raw.strip()) if resolved is None else resolved)
+ return identities
+
+
def _list_custom_folders_with_identity() -> list[dict]:
- # The identity is a realpath, so it is taken here, off the loop: on a hung network mount it would stall every request.
+ folders = list_scan_folders()
return [
- {**folder, "_identity": _inventory_path_identity(str(folder.get("path", "")))}
- for folder in list_scan_folders()
+ {**folder, "_identity": key}
+ for folder, key in zip(folders, custom_folder_identities(folders, _inventory_path_identity))
]
diff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.py
index 4738460068f..68667cb2a53 100644
--- a/studio/backend/routes/models.py
+++ b/studio/backend/routes/models.py
@@ -1252,6 +1252,7 @@ async def _shared_compat_local_inventory_scan(
models_root: Path, sources: Optional[_CompatLocalInventorySources] = None
) -> List[LocalModelInfo]:
from storage.studio_db import list_scan_folders
+ from hub.services.models.local_inventory import custom_folder_identities
from hub.utils import inventory_scan as hf_cache_scan
requested_sources = sources
@@ -1306,13 +1307,16 @@ async def _shared_compat_local_inventory_scan(
except Exception as e:
logger.warning("Could not load custom scan folders: %s", e)
custom_folders = []
+ # Off the loop and under the scan budget: realpath on a hung mount blocks in the kernel.
+ identities = await asyncio.to_thread(
+ custom_folder_identities, custom_folders, _compat_inventory_path_identity
+ )
key: _CompatLocalInventoryKey = (
Path(_compat_inventory_path_identity(models_root)),
scan_sources,
tuple(
- _compat_inventory_path_identity(folder.get("path", ""))
- + ("\x00r" if folder.get("recursive") else "")
- for folder in custom_folders
+ identity + ("\x00r" if folder.get("recursive") else "")
+ for identity, folder in zip(identities, custom_folders)
),
epoch,
)
diff --git a/studio/backend/tests/test_scan_folder_scan_budget.py b/studio/backend/tests/test_scan_folder_scan_budget.py
index 0d9767ebf5b..c4c12b4cdb0 100644
--- a/studio/backend/tests/test_scan_folder_scan_budget.py
+++ b/studio/backend/tests/test_scan_folder_scan_budget.py
@@ -4,6 +4,7 @@
"""A custom folder behind a hung mount must not hold the model list hostage."""
import asyncio
+import os
import threading
import pytest
@@ -150,3 +151,79 @@ def test_macos_case_variants_are_one_folder(tmp_path, monkeypatch):
assert not inserted
assert row["path"] == real.upper() and row["recursive"]
assert len(scan_folders.list_scan_folders()) == 1
+
+
+def _block_on(hung: str, release: threading.Event, identity):
+ def resolve(raw):
+ if str(raw) == hung:
+ release.wait(5)
+ return identity(raw)
+
+ return resolve
+
+
+def test_a_hung_folder_cannot_stall_the_hub_folder_list(monkeypatch, tmp_path):
+ release = threading.Event()
+ healthy = str(tmp_path)
+ monkeypatch.setattr(
+ local_inventory,
+ "list_scan_folders",
+ lambda: [{"id": 1, "path": "/mnt/nas"}, {"id": 2, "path": healthy}],
+ )
+ monkeypatch.setattr(
+ local_inventory,
+ "_inventory_path_identity",
+ _block_on("/mnt/nas", release, local_inventory._inventory_path_identity),
+ )
+ try:
+ folders = asyncio.run(asyncio.wait_for(local_inventory._load_custom_folders(), 2))
+ assert [folder["_identity"] for folder in folders] == [
+ os.path.normcase("/mnt/nas"),
+ local_inventory._inventory_path_identity(healthy),
+ ]
+ assert _status("/mnt/nas") == STATUS_SLOW
+ finally:
+ release.set()
+
+
+def test_a_hung_folder_cannot_stall_the_compat_model_list(monkeypatch, tmp_path):
+ from routes import models as models_route
+
+ release = threading.Event()
+ sources = models_route._CompatLocalInventorySources(
+ tmp_path / "active", tmp_path / "legacy", tmp_path / "default", (), ()
+ )
+ monkeypatch.setattr(
+ "storage.studio_db.list_scan_folders", lambda: [{"id": 1, "path": "/mnt/nas"}]
+ )
+ monkeypatch.setattr(
+ models_route,
+ "_compat_inventory_path_identity",
+ _block_on("/mnt/nas", release, models_route._compat_inventory_path_identity),
+ )
+ monkeypatch.setattr(models_route, "collect_local_models", lambda *_args, **_kwargs: [])
+
+ async def listing_and_loop():
+ ticks = 0
+
+ async def tick():
+ nonlocal ticks
+ while True:
+ ticks += 1
+ await asyncio.sleep(0.01)
+
+ ticker = asyncio.create_task(tick())
+ try:
+ rows = await asyncio.wait_for(
+ models_route._shared_compat_local_inventory_scan(tmp_path / "models", sources), 2
+ )
+ finally:
+ ticker.cancel()
+ return rows, ticks
+
+ try:
+ rows, ticks = asyncio.run(listing_and_loop())
+ assert rows == [] and ticks > 5
+ assert _status("/mnt/nas") == STATUS_SLOW
+ finally:
+ release.set()