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: refresh cache sizes that are being re-measured instead of showing the old number (#15718)

Daniel Haned9857c0c35f
diff --git a/studio/backend/hub/utils/inventory_scan.py b/studio/backend/hub/utils/inventory_scan.py
index 5a08e47d060..16431c3a02f 100644
--- a/studio/backend/hub/utils/inventory_scan.py
+++ b/studio/backend/hub/utils/inventory_scan.py
@@ -93,7 +93,8 @@ async def shared_scan(
return await asyncio.shield(flight)
-def invalidate_hf_cache_scans() -> None:
+def invalidate_hf_cache_scans(*, sizes: bool = True) -> None:
+ """``sizes=False`` is for a Settings purge, which has already forgotten the caches it cleared."""
global _hf_cache_scans_result, _hf_cache_scans_cached_at, _hf_cache_scans_epoch
with _hf_cache_scans_lock:
_hf_cache_scans_result = None
@@ -101,12 +102,13 @@ def invalidate_hf_cache_scans() -> None:
_hf_cache_scans_epoch += 1
invalidate_scan_memos()
# Settings may answer a slow cache from its last walk; a download or delete must not show the old size.
- try:
- from utils.cache_inventory import invalidate_cache_size
- for key in ("hf_hub", "hf_xet", "hf_datasets", "hf_assets"):
- invalidate_cache_size(key)
- except Exception: # noqa: BLE001 - the Settings sizes are best effort
- pass
+ if sizes:
+ try:
+ from utils.cache_inventory import invalidate_cache_size
+ for key in ("hf_hub", "hf_xet", "hf_datasets", "hf_assets"):
+ invalidate_cache_size(key)
+ except Exception: # noqa: BLE001 - the Settings sizes are best effort
+ pass
try:
from core.inference.audio_cpp_models import forget
except Exception: # noqa: BLE001 - no audio.cpp support, nothing memoized
diff --git a/studio/backend/routes/settings.py b/studio/backend/routes/settings.py
index dd44c2261bc..01115ef8d53 100644
--- a/studio/backend/routes/settings.py
+++ b/studio/backend/routes/settings.py
@@ -920,6 +920,7 @@ class CacheInventoryResponse(BaseModel):
reclaimable_bytes: int
free_bytes: Optional[int] = None
total_disk_bytes: Optional[int] = None
+ refreshing: bool = False
class CachePurgePayload(BaseModel):
diff --git a/studio/backend/tests/test_cache_inventory_walk.py b/studio/backend/tests/test_cache_inventory_walk.py
index 560218d4540..77c95276f40 100644
--- a/studio/backend/tests/test_cache_inventory_walk.py
+++ b/studio/backend/tests/test_cache_inventory_walk.py
@@ -164,6 +164,10 @@ def clean_memo(monkeypatch):
monkeypatch.setattr(cache_inventory, "_size_flights", {})
# A background walk an earlier test left running (a real cache walk under load) must not hold these up.
monkeypatch.setattr(cache_inventory, "_background_walk_lock", threading.Lock())
+ monkeypatch.setattr(cache_inventory, "_background_pending", set())
+ yield
+ # A worker a test left behind must finish before the patched state is put back.
+ _wait_for_background_walks()
def _expire(key: str, *, walk_seconds: float) -> None:
@@ -336,3 +340,139 @@ def test_the_inventory_starts_background_walks_after_its_own(monkeypatch, clean_
foreground = [key for kind, key in order if kind == "walk" and key != "uv"]
assert len(foreground) == len(cache_inventory.CACHE_DEFINITIONS) - 1
assert order.index(("background", "uv")) == len(foreground)
+
+
+def _wait_for_background_walks(timeout: float = 10.0) -> None:
+ deadline = time.monotonic() + timeout
+ for thread in list(threading.enumerate()):
+ if thread.name.startswith("cache-size-"):
+ thread.join(max(0.0, deadline - time.monotonic()))
+
+
+def test_a_queued_background_walk_does_not_hold_a_read_of_its_cache(monkeypatch, clean_memo):
+ """A purge or download of a cache whose refresh is queued behind another must not wait for that queue."""
+ uv_walking = threading.Event()
+ release_uv = threading.Event()
+ sizes = {"uv": iter([1, 2]), "triton": iter([10, 20, 30])}
+
+ def describe(target):
+ size = next(sizes[target.key])
+ if target.key == "uv" and size == 2:
+ uv_walking.set()
+ release_uv.wait(10)
+ return {"key": target.key, "size_bytes": size}
+
+ monkeypatch.setattr(cache_inventory, "describe_cache", describe)
+ for key in ("uv", "triton"):
+ cache_inventory._described(definition_for(key), refresh = False)
+ _expire(key, walk_seconds = 30)
+ try:
+ assert cache_inventory._described(definition_for("uv"), refresh = False)["size_bytes"] == 1
+ assert uv_walking.wait(5)
+ # triton's refresh is now queued behind uv's walk.
+ assert (
+ cache_inventory._described(definition_for("triton"), refresh = False)["size_bytes"] == 10
+ )
+ cache_inventory.invalidate_cache_size("triton")
+ answer: list = []
+ reader = threading.Thread(
+ target = lambda: answer.append(
+ cache_inventory._described(definition_for("triton"), refresh = False)
+ ),
+ daemon = True,
+ )
+ reader.start()
+ reader.join(2.0)
+ assert answer, "the read waited for a background walk queued behind another cache"
+ assert answer[0]["size_bytes"] == 20
+ finally:
+ release_uv.set()
+ _wait_for_background_walks()
+ # The queued refresh found a size newer than the one it was asked to replace and did not walk again.
+ assert cache_inventory._size_cache["triton"][2]["size_bytes"] == 20
+
+
+def test_a_refresh_that_cannot_start_walks_in_the_request(monkeypatch, clean_memo):
+ definition = definition_for("uv")
+ sizes = iter([10, 20])
+ monkeypatch.setattr(
+ cache_inventory,
+ "describe_cache",
+ lambda target: {"key": target.key, "size_bytes": next(sizes)},
+ )
+ assert cache_inventory._described(definition, refresh = False)["size_bytes"] == 10
+ _expire("uv", walk_seconds = 30)
+
+ def refuse(self):
+ raise RuntimeError("can't start new thread")
+
+ monkeypatch.setattr(threading.Thread, "start", refuse)
+ assert cache_inventory._described(definition, refresh = False)["size_bytes"] == 20
+
+
+def test_recheck_accepts_a_walk_begun_after_the_request(monkeypatch, clean_memo):
+ definition = definition_for("triton")
+ sizes = iter([10, 20])
+ monkeypatch.setattr(
+ cache_inventory,
+ "describe_cache",
+ lambda target: {"key": target.key, "size_bytes": next(sizes)},
+ )
+ asked = time.monotonic()
+ assert cache_inventory._described(definition, refresh = False)["size_bytes"] == 10
+ # The walk above began after the Recheck was asked for, so the Recheck does not repeat it.
+ assert cache_inventory._described(definition, refresh = True, asked_at = asked)["size_bytes"] == 10
+
+
+def _absent(target) -> dict:
+ return {
+ "key": target.key,
+ "size_bytes": 1,
+ "present": False,
+ "purgeable": False,
+ "opt_in": False,
+ }
+
+
+def test_the_inventory_says_when_a_size_is_being_refreshed(monkeypatch, clean_memo):
+ """The page asks again while a slow cache re-walks, so an old size does not stay on screen."""
+ release = threading.Event()
+
+ def describe(target):
+ if target.key == "uv" and "uv" in cache_inventory._size_cache:
+ release.wait(10)
+ return _absent(target)
+
+ monkeypatch.setattr(cache_inventory, "describe_cache", describe)
+ monkeypatch.setattr(cache_inventory, "_free_bytes", lambda: None)
+ monkeypatch.setattr(cache_inventory, "_total_disk_bytes", lambda: None)
+ assert cache_inventory.cache_inventory()["refreshing"] is False
+ _expire("uv", walk_seconds = 30)
+ try:
+ assert cache_inventory.cache_inventory()["refreshing"] is True
+ assert cache_inventory.cache_inventory()["refreshing"] is True
+ finally:
+ release.set()
+ _wait_for_background_walks()
+ assert cache_inventory.cache_inventory()["refreshing"] is False
+
+
+def test_clearing_one_hugging_face_cache_keeps_the_other_sizes(monkeypatch, clean_memo):
+ """Base re-walked only the cleared cache; the siblings' sizes did not change."""
+ walked: list = []
+ monkeypatch.setattr(
+ cache_inventory,
+ "describe_cache",
+ lambda target: (walked.append(target.key), _absent(target))[1],
+ )
+ monkeypatch.setattr(cache_inventory, "_free_bytes", lambda: None)
+ monkeypatch.setattr(cache_inventory, "_total_disk_bytes", lambda: None)
+ monkeypatch.setattr(
+ cache_inventory,
+ "purge_cache",
+ lambda key: {"key": key, "freed_bytes": 0, "removed_entries": 0, "errors": []},
+ )
+ cache_inventory.cache_inventory()
+ walked.clear()
+ cache_inventory.purge_caches(["hf_hub"])
+ assert walked == ["hf_hub"]
diff --git a/studio/backend/utils/cache_inventory.py b/studio/backend/utils/cache_inventory.py
index b9a2b1f9306..26333492130 100644
--- a/studio/backend/utils/cache_inventory.py
+++ b/studio/backend/utils/cache_inventory.py
@@ -1120,6 +1120,8 @@ _size_flights: dict[str, threading.Lock] = {}
_size_flights_lock = threading.Lock()
# Background walks run one at a time: two stat-bound walks in one process only trade the GIL.
_background_walk_lock = threading.Lock()
+# Caches with a background walk queued or running; the walk takes its flight only when it runs.
+_background_pending: set = set()
def _flight_for(key: str) -> threading.Lock:
@@ -1138,28 +1140,54 @@ def _walk_and_remember(definition: CacheDefinition, epoch: int) -> dict:
return entry
-def _refresh_in_background(definition: CacheDefinition) -> None:
- """At most one walk per cache: one already running, foreground or not, stores the newer size anyway."""
- flight = _flight_for(definition.key)
- if not flight.acquire(blocking = False):
- return
+def _refresh_in_background(definition: CacheDefinition) -> bool:
+ """At most one walk per cache; False when none could start, so the caller walks instead."""
+ key = definition.key
+ with _size_cache_lock:
+ if key in _background_pending:
+ return True
+ _background_pending.add(key)
def walk() -> None:
try:
with _background_walk_lock:
- with _size_cache_lock:
- epoch = _size_epochs.get(definition.key, 0)
- _walk_and_remember(definition, epoch)
+ # Taken only once this walk can run, so a queued walk never holds up a read of its cache.
+ flight = _flight_for(key)
+ if not flight.acquire(blocking = False):
+ return
+ try:
+ with _size_cache_lock:
+ epoch = _size_epochs.get(key, 0)
+ remembered = _size_cache.get(key)
+ # Overtaken while queued: a purge, download or Recheck already replaced the old size.
+ if (
+ remembered is None
+ or time.monotonic() - remembered[1] < _INVENTORY_TTL_SECONDS
+ ):
+ return
+ _walk_and_remember(definition, epoch)
+ finally:
+ flight.release()
except Exception as exc: # noqa: BLE001 - a failed refresh leaves the last size in place
- logger.debug(f"Could not refresh the {definition.key} cache size: {exc}")
+ logger.debug(f"Could not refresh the {key} cache size: {exc}")
finally:
- flight.release()
+ with _size_cache_lock:
+ _background_pending.discard(key)
try:
- threading.Thread(target = walk, name = f"cache-size-{definition.key}", daemon = True).start()
+ threading.Thread(target = walk, name = f"cache-size-{key}", daemon = True).start()
except Exception as exc: # noqa: BLE001 - e.g. no thread left; the next read walks in the foreground
- flight.release()
- logger.debug(f"Could not start a {definition.key} cache size refresh: {exc}")
+ with _size_cache_lock:
+ _background_pending.discard(key)
+ remembered = _size_cache.get(key)
+ if (
+ remembered is not None
+ and time.monotonic() - remembered[1] >= _INVENTORY_TTL_SECONDS
+ ):
+ del _size_cache[key]
+ logger.debug(f"Could not start a {key} cache size refresh: {exc}")
+ return False
+ return True
def _described(
@@ -1167,9 +1195,11 @@ def _described(
*,
refresh: bool,
deferred: Optional[list] = None,
+ asked_at: Optional[float] = None,
) -> dict:
- """``deferred`` collects the background refreshes for the caller to start, after its own walks."""
+ """``deferred`` collects background refreshes to start after the caller's walks; a forced read accepts any walk begun after ``asked_at``."""
started = time.monotonic()
+ asked = started if asked_at is None else asked_at
with _size_cache_lock:
remembered = None if refresh else _size_cache.get(definition.key)
if remembered is not None:
@@ -1177,11 +1207,11 @@ def _described(
return remembered[2]
# A forced read (the Recheck button) still waits for a fresh walk below.
if remembered[3] >= _BACKGROUND_WALK_SECONDS:
- if deferred is None:
- _refresh_in_background(definition)
- else:
+ if deferred is not None:
deferred.append(definition)
- return remembered[2]
+ return remembered[2]
+ if _refresh_in_background(definition):
+ return remembered[2]
# One walk per cache at a time, or simultaneous misses (e.g. two open tabs) each pay a cold walk.
with _flight_for(definition.key):
with _size_cache_lock:
@@ -1190,7 +1220,7 @@ def _described(
# A forced read needs a walk that began after it asked.
if remembered is not None:
if refresh:
- if remembered[0] >= started:
+ if remembered[0] >= asked:
return remembered[2]
elif time.monotonic() - remembered[1] < _INVENTORY_TTL_SECONDS:
return remembered[2]
@@ -1200,8 +1230,9 @@ def _described(
def cache_inventory(*, refresh: bool = False) -> dict:
# Started after the foreground walks, which would otherwise share the GIL with them.
deferred: list = []
+ asked_at = time.monotonic()
entries = [
- _described(definition, refresh = refresh, deferred = deferred)
+ _described(definition, refresh = refresh, deferred = deferred, asked_at = asked_at)
for definition in CACHE_DEFINITIONS
]
for definition in deferred:
@@ -1218,6 +1249,7 @@ def cache_inventory(*, refresh: bool = False) -> dict:
"reclaimable_bytes": reclaimable,
"free_bytes": _free_bytes(),
"total_disk_bytes": _total_disk_bytes(),
+ "refreshing": bool(deferred),
}
@@ -1501,7 +1533,7 @@ def _invalidate_hf_scans() -> None:
except ImportError as exc:
logger.debug(f"Could not invalidate the Hugging Face scans: {exc}")
return
- invalidate_hf_cache_scans()
+ invalidate_hf_cache_scans(sizes = False)
def invalidate_hf_rooted_sizes() -> None:
diff --git a/studio/frontend/src/features/settings/api/caches.ts b/studio/frontend/src/features/settings/api/caches.ts
index 08ca51835a4..7189d4df198 100644
--- a/studio/frontend/src/features/settings/api/caches.ts
+++ b/studio/frontend/src/features/settings/api/caches.ts
@@ -49,6 +49,7 @@ export type CacheInventory = {
reclaimableBytes: number;
freeBytes: number | null;
totalDiskBytes: number | null;
+ refreshing: boolean;
};
export type CachePurgeResult = {
@@ -98,6 +99,7 @@ type ApiCacheInventory = {
free_bytes: number | null;
// biome-ignore lint/style/useNamingConvention: API schema
total_disk_bytes: number | null;
+ refreshing?: boolean;
};
type ApiCachePurgeResult = {
@@ -141,6 +143,7 @@ export function inventoryFromApi(value: ApiCacheInventory): CacheInventory {
reclaimableBytes: value.reclaimable_bytes,
freeBytes: value.free_bytes,
totalDiskBytes: value.total_disk_bytes,
+ refreshing: value.refreshing === true,
};
}
diff --git a/studio/frontend/src/features/settings/components/cache-storage-rows.tsx b/studio/frontend/src/features/settings/components/cache-storage-rows.tsx
index 26a91f0b5a0..14ee29bf612 100644
--- a/studio/frontend/src/features/settings/components/cache-storage-rows.tsx
+++ b/studio/frontend/src/features/settings/components/cache-storage-rows.tsx
@@ -104,6 +104,10 @@ export function presentCaches(inventory: CacheInventory | null): CacheEntry[] {
);
}
+// A slow cache can answer from its last walk while it re-walks; ask again until every size is current.
+const FOLLOW_UP_MS = 2000;
+const FOLLOW_UP_LIMIT = 60;
+
export function useCacheStorage() {
const t = useT();
const [inventory, setInventory] = useState<CacheInventory | null>(null);
@@ -113,28 +117,57 @@ export function useCacheStorage() {
// Only the newest measurement installs itself; overlapping ones finish in any order.
const latestRequest = useRef(0);
+ const followUp = useRef<ReturnType<typeof setTimeout> | null>(null);
+
+ const cancelFollowUp = useCallback(() => {
+ if (followUp.current !== null) clearTimeout(followUp.current);
+ followUp.current = null;
+ }, []);
+
+ const scheduleFollowUp = useCallback(
+ (wanted: boolean, request: number, run: () => void) => {
+ if (!wanted) return;
+ followUp.current = setTimeout(() => {
+ followUp.current = null;
+ if (request === latestRequest.current) run();
+ }, FOLLOW_UP_MS);
+ },
+ [],
+ );
const refresh = useCallback(
- async (options: { refresh?: boolean } = {}) => {
- const request = ++latestRequest.current;
- setLoading(true);
- try {
- const next = await loadCacheInventory(options);
- if (request !== latestRequest.current) return;
- setInventory(next);
- setLoadError(null);
- } catch (error) {
- if (request !== latestRequest.current) return;
- setLoadError(
- error instanceof Error
- ? error.message
- : t("settings.resources.storage.caches.measureFailed"),
- );
- } finally {
- if (request === latestRequest.current) setLoading(false);
- }
+ async (options: { refresh?: boolean } = {}, firstAttempt = 0) => {
+ cancelFollowUp();
+ // A follow-up only swaps in the newer sizes: no measuring state, and a failed one keeps the rows.
+ const load = async (attempt: number) => {
+ const request = ++latestRequest.current;
+ if (attempt === 0) setLoading(true);
+ try {
+ const next = await loadCacheInventory(attempt === 0 ? options : {});
+ if (request !== latestRequest.current) return;
+ setInventory(next);
+ setLoadError(null);
+ scheduleFollowUp(
+ next.refreshing && attempt < FOLLOW_UP_LIMIT,
+ request,
+ () => void load(attempt + 1),
+ );
+ } catch (error) {
+ if (request !== latestRequest.current || attempt > 0) return;
+ setLoadError(
+ error instanceof Error
+ ? error.message
+ : t("settings.resources.storage.caches.measureFailed"),
+ );
+ } finally {
+ if (attempt === 0 && request === latestRequest.current) {
+ setLoading(false);
+ }
+ }
+ };
+ await load(firstAttempt);
},
- [t],
+ [t, cancelFollowUp, scheduleFollowUp],
);
// Saving Models Folder bumps the inventory version; re-measure so rows match the new folder.
@@ -146,6 +179,12 @@ export function useCacheStorage() {
const moved = measuredVersion.current !== inventoryVersion;
measuredVersion.current = inventoryVersion;
void refresh(moved ? { refresh: true } : {});
+ // Leaving the page also drops a follow-up read still in flight.
+ return () => {
+ latestRequest.current += 1;
+ if (followUp.current !== null) clearTimeout(followUp.current);
+ followUp.current = null;
+ };
}, [refresh, inventoryVersion]);
const entries = presentCaches(inventory);
@@ -154,10 +193,18 @@ export function useCacheStorage() {
const runPurge = async (keys: readonly CacheKey[]): Promise<boolean> => {
setClearing(true);
+ cancelFollowUp();
const request = ++latestRequest.current;
try {
const outcome = await purgeCaches(keys);
- if (request === latestRequest.current) setInventory(outcome.inventory);
+ if (request === latestRequest.current) {
+ setInventory(outcome.inventory);
+ scheduleFollowUp(
+ outcome.inventory.refreshing,
+ request,
+ () => void refresh({}, 1),
+ );
+ }
if (keys.some((key) => HUB_INVENTORY_KEYS.has(key))) {
// Our own bump is marked so the rows skip a cold walk for data they already hold.
bumpInventoryVersion();
diff --git a/studio/frontend/tests/cache-rows-model-folder.test.ts b/studio/frontend/tests/cache-rows-model-folder.test.ts
index 6233c875cef..ae347169091 100644
--- a/studio/frontend/tests/cache-rows-model-folder.test.ts
+++ b/studio/frontend/tests/cache-rows-model-folder.test.ts
@@ -197,16 +197,21 @@ function driveRows(options: {
"./storage-overview": { SettingsCardRow: "SettingsCardRow" },
});
- return () => {
+ let cleanup: (() => void) | undefined;
+ const render = () => {
stateCursor = 0;
refCursor = 0;
callbackCursor = 0;
const tree = rows.CacheStorageRows({ cache: rows.useCacheStorage() });
- const effect = pending;
+ const effect = pending as (() => (() => void) | undefined) | null;
pending = null;
- effect?.();
+ if (effect) {
+ cleanup?.();
+ cleanup = effect() ?? undefined;
+ }
return tree;
};
+ return Object.assign(render, { unmount: () => cleanup?.() });
}
type Element = { type: unknown; props?: Record<string, unknown> };
@@ -349,3 +354,64 @@ test("a measurement that failed leaves the clears disabled", async () => {
);
assert.equal(destructive[0].props?.disabled, true);
});
+
+test("a size still being re-walked is asked for again until it is current", async (t) => {
+ t.mock.timers.enable({ apis: ["setTimeout"] });
+ const pending: ((value: unknown) => void)[] = [];
+ const asked: { refresh?: boolean }[] = [];
+ const render = driveRows({
+ load: (options) => {
+ asked.push(options);
+ return new Promise((resolve) => pending.push(resolve));
+ },
+ version: () => 0,
+ });
+
+ render();
+ pending[0]({ ...inventory("/old/hub"), refreshing: true });
+ await settle();
+ const shown = render();
+ // The old size stays on screen, without the measuring state, while the walk runs.
+ assert.equal(buttons(shown, CLEAR_ONE).length, 0);
+ click(buttons(shown, DETAILS)[0]);
+ assert.equal(buttons(render(), CLEAR_ONE)[0].props?.disabled, false);
+ assert.equal(pending.length, 1);
+
+ t.mock.timers.tick(2000);
+ await settle();
+ assert.equal(pending.length, 2);
+ assert.deepEqual(asked[1], {});
+ assert.equal(buttons(render(), CLEAR_ONE)[0].props?.disabled, false);
+ pending[1]({ ...inventory("/new/hub"), refreshing: false });
+ await settle();
+
+ t.mock.timers.tick(10_000);
+ await settle();
+ assert.equal(pending.length, 2, "a current answer is not asked for again");
+ const paths = [...walk(render())]
+ .map((element) => element.props?.title)
+ .filter((title) => typeof title === "string");
+ assert.deepEqual(paths, ["/new/hub"]);
+});
+
+test("leaving the page stops a follow-up read still in flight", async (t) => {
+ t.mock.timers.enable({ apis: ["setTimeout"] });
+ const pending: ((value: unknown) => void)[] = [];
+ const render = driveRows({
+ load: () => new Promise((resolve) => pending.push(resolve)),
+ version: () => 0,
+ });
+
+ render();
+ pending[0]({ ...inventory("/old/hub"), refreshing: true });
+ await settle();
+ t.mock.timers.tick(2000);
+ await settle();
+ assert.equal(pending.length, 2);
+ render.unmount();
+ pending[1]({ ...inventory("/old/hub"), refreshing: true });
+ await settle();
+ t.mock.timers.tick(10_000);
+ await settle();
+ assert.equal(pending.length, 2);
+});