Skip to content

perf(xtask): use RwLock for CWD_LOCK so spawning tests run in parallel (#2124) - #2132

Merged
EffortlessSteven merged 1 commit into
mainfrom
codex/xtask-cwd-rwlock-2124
Jul 21, 2026
Merged

EffortlessSteven merged 1 commit into
mainfrom
codex/xtask-cwd-rwlock-2124

Conversation

@EffortlessSteven

Copy link
Copy Markdown
Member

Problem (#2124)

The CWD_LOCK added in #2051 was a single Mutex<()>. Every subprocess-spawning test (14 in run.rs) AND every cwd-manipulating test (with_temp_cwd, with_repo_cwd) acquired the SAME exclusive lock. This meant:

  • Two spawning tests could not run in parallel (they only need to exclude cwd-manipulating tests, not each other).
  • The 109s policy_checker_facade_runs_current_repo_checks test blocked every other spawning test behind the mutex.

The current wall-clock impact is acceptable (rust-tests-junit on main finishes in 4-6 minutes), but the over-serialisation is unnecessary.

Change

Replace Mutex<()> with RwLock<()>:

// Write guard (exclusive) — for tests that change the process cwd:
pub(crate) fn acquire_test_cwd_write_guard() -> RwLockWriteGuard<'static, ()> { ... }

// Read guard (shared) — for tests that spawn subprocesses inheriting cwd:
pub(crate) fn acquire_test_cwd_read_guard() -> RwLockReadGuard<'static, ()> { ... }
  • with_temp_cwd and with_repo_cwd in main.rs use the write guard (they change the process cwd and delete temp dirs).
  • All 14 spawning tests in run.rs use the read guard (they inherit the process cwd but don't change it, so they can run in parallel with each other).

This restores parallelism for the spawning-test lane while keeping the race fix (#2044) intact: a spawning test and a cwd-manipulating test can never run concurrently (read excludes write).

Why RwLock and not two separate Mutexes?

A two-mutex approach (one for cwd, one for spawn) would NOT close the race — the spawning test needs to be excluded from the cwd-manipulating test's set_current_dir/remove_dir_all window, and vice versa. RwLock is the standard primitive for "multiple readers OR one writer, never both" — exactly this access pattern.

Evidence

cargo check -p xtask --all-targets   PASS
cargo test -p xtask                  PASS (1204 tests, 0 failures, 114s)
cargo clippy -p xtask -D warnings    PASS
cargo xtask check-no-panic-family    PASS (0 stale, 0 unallowed)

The 1204-test suite passes with no flakes across the full parallel run — the race #2044 fixed is genuinely closed by the read/write exclusion, not by the global mutex.

Scope

Two-file change:

  • xtask/src/main.rs: CWD_LOCK type changed from Mutex to RwLock; acquire_test_cwd_lock split into acquire_test_cwd_write_guard + acquire_test_cwd_read_guard; with_temp_cwd and with_repo_cwd updated to use the write guard.
  • xtask/src/run.rs: import updated; all 14 spawning tests updated to use the read guard.

No production code change. No behavior change for the test outcomes — only the parallelism model. Closes #2124.

Non-goals

  • Does NOT change which tests are cwd-sensitive. Only the lock granularity.
  • Does NOT partition tests into separate test binaries. That's a larger structural change.

#2124)

The CWD_LOCK was a single Mutex<()>. Every subprocess-spawning test
(14 in run.rs) AND every cwd-manipulating test (with_temp_cwd,
with_repo_cwd) acquired the SAME exclusive lock. This meant:

- Two spawning tests could not run in parallel (they only need to
  exclude cwd-manipulating tests, not each other).
- The 109s policy_checker_facade_runs_current_repo_checks test
  blocked every other spawning test behind the mutex.

## Change

Replace Mutex<()> with RwLock<()>:

- acquire_test_cwd_write_guard() -> RwLockWriteGuard (exclusive)
  Used by with_temp_cwd and with_repo_cwd — the tests that actually
  change the process cwd and delete temp dirs.

- acquire_test_cwd_read_guard() -> RwLockReadGuard (shared)
  Used by all 14 spawning tests in run.rs — they inherit the process
  cwd but don't change it, so they can run in parallel with each
  other. They are only excluded while a cwd-manipulating test holds
  the write guard.

This restores parallelism for the spawning-test lane while keeping
the race fix (#2044) intact: a spawning test and a cwd-manipulating
test can never run concurrently.

## Renamed

- acquire_test_cwd_lock() -> split into acquire_test_cwd_write_guard()
  and acquire_test_cwd_read_guard() so the caller's intent is explicit.

Verified:
  cargo check -p xtask --all-targets   PASS
  cargo test -p xtask                  PASS (1204 tests, 0 failures, 114s)
  cargo clippy -p xtask -D warnings    PASS
  cargo xtask check-no-panic-family    PASS (0 stale, 0 unallowed)

Closes #2124.
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: c7ca36fa-eacb-4f5d-81c7-a34fc6aa935a

📥 Commits

Reviewing files that changed from the base of the PR and between c953f0b and 4a8257b.

📒 Files selected for processing (2)
  • xtask/src/main.rs
  • xtask/src/run.rs
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • EffortlessMetrics/ripr (auto-detected)
  • EffortlessMetrics/unsafe-review (auto-detected)
  • EffortlessMetrics/perl-lsp (auto-detected)
  • EffortlessMetrics/ub-review (auto-detected)

Summary by CodeRabbit

  • Tests
    • Improved test execution so independent subprocess tests can run concurrently.
    • Preserved safeguards for tests that modify the current working directory, reducing race conditions and improving reliability.

Walkthrough

The test CWD lock changes from Mutex to RwLock. CWD-mutating helpers acquire exclusive write guards, while subprocess-spawning tests acquire shared read guards, allowing those tests to run concurrently.

Changes

CWD test synchronization

Layer / File(s) Summary
Shared and exclusive CWD guard API
xtask/src/main.rs
CWD_LOCK uses RwLock, with separate helpers returning read and write guards.
Guard call-site migration
xtask/src/main.rs, xtask/src/run.rs
CWD-changing helpers use write guards, while subprocess-spawning tests use read guards.

Estimated code review effort: 2 (Simple) | ~10 minutes

Poem

I’m a rabbit guarding paths in the night,
Writers hold tight, readers share the light.
Spawn little tests side by side,
While changing-CWD bunnies wait outside.
Hop, hop—less queue, same stride!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the CWD_LOCK RwLock change and the parallel test impact.
Description check ✅ Passed The description covers the problem, fix, evidence, scope, and non-goals, though several template sections are left empty.
Linked Issues check ✅ Passed The change matches #2124 by splitting cwd access into read and write guards and preserving race protection.
Out of Scope Changes check ✅ Passed The changes stay within xtask test-locking behavior and the two expected files, with no obvious unrelated additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/xtask-cwd-rwlock-2124

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ast-grep (0.44.1)
xtask/src/main.rs

ast-grep timed out on this file


Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the test suite in xtask/src/run.rs by replacing the exclusive acquire_test_cwd_lock with acquire_test_cwd_read_guard across multiple test cases. This change allows tests that do not modify the current working directory to run concurrently using a shared read lock. There are no review comments, and 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

codecov Bot commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

perf(xtask): CWD_LOCK over-serialises spawning tests against cwd-manipulating tests

1 participant