harden(tests): share GLOBAL_ENV_LOCK across env-mutating integration suites (#48) - #68
Merged
Merged
Conversation
…suites (#48) Adds tests/common/mod.rs with a single `pub static GLOBAL_ENV_LOCK: Mutex<()>` plus an `env_lock()` helper that recovers from poisoning. Every integration test that spawns the binary with mutated env now acquires the shared lock across the spawn, so parallel `cargo test` runs (the default) cannot race on env state. Touched suites: runtime_hardening, cloud_hardening, cargo_hardening, node_hardening, git_hardening, gh_glab_gt_hardening, proxy_nudge. For helper-based suites (runtime/cloud/gh-glab-gt/proxy_nudge) the guard is taken inside the shared `run_with_env`/`run_cc`/ `run_capture_stderr` helper; for the others it is added at the top of each env-mutating `#[test]`. Tests that don't mutate env are untouched, so the lock only serializes the suite-internal env path. Why a single per-binary `static Mutex<()>`: every test binary in Rust is its own process, so cross-binary races are impossible — the lock only needs to serialize within a binary. `Mutex::new` is const since 1.63 so no `lazy_static` / `OnceLock` ceremony is required. Note: the original issue also cites unit tests under `src/core/utils.rs` (secure_git_tests, secure_cargo_tests) that mutate env via `std::env::set_var`. Those use a per-module `GLOBAL_ENV_LOCK` already and live outside this branch's contract (src/ untouched). Consolidating them with the integration-side lock would require a shared crate-level lock and is left for a follow-up. Refs #48.
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 #48 (test-suite layer). Consolidates env-mutating integration tests onto a single shared
GLOBAL_ENV_LOCKso parallel test execution can't race on env mutation. Defense-in-depth — the current integration tests all useCommand::env(...)which only affects child env, not parent. The lock prevents a future test that mutates parent env from racing with the existing suite.Real-race follow-up (not in this PR)
Agent A's report flagged that the actual parent-env race lives in
src/core/utils.rsunit tests (which usestd::env::set_var/remove_var) — those mutate the parent process env and CAN race. A pre-existingGLOBAL_ENV_LOCKatsrc/core/utils.rs:1525covers one block; other env-mutating unit tests in the same file don't grab it. Filing separately.Design
tests/common/mod.rsexposespub static GLOBAL_ENV_LOCK: Mutex<()>+ a poisoning-tolerantenv_lock()helper#[test]bodyMutex::newis const since Rust 1.63 — nolazy_staticceremonyVerified
git diff --stat:Partially closes #48 — full closure waits on the
src/core/utils.rsunit-test race follow-up.