bugfix(client/rust): isolate MAT_VIS_HF_BASE per-test (#241)#254
Merged
bugfix(client/rust): isolate MAT_VIS_HF_BASE per-test (#241)#254
Conversation
…e state leak (#241) The Rust client suite mixes httpmock-based unit tests (which set MAT_VIS_HF_BASE to a 127.0.0.1 mock server) with @LiVe tests that need to hit real HF. Even with `cargo test -- --test-threads=1` the env var leaked across tests in the same process — live tests then 404'd on http://127.0.0.1:<port>/v2026.04.2/release-manifest.json. Fix: an in-tree `EnvGuard` RAII helper (Option C — no new dev dep). Each mock test stores the prior value of MAT_VIS_HF_BASE, sets the mock URL, and restores on drop. Live tests then see the original (unset) state regardless of test ordering. `ENV_LOCK` stays so the suite is race-safe under default (parallel) `cargo test` — `std::env::set_var` is `unsafe` in 2024 edition. The existing hand-rolled restore in `e2e_fetch_color_png` and `resolved_tag_falls_back_to_default` is replaced by `EnvGuard` / panic-safe restoration. Verified: `MAT_VIS_LIVE_TESTS=1 MAT_VIS_LIVE_TAG=v2026.04.2 cargo test -- --test-threads=1` passes all 21 tests (4 live) cleanly in a single process, no SIGABRT, no 127.0.0.1 leakage into live URLs. Also rolls back the #253 dagger-side workaround (`test_client_rust` dual cargo invocation): with proper test-side isolation, the single-invocation form is correct again.
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
Closes #241.
The Rust client suite mixed
httpmock-based unit tests (which setMAT_VIS_HF_BASEto a127.0.0.1mock server) with@livetests that need to hit real HF. Even withcargo test -- --test-threads=1the env var leaked across tests in the same process, so live tests 404'd onhttp://127.0.0.1:<port>/v2026.04.2/release-manifest.jsoninstead of huggingface.co — the underlying bug behind #241 (and the SIGABRT symptom that originally got reported).Choice: Option C — in-tree
EnvGuardRAII helperConsidered three options from the issue:
serial_test: doesn't restore env, just serializes execution; new dev dep.scopeguard::defer!: correct, but requires a new dev dep.EnvGuard: ~20 LOC, zero new deps, typed handle that's hard to forget. Picked C.EnvGuard::set("MAT_VIS_HF_BASE", url)stores the prior value, sets the mock URL, andDroprestores (or removes if previously unset). The existingENV_LOCK: Mutex<()>is kept so the suite stays race-safe under the default parallelcargo testrunner —std::env::set_varisunsafein the 2024 edition for that reason. Drop order: each test declares_lockbefore_env, so reverse-decl order drops_env(env restoration) while_lockis still held.The hand-rolled env restore in
e2e_fetch_color_pngandresolved_tag_falls_back_to_defaultis also folded into the same helper / lock pattern — thee2etest now becomes panic-safe (was previously best-effort with manualunsafeblocks).#253 rollback
Rolled back the
.dagger/src/mat_vis_ci/main.py::test_client_rustdual-invocation workaround from #253. With proper test-side isolation, the single-invocation form is correct again — and avoids the secondapt-get install+cargo testboot.Before / after
Before (against
bugfix/241-rust-live-isolationhead, single cargo invocation): live tests after mock tests fail with 404 onhttp://127.0.0.1:<port>/v2026.04.2/release-manifest.json— env var leaked.After (this branch, single cargo invocation):
All 21 tests (including 4
live_*) pass cleanly in a single process — no SIGABRT, no 127.0.0.1 leakage in live test output.Test plan
cargo build --testscleancargo test -- --test-threads=1(no live) — 21 passedMAT_VIS_LIVE_TESTS=1 MAT_VIS_LIVE_TAG=v2026.04.2 cargo test -- --test-threads=1— 21 passed including 4 live teststest_client_rust