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.

Clear the metadata directory cache before scanning installed records (#10797)

小云f29beb2e615b
diff --git a/studio/install_manifest.py b/studio/install_manifest.py
index 7b773ef7550..8dc619c0026 100644
--- a/studio/install_manifest.py
+++ b/studio/install_manifest.py
@@ -151,10 +151,18 @@ def _metadata_scan_paths() -> List[str]:
def _installed_metadata_records(dist_name: str) -> List[Tuple[str, Optional[Path]]]:
"""Every matching metadata version and its directory, when available."""
- from importlib.metadata import distributions
+ from importlib.metadata import MetadataPathFinder, distributions
wanted = _canonical(dist_name)
paths = _metadata_scan_paths()
+ # The listing cache is keyed on the directory's st_mtime, so a dist-info added since an earlier
+ # scan in this process stays invisible while that mtime holds (two writes in one tick; exFAT 2s,
+ # HFS+ 1s), and a damaged install verifies as healthy. Via an INSTANCE: invalidate_caches only
+ # became a classmethod in 3.11.9 / 3.12.3 (gh-116811), and importlib.invalidate_caches() gained
+ # its delegation there too, so before those neither the class call nor the caller works. 3.9
+ # resolves it to MetaPathFinder's no-op, which is right: its FastPath does not cache.
+ if getattr(MetadataPathFinder, "invalidate_caches", None) is not None:
+ MetadataPathFinder().invalidate_caches()
kwargs = {"path": paths} if paths else {}
found: List[Tuple[str, Optional[Path]]] = []
for dist in distributions(**kwargs):
diff --git a/tests/studio/install/test_install_manifest.py b/tests/studio/install/test_install_manifest.py
index 58b8ccfe565..a493a03c239 100644
--- a/tests/studio/install/test_install_manifest.py
+++ b/tests/studio/install/test_install_manifest.py
@@ -13,6 +13,7 @@ from __future__ import annotations
import importlib.util
import json
+import os
import pathlib
import sysconfig
@@ -211,6 +212,91 @@ def test_malformed_matching_metadata_invalidates_the_manifest(
assert state["reason"] == "studio_install_metadata_conflict"
+def test_a_metadata_record_added_since_the_last_scan_is_seen(tmp_path, monkeypatch):
+ """The test above, made filesystem-independent: the cache is keyed on the directory's st_mtime,
+ which creating a dist-info normally moves by itself. Restoring it reproduces the staleness
+ everywhere, not just on exFAT (2s) and HFS+ (1s).
+ """
+ site = tmp_path / "site-packages"
+ site.mkdir()
+ _write_dist_metadata(site, "demo", "1.0")
+ monkeypatch.setattr(im, "_metadata_scan_paths", lambda: [str(site)])
+
+ assert im.installed_versions("demo") == ["1.0"]
+ unchanged = site.stat().st_mtime_ns
+
+ malformed = site / "demo-2.0.dist-info"
+ malformed.mkdir()
+ (malformed / "METADATA").write_bytes(b"\xff\xfe")
+ os.utime(site, ns = (unchanged, unchanged))
+
+ versions = im.installed_versions("demo")
+ assert versions == ["", "1.0"]
+ assert im.metadata_conflict(versions) is True
+
+
+def test_the_metadata_scan_survives_a_pre_classmethod_invalidate_caches(tmp_path, monkeypatch):
+ """invalidate_caches only became a classmethod in 3.11.9 / 3.12.3 (gh-116811), so before those
+ a class-level call raised TypeError and took verify_install with it. No CI leg runs them.
+ """
+ import importlib.metadata
+
+ fast_path = getattr(importlib.metadata, "FastPath", None)
+ if fast_path is None or not hasattr(fast_path.__new__, "cache_clear"):
+ pytest.skip("this interpreter has no importlib.metadata directory listing cache")
+
+ def invalidate_caches(cls):
+ fast_path.__new__.cache_clear()
+
+ monkeypatch.setattr(
+ importlib.metadata.MetadataPathFinder, "invalidate_caches", invalidate_caches
+ )
+
+ site = tmp_path / "site-packages"
+ site.mkdir()
+ _write_dist_metadata(site, "demo", "1.0")
+ monkeypatch.setattr(im, "_metadata_scan_paths", lambda: [str(site)])
+
+ assert im.installed_versions("demo") == ["1.0"]
+ unchanged = site.stat().st_mtime_ns
+ _write_dist_metadata(site, "demo", "2.0")
+ os.utime(site, ns = (unchanged, unchanged))
+
+ assert im.installed_versions("demo") == ["1.0", "2.0"]
+
+
+def test_the_metadata_scan_survives_a_finder_without_invalidate_caches(tmp_path, monkeypatch):
+ """3.9 has no invalidate_caches and no listing cache, so there is nothing to drop."""
+ import importlib.metadata
+
+ monkeypatch.delattr(importlib.metadata.MetadataPathFinder, "invalidate_caches", raising = False)
+
+ site = tmp_path / "site-packages"
+ site.mkdir()
+ _write_dist_metadata(site, "demo", "1.0")
+ monkeypatch.setattr(im, "_metadata_scan_paths", lambda: [str(site)])
+
+ assert im.installed_versions("demo") == ["1.0"]
+
+
+def test_a_healthy_install_still_verifies_after_repeated_checks(
+ tmp_path, monkeypatch, install_root, req_root
+):
+ """A fresh read every scan must not turn one healthy record into a conflict: the desktop
+ preflight treats studio_install_metadata_conflict as auto-repairable and reinstalls on launch.
+ """
+ site = tmp_path / "site-packages"
+ site.mkdir()
+ _write_dist_metadata(site, "demo", "1.0")
+ monkeypatch.setattr(im, "_metadata_scan_paths", lambda: [str(site)])
+ im.write_manifest(root = install_root, req_root = req_root, package_name = "demo")
+
+ for _ in range(3):
+ state = im.verify_install(root = install_root, req_root = req_root, package_name = "demo")
+ assert state["manifest_ok"] is True
+ assert im.installed_versions("demo") == ["1.0"]
+
+
def test_duplicate_package_metadata_invalidates_the_manifest(
tmp_path, monkeypatch, install_root, req_root
):