feat: cache and reuse XetSession in HFClient#21
Merged
assafvayner merged 24 commits intomainfrom Apr 10, 2026
Merged
Conversation
Store a lazily-initialized XetSession on HFClientInner behind #[cfg(feature = "xet")]. The xet_session() method creates the session on first use, probes health on subsequent calls, and transparently replaces it if the session is permanently poisoned (e.g. after abort). Call sites use inline single-retry for the narrow race between the health check and the factory call. This avoids creating a new tokio thread pool for every xet operation.
Replace zero-filled 11MB buffer with random bytes so the test exercises real xet deduplication rather than trivially compressible data.
Verify that xet_session() self-heals after both abort() and sigint_abort() by detecting the poisoned session and replacing it with a fresh one.
- Expand is_session_poisoned coverage to all XetError variants - Add inline retry pattern simulation test - Add xet error message wrapping test - Add CLI signal tests: SIGINT during xet upload and download
hub-ci does not support xet transfers. Guard write_upload_large_file and both signal abort tests with is_hub_ci() early return.
Xet transfers work on hub-ci. The guards were added in error.
…clients Move all *Params structs from repository.rs into a new types/repo_params.rs module for better organization. Rework integration test client setup for CI: - prod_api() uses HF_PROD_TOKEN against huggingface.co for read-only tests - api() uses HF_CI_TOKEN against hub-ci.huggingface.co for write tests - Local dev uses HF_TOKEN with default/env endpoint via default_api() - All client creation goes through build_client() with explicit token+endpoint
…clients - Replace huggingface-hub-rust-test-user with __DUMMY_TRANSFORMERS_USER__ across all test files (blocking, cache, cli, download) - Split CI token handling: HF_CI_TOKEN for hub-ci, HF_PROD_TOKEN for prod repos (cache xet tests, integration tests) - Remove HF_TOKEN from CI yaml; only HF_CI_TOKEN and HF_PROD_TOKEN - Add prod_api() and prod_api_with_cache() to cache_test for xet tests that reference hardcoded prod repos (mcpotato/42-xet-test-repo) - Update CliRunner to fall back to HF_CI_TOKEN when HF_TOKEN is unset
Remove the per-call health probe from xet_session() — it was creating a throwaway download group builder on every access. Instead: - xet_session() just returns the cached session or creates one - replace_xet_session() drops the cached session so the next xet_session() call builds a fresh one - Call sites: on factory error, call replace_xet_session then retry This is cheaper since the probe only runs when an actual error occurs, not on every session access.
xet_session() now returns (XetSession, generation). Call sites pass the generation to replace_xet_session(), which only clears the cached session if the generation matches. This prevents a race where T2 discards T1's fresh session: T2's stale generation won't match the new one, so the replace is a no-op.
…clients All test files now use the correct endpoint and token: - Read-only tests (hardcoded repos): always target production (HF_PROD_TOKEN + huggingface.co in CI, HF_TOKEN locally) - Write tests (create/delete repos): target hub-ci (HF_CI_TOKEN in CI, HF_TOKEN locally) Removed all is_hub_ci() branching and conditional repo names. Repo name functions now return fixed prod values since read-only tests always hit production. Files changed: - blocking_test: prod_sync_api() for reads, ci_sync_api() for writes - cache_test: api() always targets prod, removed prod_api indirection - download_test: api() always targets prod - cli/helpers: hfrs() targets prod, hfrs_ci() targets hub-ci - cli/cli_comparison: write tests use hfrs_ci(), reads use hfrs()
All CI jobs now use Swatinem/rust-cache@v2 for consistent caching.
Replace Swatinem/rust-cache with actions/cache pinned to a specific commit hash. Caches ~/.cargo/registry, ~/.cargo/git, and target/ keyed on Cargo.lock hash.
- actions/checkout pinned to de0fac2e (v6.0.2) - dtolnay/rust-toolchain pinned to 3c5f7ea2 (master) - actions/cache already pinned to 66822842 - Added explicit toolchain: nightly/stable since the hash ref no longer encodes the toolchain name
Move test environment variable names and endpoint URLs to huggingface_hub::test_utils, eliminating string literal duplication across test files. Constants: HF_TOKEN, HF_CI_TOKEN, HF_PROD_TOKEN, HF_TEST_WRITE, HF_ENDPOINT, GITHUB_ACTIONS, HF_HUB_CACHE, HF_HOME, XDG_CACHE_HOME, PROD_ENDPOINT, HUB_CI_ENDPOINT. Helpers: is_ci(), write_enabled(), resolve_ci_token(), resolve_prod_token().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Cache and reuse
XetSessioninHFClientinstead of creating a new one per xet operation. The session is lazily initialized on first use and shared across all xet operations viaArc<HFClientInner>.Generation-based replacement:
xet_session()returns(session, generation). When a factory call fails, call sites invokereplace_xet_session(generation, &err)which only clears the cached session if the generation matches — preventing concurrent threads from discarding each other's fresh sessions.Call-site retry pattern: Each xet call site (5 total: download to local dir, download to blob, batch download, stream download, upload) retries once with a fresh session on factory errors.
Split CI test infrastructure: Read-only tests (hardcoded repos) always target production (
HF_PROD_TOKEN). Write tests target hub-ci (HF_CI_TOKEN). Removed allis_hub_ci()conditional repo name branching.Signal abort tests: CLI tests that send SIGINT during xet upload/download and verify the process terminates cleanly.
Xet error handling tests: Comprehensive
is_session_poisonedclassification, inline retry pattern simulation, error message preservation throughHFError::Otherwrapping.Test plan
cargo test -p huggingface-hub --all-features(78 unit tests pass)HF_TEST_WRITE=1 cargo test --test blocking_test(4 pass)HF_TEST_WRITE=1 cargo test --test cli_comparison -- write_(22 pass)HF_TEST_WRITE=1 cargo test --test cli_comparison -- signal_abort(2 pass)HF_TEST_WRITE=1 cargo test --test integration_test(37 pass)HF_TEST_WRITE=1 cargo test --test xet_transfer_test(10 pass)cargo test --test cache_test(47 pass)cargo test --test download_test(13 pass)HF_CI_TOKENandHF_PROD_TOKENsecrets configured in GitHub