Skip to content

Test isolation: private ENV_LOCK and unguarded JCODE_HOME mutation make jcode-app-core tests flaky in parallel #593

Closed

Description

@1jehuang

cargo test -p jcode-app-core --lib fails nondeterministically, with a different small set of tests failing on each run. --test-threads=1 passes cleanly: 1024 passed; 0 failed.

Observed across consecutive runs on the same tree:

run 1: server::reload_recovery::tests::garbage_collection_removes_delivered_and_stale_records
run 2: server::debug_command_exec::tests::debug_tool_selfdev_reload_returns_promptly_for_direct_execution
       server::reload::reload_tests::persist_reload_recovery_intents_records_running_peer_recovery
       server::reload_recovery::tests::garbage_collection_uses_file_age_for_malformed_and_orphaned_records

Each of these passes in isolation, so the assertions are fine; the sandboxing is not.

Root cause

Two independent mutual-exclusion regimes guard the same process-global state.

  1. IsolatedHome in crates/jcode-app-core/src/server/reload_recovery.rs:370 sets the process-wide JCODE_HOME env var in new() and restores it in Drop. Env vars are per-process, so any concurrently running test that resolves a storage path sees another test's temp home. All 9 uses in that file do pair it with crate::storage::lock_test_env(), which is correct.

  2. crates/jcode-app-core/src/server/debug_command_exec.rs:636 defines its own private ENV_LOCK and lock_env() helper, and its EnvGuard mutates env vars under that lock instead of the shared storage::lock_test_env() mutex. Two different mutexes protecting one global means neither actually serializes against the other, so a debug_command_exec test can mutate the environment in the middle of a reload_recovery test that believes it holds the env lock, and vice versa.

That explains both the rotating failure set and why single-threaded runs are green.

Suggested fix

Have every env-mutating test path acquire the same lock. Concretely: drop the private ENV_LOCK/lock_env() in debug_command_exec.rs and route its EnvGuard through crate::storage::lock_test_env(). A grep for other private env mutexes is worthwhile, since the same pattern may exist elsewhere.

Longer term this is a design smell: tests reach global state through env vars rather than an injected config/paths value, so correctness depends on every test author remembering the right lock. Threading an explicit paths/home parameter would remove the whole class.

Why it matters

CI and local runs currently fail for reasons unrelated to the change under test, which trains people to re-run until green and to dismiss real regressions as flakes.

Found while validating an unrelated fix. Separately fixed two genuinely stale tests in this crate (legacy_snapshot_without_mode_defaults_to_light, which dormant-plan pruning had outgrown, and three intent-schema tests that predated central required-intent injection); those were deterministic failures and are not part of this issue.

Activity

  1. added
    bug
    priority: medium
    triage: reproducible
    tech-debt
    on Jul 25, 2026
  2. 1jehuang commented on Jul 25, 2026

    @1jehuang
    OwnerAuthor

    Largely fixed. Consolidating every env-mutating test path onto the single shared storage::lock_test_env() mutex took cargo test -p jcode-app-core --lib from failing on essentially every parallel run to 1024 passed / 0 failed on 7 of 8 consecutive runs.

    Private env mutexes removed, each of which was racing the others:

    • server/debug_command_exec.rs (ENV_LOCK / lock_env)
    • server/lifecycle.rs (TEST_ENV_LOCK)
    • server/provider_control_tests.rs (private LOCK)
    • telemetry_tests.rs (TELEMETRY_TEST_LOCK)
    • tool/selfdev/tests.rs (ENV_LOCK)

    One trap worth recording: lock_test_env() is a plain non-reentrant Mutex, so once these all point at the same lock, any test taking two env guards self-deadlocks. tool/selfdev/tests.rs had 23 tests holding both a _storage_guard and a private _env_lock; after consolidation that hung the suite instead of failing it. Fixed by removing the now-redundant second acquisition. Anyone touching this pattern should keep it to exactly one env guard per test.

    Remaining: roughly 1 run in 8 still shows a single failure, so there is at least one more shared-global race that env locking does not cover. Leaving this open for that residual. The deeper fix is still the design change, threading an explicit paths/home value instead of reaching through process-global env vars, which would remove the whole class rather than serializing around it.

  3. 1jehuang commented on Jul 25, 2026

    @1jehuang
    OwnerAuthor

    Residual flake found and fixed. cargo test -p jcode-app-core --lib is now 1024 passed / 0 failed on 14 consecutive parallel runs (previously it failed on nearly every run, then ~1 in 8).

    The last offender was not an env race at all: tool/bash_tests.rs polled for asynchronous background-task state using fixed iteration budgets, several of which were about one second of wall clock:

    • test_background_command_respects_timeout: 50 x 50ms
    • three progress-observation tests: 50 x 20ms each
    • process-group cleanup: 100 x 10ms

    The commands under test finish in ~100ms, but observing the resulting status depends on scheduler latency, so under a fully parallel suite on a loaded machine the budget simply ran out before the state transition was visible. Converted all five to wall-clock deadlines (30s). The assertions are unchanged and still exact, so a genuine regression fails as loudly as before; it just is not reported as a flake.

    Summary of the whole fix:

    1. Consolidated five competing private env mutexes onto the shared storage::lock_test_env().
    2. Removed the double env-guard acquisitions that then self-deadlocked on that non-reentrant mutex.
    3. Replaced iteration-count polling with wall-clock deadlines in bash_tests.rs.

    Closing as fixed. The design-level point still stands and is worth a separate look someday: tests reach global state through env vars, so correctness depends on every author remembering the right lock. Threading an explicit paths/home value would remove the class rather than serializing around it. Happy to reopen if the suite drifts again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority: mediumtech-debttriage: reproducible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions