Skip to content

feat: cache and reuse XetSession in HFClient#21

Merged
assafvayner merged 24 commits intomainfrom
assaf/next
Apr 10, 2026
Merged

feat: cache and reuse XetSession in HFClient#21
assafvayner merged 24 commits intomainfrom
assaf/next

Conversation

@assafvayner
Copy link
Copy Markdown
Collaborator

@assafvayner assafvayner commented Apr 9, 2026

Summary

  • Cache and reuse XetSession in HFClient instead of creating a new one per xet operation. The session is lazily initialized on first use and shared across all xet operations via Arc<HFClientInner>.

  • Generation-based replacement: xet_session() returns (session, generation). When a factory call fails, call sites invoke replace_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 all is_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_poisoned classification, inline retry pattern simulation, error message preservation through HFError::Other wrapping.

Test plan

  • Unit tests: cargo test -p huggingface-hub --all-features (78 unit tests pass)
  • Blocking write tests: HF_TEST_WRITE=1 cargo test --test blocking_test (4 pass)
  • CLI write tests: HF_TEST_WRITE=1 cargo test --test cli_comparison -- write_ (22 pass)
  • CLI signal tests: HF_TEST_WRITE=1 cargo test --test cli_comparison -- signal_abort (2 pass)
  • Integration tests: HF_TEST_WRITE=1 cargo test --test integration_test (37 pass)
  • Xet transfer tests: HF_TEST_WRITE=1 cargo test --test xet_transfer_test (10 pass)
  • Cache tests: cargo test --test cache_test (47 pass)
  • Download tests: cargo test --test download_test (13 pass)
  • CI: requires HF_CI_TOKEN and HF_PROD_TOKEN secrets configured in GitHub

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.
@assafvayner assafvayner requested a review from seanses April 9, 2026 07:19
Replace zero-filled 11MB buffer with random bytes so the test
exercises real xet deduplication rather than trivially compressible
data.
@assafvayner assafvayner marked this pull request as ready for review April 9, 2026 07:27
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().
@assafvayner assafvayner merged commit 6084c0f into main Apr 10, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant