Repository navigation
test: acquire the env lock in backend seam tests for xla-backend builds - #1169
Conversation
`select_backend()` reads `MLXCEL_BACKEND` when built with the opt-in `xla-backend` or `experimental-backend` features, but `mlx_session_threads_the_token_bias_through` and three sibling tests in `src/backend/tests.rs` called it without holding the crate-wide env lock from PR #1157. Under those features, a parallel full-suite run can interleave with `muse_glimmer_startup_rejects_xla_backend_selection`, which transiently sets `MLXCEL_BACKEND=xla` while holding that lock, and make a backend test observe `xla` instead of the default MLX selection. All four `select_backend()` call sites in the file now acquire `crate::test_support::env_lock::env_lock()` before calling it, bound to a named `_env_guard` variable so the guard's `Drop` lasts the whole test body: `select_backend_resolves_to_mlx_under_default_features` and `mlx_session_threads_the_token_bias_through` would otherwise fail their MLX-only assertions (or hit the `unreachable!` arm) under the race; `seam_delegates_to_real_mlx_loader_on_missing_dir` and `mlx_backend_creates_a_session_and_advertises_batched_serving` would not fail outright but would silently validate the wrong backend's behavior, defeating what their docstrings claim to test. The lock is a no-op under default features (`select_backend` const-folds with no environment read there), so this is test-only and changes no behavior under the feature sets CI actually builds. Verified with `cargo check --lib --tests`, `cargo test --lib backend::tests` (5 passed), and `cargo clippy --lib --tests -- -D warnings`, all under default features on this machine; the `xla-backend` feature itself was not built locally (cold `mlxcel-xla` build), so the race fix is verified by code inspection against the #1157 pattern plus this targeted default-feature run, per the issue's accepted evidence.
Orchestrator verification:
|
Implementation Review SummaryIntent
Findings AddressedNone. No CRITICAL, HIGH, or MEDIUM findings were found, and no fixes were required. Verification performed in this reviewThe one correctness trap (bare Placement is correct in all four. Each guard is acquired before its No call site was missed. Guarding all four rather than only the issue's named test is justified and the reasoning checks out. The mutating side is fully covered. No deadlock or self-lock risk. Compiles under the feature set the fix targets. Genuinely test-only. One file changed, 26 insertions, 0 deletions, all inside Conventions. The guard uses the exact Remaining ItemsTwo observations, neither one a defect and neither requiring a change:
Verification
The |
Security and performance reviewNo CRITICAL, HIGH, MEDIUM, or LOW findings. Nothing changed; the PR is unmodified. Reviewed the five dimensions a lock change can actually affect: Deadlock / lock ordering. No cycle is reachable. The four guarded tests take exactly one Rust-level lock ( Lock-hold duration. Measured on the prebuilt Guard lifetime. All four are Masking. The guards serialize; they do not weaken any assertion. Every existing One point in the PR's favor that the description understates: Optional, non-blocking, and explicitly out of scope for this PR: |
Bilingual report covering the backend seam env-lock change: the race mechanism under the opt-in xla-backend and experimental-backend features, why all four select_backend() call sites were guarded rather than only the one named in the issue, and the verification runs including the --features xla-backend contention run that the issue's second acceptance criterion asks for.
…ds (lablup#1169) ## Summary Closes a pre-existing test-only race in `src/backend/tests.rs`: under the opt-in `xla-backend` / `experimental-backend` features, `select_backend()` reads `MLXCEL_BACKEND`, and a parallel full-suite run could interleave with `muse_glimmer_startup_rejects_xla_backend_selection` (which transiently sets that variable while holding the crate-wide env lock added in lablup#1157) and make a backend test observe the transient `xla` value instead of the default MLX selection. ## What changed - `src/backend/tests.rs`: all four `select_backend()` call sites now acquire `crate::test_support::env_lock::env_lock()`, bound to a named `_env_guard` variable (never a bare `_`, since dropping it immediately would reinstate the exact race this fix closes), before calling `select_backend()`. - `mlx_session_threads_the_token_bias_through` (the test named in the issue) is guarded: without the lock it would hit the `unreachable!("select_backend defaults to MLX without MLXCEL_BACKEND=xla")` arm under the race. - `select_backend_resolves_to_mlx_under_default_features` is guarded: without the lock its `matches!(backend, Backend::Mlx(_))` and `backend.name() == "mlx"` assertions would fail under the race. - `mlx_backend_creates_a_session_and_advertises_batched_serving` is guarded: without the lock, `backend.supports_batched_serving()` and the session's `caps.multimodal` assertion would fail under the race (the XLA scaffold session reports `multimodal: false`, the opposite of what this test asserts for MLX). - `seam_delegates_to_real_mlx_loader_on_missing_dir` is guarded too, even though it would not fail outright under the race (both the MLX loader and the XLA scaffold's `load_unsupported()` return `Err` for a missing path): its docstring claims to prove the seam reaches the real MLX loader specifically, and without the lock a race could silently validate the XLA scaffold instead, defeating that claim without a visible test failure. All three of these sites were evaluated per the issue's discussion, and none were left unguarded. - Each guarded test carries a short comment explaining why the lock is needed, since under default features `select_backend()` reads no environment variable and the lock can otherwise look superfluous to a future reader. - Test-only change. `src/backend/mod.rs` (the feature-gated `select_backend` arm that reads `MLXCEL_BACKEND`) and `src/server/muse_glimmer_startup_guard_tests.rs` (already fixed by lablup#1157) are untouched, and no production code path changed. ## Test plan - [x] `cargo check --lib --tests` (default features) - [x] `cargo test --lib backend::tests` (default features): 5 passed, 0 failed - [x] `cargo clippy --lib --tests -- -D warnings` (default features): clean - [ ] Full-suite parallel run with `--features metal,accelerate,xla-backend`: not run. This machine is a CUDA/GB10 Linux box where `metal` is not applicable, no CI workflow builds `xla-backend`, and a cold `mlxcel-xla` build exceeds local verification time budgets. Per the issue's accepted evidence, this PR relies on code inspection (the guard follows the exact `let _env_guard = crate::test_support::env_lock::env_lock();` pattern established by lablup#1157 in `muse_glimmer_startup_guard_tests.rs`) plus the targeted default-feature run above. Closes lablup#1159
Summary
Closes a pre-existing test-only race in
src/backend/tests.rs: under the opt-inxla-backend/experimental-backendfeatures,select_backend()readsMLXCEL_BACKEND, and a parallel full-suite run could interleave withmuse_glimmer_startup_rejects_xla_backend_selection(which transiently sets that variable while holding the crate-wide env lock added in #1157) and make a backend test observe the transientxlavalue instead of the default MLX selection.What changed
src/backend/tests.rs: all fourselect_backend()call sites now acquirecrate::test_support::env_lock::env_lock(), bound to a named_env_guardvariable (never a bare_, since dropping it immediately would reinstate the exact race this fix closes), before callingselect_backend().mlx_session_threads_the_token_bias_through(the test named in the issue) is guarded: without the lock it would hit theunreachable!("select_backend defaults to MLX without MLXCEL_BACKEND=xla")arm under the race.select_backend_resolves_to_mlx_under_default_featuresis guarded: without the lock itsmatches!(backend, Backend::Mlx(_))andbackend.name() == "mlx"assertions would fail under the race.mlx_backend_creates_a_session_and_advertises_batched_servingis guarded: without the lock,backend.supports_batched_serving()and the session'scaps.multimodalassertion would fail under the race (the XLA scaffold session reportsmultimodal: false, the opposite of what this test asserts for MLX).seam_delegates_to_real_mlx_loader_on_missing_diris guarded too, even though it would not fail outright under the race (both the MLX loader and the XLA scaffold'sload_unsupported()returnErrfor a missing path): its docstring claims to prove the seam reaches the real MLX loader specifically, and without the lock a race could silently validate the XLA scaffold instead, defeating that claim without a visible test failure. All three of these sites were evaluated per the issue's discussion, and none were left unguarded.select_backend()reads no environment variable and the lock can otherwise look superfluous to a future reader.src/backend/mod.rs(the feature-gatedselect_backendarm that readsMLXCEL_BACKEND) andsrc/server/muse_glimmer_startup_guard_tests.rs(already fixed by test: skip pinned muse_glimmer checkpoint tests and fix MLXCEL_BACKEND race #1157) are untouched, and no production code path changed.Test plan
cargo check --lib --tests(default features)cargo test --lib backend::tests(default features): 5 passed, 0 failedcargo clippy --lib --tests -- -D warnings(default features): clean--features metal,accelerate,xla-backend: not run. This machine is a CUDA/GB10 Linux box wheremetalis not applicable, no CI workflow buildsxla-backend, and a coldmlxcel-xlabuild exceeds local verification time budgets. Per the issue's accepted evidence, this PR relies on code inspection (the guard follows the exactlet _env_guard = crate::test_support::env_lock::env_lock();pattern established by test: skip pinned muse_glimmer checkpoint tests and fix MLXCEL_BACKEND race #1157 inmuse_glimmer_startup_guard_tests.rs) plus the targeted default-feature run above.Closes #1159