From 59c9371dcbe62b0c1410fcdc8b4ac88ab2440833 Mon Sep 17 00:00:00 2001 From: Bram Vanroy <2779410+BramVanroy@users.noreply.github.com> Date: Wed, 7 Oct 2026 12:31:05 +0200 Subject: [PATCH 1/2] Fix race in _get_version that makes concurrent downloads skip hf_xet --- src/huggingface_hub/utils/_runtime.py | 7 ++++-- tests/test_utils_runtime.py | 33 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/huggingface_hub/utils/_runtime.py b/src/huggingface_hub/utils/_runtime.py index bf290282c2b..86e2db8eea0 100644 --- a/src/huggingface_hub/utils/_runtime.py +++ b/src/huggingface_hub/utils/_runtime.py @@ -64,13 +64,16 @@ def _get_version(package_name: str) -> str: if package_name not in _package_versions: - _package_versions[package_name] = "N/A" + # Resolve into a local variable and assign once: a placeholder written to the shared cache before + # the lookup finishes is read as "not installed" by concurrent callers (e.g. parallel downloads). + version = "N/A" for distribution_name in _CANDIDATES.get(package_name, (package_name,)): try: - _package_versions[package_name] = importlib.metadata.version(distribution_name) + version = importlib.metadata.version(distribution_name) break except importlib.metadata.PackageNotFoundError: pass + _package_versions[package_name] = version return _package_versions[package_name] diff --git a/tests/test_utils_runtime.py b/tests/test_utils_runtime.py index 9cefbcc164b..e462f9bbd0c 100644 --- a/tests/test_utils_runtime.py +++ b/tests/test_utils_runtime.py @@ -1,3 +1,11 @@ +import importlib.metadata +import threading +import time +from concurrent.futures import ThreadPoolExecutor + +import pytest + +from huggingface_hub.utils import _runtime from huggingface_hub.utils._runtime import is_google_colab, is_notebook @@ -9,3 +17,28 @@ def test_is_notebook(self) -> None: def test_is_google_colab(self) -> None: """Test `is_google_colab`.""" assert not is_google_colab() + + +def test_get_version_concurrent_first_lookup(monkeypatch: pytest.MonkeyPatch) -> None: + """Concurrent first lookups must all see the installed version, not a "N/A" placeholder. + + Regression test for https://github.com/huggingface/huggingface_hub/issues/5101. + """ + num_threads = 8 + monkeypatch.setattr(_runtime, "_package_versions", {}) + + def slow_version(distribution_name: str) -> str: + time.sleep(0.2) + return "1.2.3" + + monkeypatch.setattr(importlib.metadata, "version", slow_version) + barrier = threading.Barrier(num_threads) + + def get_version(_: int) -> str: + barrier.wait() + return _runtime._get_version("hf_xet") + + with ThreadPoolExecutor(num_threads) as pool: + versions = list(pool.map(get_version, range(num_threads))) + + assert versions == ["1.2.3"] * num_threads From c1d2ad11861a98186f71a9325f3342f9e18d12b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?c=C3=A9lina?= Date: Wed, 7 Oct 2026 14:29:30 +0200 Subject: [PATCH 2/2] Update src/huggingface_hub/utils/_runtime.py --- src/huggingface_hub/utils/_runtime.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/huggingface_hub/utils/_runtime.py b/src/huggingface_hub/utils/_runtime.py index 86e2db8eea0..e2735be1ce9 100644 --- a/src/huggingface_hub/utils/_runtime.py +++ b/src/huggingface_hub/utils/_runtime.py @@ -64,8 +64,7 @@ def _get_version(package_name: str) -> str: if package_name not in _package_versions: - # Resolve into a local variable and assign once: a placeholder written to the shared cache before - # the lookup finishes is read as "not installed" by concurrent callers (e.g. parallel downloads). + # Write once after the lookup: concurrent callers would read a placeholder as "not installed". version = "N/A" for distribution_name in _CANDIDATES.get(package_name, (package_name,)): try: