Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix race in _get_version that makes concurrent downloads skip hf_xet
  • Loading branch information
BramVanroy committed Oct 7, 2026
commit 59c9371dcbe62b0c1410fcdc8b4ac88ab2440833
7 changes: 5 additions & 2 deletions src/huggingface_hub/utils/_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Comment thread
hanouticelina marked this conversation as resolved.
Resolved
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]


Expand Down
33 changes: 33 additions & 0 deletions tests/test_utils_runtime.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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