Skip to content

fix(test): serialize mlxcel-server env tests and stop lock-poison cascades - #2946

Merged
inureyes merged 3 commits into
mainfrom
fix/issue-2937-test-gate-isolation
Nov 10, 2026
Merged

inureyes merged 3 commits into
mainfrom
fix/issue-2937-test-gate-isolation

Conversation

@inureyes

@inureyes inureyes commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

The mlxcel-server argument tests raced whenever that binary ran threaded. clap 4 reads each env = "..." binding when it builds the command (Arg::env calls env::var_os), so every test that built or parsed Cli, or called build_startup_input, read LLAMA_ARG_* and MLXCEL_* values that a sibling had set under CLI_ENV_LOCK. The four tests named in the issue were just the ones whose assertions noticed; the audit found 40 unlocked tests that read env this way, including the help and flag-introspection tests and the parent of the CORS child-process test, whose child inherits the parent's env at spawn time.

Changes

  • src/bin/mlx_server.rs tests: one poison-tolerant cli_env_lock() guard replaces the 12 hand-rolled lock sites (7 of them used expect and would cascade on poison), and every test that builds, parses, or spawns the child takes it. parse_server_args, try_parse_cli, cli_command, and the ScopedEnv writers assert the guard is held, so a new test that forgets fails with a message naming the fix instead of racing. A thread-local flag makes a second acquisition on one thread panic instead of deadlocking.
  • src/models/popular_dense_batched_decode_tests.rs: test_guard() is taken with unwrap_or_else(PoisonError::into_inner), so one failing case no longer turns its siblings into PoisonError failures.
  • CONTRIBUTING.md: the macOS test command is now the serial make verify-test gate, with one sentence on why ([nightly-verify] main is red #1092, fix(ci): run the Metal test gate single threaded, like the CUDA one #1210).

CLAUDE.md is not in this PR: it is a gitignored local symlink to an untracked AGENTS.md, so its "Build and test" line has to be fixed outside the tree.

Validation

  • Before, on the untouched tree: 5 of 6 threaded runs of cargo test --profile test-fast --features metal,accelerate --bin mlxcel-server failed (image_model_without_model_* 3 times, legacy_server_mode_* and no_prompt_cache_flag_* 1 to 2 times each).
  • After: 10 of 10 threaded runs of the whole binary pass (51 passed, 1 ignored), and 5 of 5 runs of the issue's four-test filter pass.
  • Deleting the guard from one test makes it fail with the assertion message.
  • --lib popular_dense_batched_decode passes threaded (15 passed); cargo clippy --lib --tests --bin mlxcel-server --features metal,accelerate -- -D warnings and cargo fmt --check are clean.
  • The race also left a stray zero-byte legacy.log in the checkout: a test built a logging config while a sibling held LLAMA_LOG_FILE=legacy.log. After the fix, 5 more threaded runs created none.
  • No tolerance or snapshot changed. The full make verify-test run is still pending.

Closes #2937

…cades

The mlxcel-server argument tests raced whenever the binary ran threaded: clap reads every `env = "..."` binding when it builds the command, so any test that built or parsed `Cli` (or called `build_startup_input`) saw `LLAMA_ARG_*` and `MLXCEL_*` values that a sibling had set under `CLI_ENV_LOCK`. On an untouched tree 5 of 6 threaded runs of `--bin mlxcel-server` failed one to three of the four tests named in the issue.

Every test in that module that builds, parses, or spawns the CORS child now holds the lock through a poison-tolerant `cli_env_lock()` guard, and `parse_server_args`, `try_parse_cli`, and `cli_command` assert it is held, so a future test that forgets fails with a message instead of racing. The dense batched decode test lock now recovers from poison, so one failing case no longer turns every later case into a `PoisonError`. CONTRIBUTING.md documents the serial `make verify-test` gate and why it is serial (#1092, #1210).

Validated: 10 threaded runs of the whole `--bin mlxcel-server` set and 5 of the four-test filter all pass; removing one guard makes that test fail with the assertion; `popular_dense_batched_decode` passes threaded; clippy and fmt are clean. No tolerance or snapshot changed.

Closes #2937
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:models Model architectures, weights, loading, metadata area:cli Command-line interface / CLI flags status:review Under review labels Nov 10, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Serialize every env-reading mlxcel-server test under one poison-tolerant lock, make the dense batched decode test lock poison-tolerant, and document the serial make verify-test gate.

Findings Addressed

  • None needed fixing (no CRITICAL or HIGH findings).

Remaining Items

  • ScopedEnv::set / ScopedEnv::unset and apply_cors_origin_env do not call assert_cli_env_lock_held(). Every current caller holds the lock, but the env writer is the half that causes the race, so a future test could mutate env unguarded without tripping the check. (LOW)
  • CONTRIBUTING.md steps 3 and 4 now carry the identical serial test command; step 3 calls it "the gate" while step 4 says "NOT gated at PR time". Consider wording step 3 as "the CI-faithful command" to avoid the apparent contradiction. (LOW)
  • make verify-test (an acceptance criterion) has not been run yet; the PR body already marks it pending.
  • The CLAUDE.md criterion is met outside the tree by design (gitignored symlink), so it is not verifiable from this PR.

Verification

  • All stated requirements implemented (CLAUDE.md handled out of tree)
  • No placeholder/mock code remaining
  • Integrated into project code flow (all 52 tests in the module take cli_env_lock(), 51 run plus 1 ignored child)
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass: --bin mlxcel-server 6 of 6 threaded runs and 1 serial run pass, --lib popular_dense_batched_decode passes threaded, fmt and the narrow clippy slice are clean; make verify-test and workspace clippy still pending

Guard review: poison is tolerated through PoisonError::into_inner; locals drop in reverse order, so a ScopedEnv bound after _lock restores env before the mutex is released; CliEnvGuard::drop clears the thread-local flag before its field releases the mutex, and the flag is per-thread, so the reentrancy check cannot misfire across threads. The child-process test holds the lock in the parent during spawn, and the child takes its own lock in a separate process, so no deadlock.

Review follow-up for #2937. `ScopedEnv::set` and `ScopedEnv::unset` now assert the env lock is held, so the writer half of the race is checked as well as the readers. The CONTRIBUTING.md note no longer calls the serial command "the gate" next to step 4's "NOT gated at PR time"; it says the command is what `make verify-test` and the nightly workflow run.

Validated with 5 threaded runs of `--bin mlxcel-server` (51 passed each), clippy on the bin's tests, and fmt.
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 10, 2026
Adds the bilingual (EN and KO) technical report for the mlxcel-server env test serialization and lock-poison fixes (#2937). It records why the threaded command reported failures the serial `make verify-test` gate never sees (#1092, #1210), the single poison-tolerant `cli_env_lock()` guard and its assertions, the before and after threaded run counts, and the open items: the local `CLAUDE.md` line outside the tree, the MLX-backed parallel-only failures left out of scope, and the pending full gate run.
@inureyes
inureyes marked this pull request as ready for review November 10, 2026 14:47
@inureyes
inureyes merged commit b7faec9 into main Nov 10, 2026
30 of 31 checks passed
@inureyes
inureyes deleted the fix/issue-2937-test-gate-isolation branch November 10, 2026 14:48
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:cli Command-line interface / CLI flags area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Align the documented test gate and isolate parallel-only test failures

1 participant