Repository navigation
Fix race in _get_version that makes concurrent downloads skip hf_xet - #5102
Conversation
Ayushdevo
left a comment
There was a problem hiding this comment.
AI-assisted community review at 59c9371, Python 3.12.14. With PYTHONPATH=src, python -m pytest tests/test_utils_runtime.py -q passes 3 tests.
I additionally used threading.Event to hold the metadata lookup before completion and inspected the cache while it was blocked: hf_xet is absent rather than temporarily cached as N/A. Releasing the lookup lets both concurrent callers receive 1.2.3. I also mocked PackageNotFoundError: two sequential calls return cached N/A, with only one metadata lookup. This verifies the installed-package race fix alongside unchanged negative-cache behavior. Concurrent duplicate metadata lookups remain possible, as the PR describes. I did not run real downloads or the full suite.
hanouticelina
left a comment
There was a problem hiding this comment.
Thanks a lot for catching and fixing this @BramVanroy!
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5102 +/- ##
==========================================
+ Coverage 75.00% 77.53% +2.52%
==========================================
Files 145 177 +32
Lines 13978 24831 +10853
==========================================
+ Hits 10484 19252 +8768
- Misses 3494 5579 +2085 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
|
This PR has been shipped as part of the v2.2.0 release. |
Fixes #5101.
_get_versionwrote the placeholder"N/A"into the shared_package_versionscache before it calledimportlib.metadata.version. A thread that calledis_xet_available()during that lookup read the placeholder and took the plain HTTP path, so concurrenthf_hub_downloadcalls in a fresh process could skip hf_xet for part of the files.The version is now resolved into a local variable and written to the cache once. Concurrent first calls may each do the lookup, but none of them can read a half-written entry. The cache and the return values are otherwise unchanged.
The new test in
tests/test_utils_runtime.pyreplacesimportlib.metadata.versionwith a slow stub and starts 8 threads behind a barrier. Onmainit fails in 10 of 10 runs, with threads that get"N/A". With this change it passes in 20 of 20 runs.Checks (Python 3.14, Linux):
pytest tests/test_utils_runtime.py,ruff check,ruff format --check.ty check srcreports the same 2 diagnostics incli/_output.pyas onmain, and none in_runtime.py.Note
Medium Risk
Changes thread-safety of package availability caching used by
is_xet_available()and download routing; fix is small but affects concurrent hub downloads.Overview
Fixes a race in
_get_versionwhere the shared_package_versionscache was updated with a"N/A"placeholder beforeimportlib.metadata.versionfinished. Concurrent first-time callers (e.g. viais_xet_available()during parallelhf_hub_download) could read that placeholder and treathf_xetas missing, skipping the Xet download path for some files in a fresh process (#5101).The lookup now resolves into a local variable and writes the cache once after resolution. Concurrent cold starts may still duplicate metadata lookups, but no thread observes a half-populated entry.
Adds
test_get_version_concurrent_first_lookup: eight threads hit_get_version("hf_xet")behind a barrier with a slow mockedimportlib.metadata.version, asserting every thread gets the real version instead of"N/A".Reviewed by Cursor Bugbot for commit c1d2ad1. Bugbot is set up for automated code reviews on this repo. Configure here.