Repository navigation
fix(xtask): serialise subprocess-spawning tests against cwd-manipulating tests (#2044) - #2051
Conversation
…ing tests (#2044) The xtask test suite had a parallel-execution race: main.rs tests (`with_temp_cwd`, `with_repo_cwd`) call `std::env::set_current_dir` and delete the temporary directory on teardown. Meanwhile run.rs tests spawn rustc/cargo/sh which inherit the process cwd. A subprocess spawn could land in the tiny window between `set_current_dir(temp)` and `remove_dir_all(temp)`, producing intermittent failures: run_output_owned_includes_stderr_on_failure: rustc --version failed ... 'Unable to proceed. Could not locate working directory.' run_output_reports_stdout_and_failure: rustc --version failed with exit status: 1 capture_output_returns_status_stdout_and_stderr: rustc --version should succeed The same tests passed in isolation and on full-suite re-runs. Denominator evidence on current main: run 1 = 3 failed/1268 passed, run 2 = 0 failed. ## Fix Hoist the CWD_LOCK static from main.rs's tests module to a crate-root `pub(crate) static` plus a `pub(crate) fn acquire_test_cwd_lock()` helper. The run.rs test module imports the helper and acquires the guard at the top of every subprocess-spawning test body. Now both modules share the same process-wide mutex and cannot race: - main.rs's with_temp_cwd/with_repo_cwd hold the lock while they mutate cwd and tear down temp dirs - run.rs's spawning tests (run_output*, capture_output*, capture_stdout_to_file_with_timeout, terminate_after_timeout, timeout_was_enforced, etc.) hold the same lock while subprocesses inherit cwd Removed the stale `panic-0003` allowlist entry from policy/no-panic-allowlist.toml: it matched the old `CWD_LOCK.get_or_init(...).lock().unwrap()` call which is now replaced by acquire_test_cwd_lock()'s poison-recovering `unwrap_or_else(|poison| poison.into_inner())` (no panic-family call remains). ## Verified cargo test -p xtask PASS (1268 tests, no flakes across multiple parallel runs) cargo clippy --workspace --all-targets -D warnings PASS cargo xtask check-no-panic-family PASS (0 stale, 0 unallowed) cargo xtask check-file-policy PASS cargo xtask check-static-language PASS Closes #2044.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Warning Review limit reached
Next review available in: 1 minute Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Code Review
This pull request removes a panic allowlist entry from policy/no-panic-allowlist.toml and updates various test cases in xtask/src/run.rs to acquire a shared current working directory (CWD) lock (acquire_test_cwd_lock()). This prevents concurrent tests that manipulate the CWD from racing with subprocess spawns. There are no review comments, so I have no additional feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Decision
- Needs reviewer attention before upstream: findings remain.
Confirmed findings
- Architecture boundary is clean and minimal; lock acquisition is duplicated 13 times in run.rs but this is the smallest correct fix (no shared test util module is warranted for one helper call). A future refactor could extract a spawn_test_cwd helper but it would not reduce the actual lock-acquire cost and risks obscuring the invariant.
Verification questions
- Confirm pR body says 13 spawning tests acquired; diff shows guard added to 13 test bodies in run.rs (counted in patch).
- Confirm no new behavior assertion added: lock acquisition is side-effect-free, so no test distinguishes old vs new behavior. Green build only proves races are rarer, not closed.
- Confirm cWD_LOCK and acquire_test_cwd_lock are now crate-root #[cfg(test)] items, making the guard process-wide and shared across main.rs/run.rs test binaries (each binary is its own process so this remains correct).
- Confirm with_temp_cwd/with_repo_cwd both now route through acquire_test_cwd_lock, so any future test calling these helpers inherits serialisation without code change.
- Confirm load-bearing evidence (cargo-test green across multiple parallel runs; intermittent-failure denominator) is not in any sensor receipt: artifact-verifier failed and cargo-test is scheduled-late. Race-closure is an empirical claim; gate should not approve without that receipt.
- Confirm cargo-test receipt is scheduled-late; race-fix verification depends on it landing green before the gate. Heavy witnesses (build/clippy/fmt/miri) are skipped, so parallelism regressions or new unwraps in test code are not independently proven by the available sensors.
Refuted
- The PR's race-closure proof is incomplete because no test in run.rs references the cwd guard's effect.; refuted because: TestsRedGreen scope is red/green discrimination; the guard is a synchronization primitive with no observable return value, so a discriminating assertion is structurally absent. PR evidence relies on empirical multi-run cargo test, not a deterministic oracle. Treat as test-gap, not test-absent.
- A narrow test-only Mutex should live inside the test module, not crate-root, to minimise surface area; refuted because: Crate-root placement is required because main.rs tests module and run.rs tests module are separate modules and need to share the same Mutex<()>. Inline per-module locks would re-introduce the race. cfg(test) keeps it out of production.
- Lock is not actually shared because OnceLock is per-module; refuted because: Diff hoists CWD_LOCK to crate-root with #[cfg(test)] pub(crate) static; run.rs imports the helper and resolves through crate path, so both modules see the same OnceLock instance.
Parked follow-ups
- Process-wide scope is justified: cwd is process-wide and lock only gates ~12 spawn tests + cwd-mutators while leaving the remaining 1256 tests parallel.
Evidence gaps
[review body truncated; see review artifacts]
Problem
The xtask test suite had a parallel-execution race (#2044). Two test families run concurrently under
cargo test -p xtask:with_temp_cwd/with_repo_cwdcallstd::env::set_current_dir(temp)andremove_dir_all(temp)on teardownrun_output*,capture_output*,capture_stdout_to_file_with_timeout,terminate_after_timeout,timeout_was_enforcedspawnrustc/cargo/shwhich inherit the process cwdA subprocess spawn could land in the tiny window between
set_current_dir(temp)andremove_dir_all(temp), producing intermittent failures like:The same tests passed in isolation and on full-suite re-runs. Denominator evidence on current main: run 1 = 3 failed/1268 passed, run 2 = 0 failed.
Change
CWD_LOCKfrom main.rs's tests module to a crate-rootpub(crate) staticplus apub(crate) fn acquire_test_cwd_lock() -> MutexGuard<'static, ()>helper. The helper usesunwrap_or_else(|poison| poison.into_inner())to recover from a poisoned mutex (no panic-family call).with_temp_cwdandwith_repo_cwdnow callacquire_test_cwd_lock()instead of the inlineCWD_LOCK.get_or_init(...).lock().unwrap().acquire_test_cwd_lockand acquires the guard at the top of every subprocess-spawning test body.Both modules now share the same process-wide mutex and cannot race.
panic-0003allowlist entry frompolicy/no-panic-allowlist.toml: it matched the oldCWD_LOCK.get_or_init(...).lock().unwrap()call which no longer exists. The no-panic gate fails on stale entries, so this removal is required for the gate to stay green.Why a process-wide lock instead of
--test-threads=1?--test-threads=1would mask the race but slow every other test (the suite is 1268 tests). The lock serialises only the cwd-sensitive subset — about 12 spawning tests plus the cwd-mutating tests — while everything else still runs in parallel. The lock is process-wide because cwd is process-wide; there is no narrower scope.Evidence
Scope
Three-file change:
xtask/src/main.rs(hoist lock + helper + update two callers),xtask/src/run.rs(import helper + acquire in 13 spawning tests),policy/no-panic-allowlist.toml(remove one stale entry). No production code change. Closes #2044.Non-goals
run_output/capture_outputhelpers to take an explicitcurrent_dir. That would be a wider refactor and is not needed — the lock fully closes the race for the test surface.--test-threads. The lock is sufficient and keeps parallelism for unrelated tests.