Repository navigation
Description
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.
-
IsolatedHomeincrates/jcode-app-core/src/server/reload_recovery.rs:370sets the process-wideJCODE_HOMEenv var innew()and restores it inDrop. 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 withcrate::storage::lock_test_env(), which is correct. -
crates/jcode-app-core/src/server/debug_command_exec.rs:636defines its own privateENV_LOCKandlock_env()helper, and itsEnvGuardmutates env vars under that lock instead of the sharedstorage::lock_test_env()mutex. Two different mutexes protecting one global means neither actually serializes against the other, so adebug_command_exectest can mutate the environment in the middle of areload_recoverytest 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
test(selfdev): drop double env-lock acquisition that self-deadlocks (…
1jehuang commentedon Jul 25, 2026
Largely fixed. Consolidating every env-mutating test path onto the single shared
storage::lock_test_env()mutex tookcargo test -p jcode-app-core --libfrom 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(privateLOCK)telemetry_tests.rs(TELEMETRY_TEST_LOCK)tool/selfdev/tests.rs(ENV_LOCK)One trap worth recording:
lock_test_env()is a plain non-reentrantMutex, so once these all point at the same lock, any test taking two env guards self-deadlocks.tool/selfdev/tests.rshad 23 tests holding both a_storage_guardand 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.
test(bash): use wall-clock deadlines for background polling loops (refs
1jehuang commentedon Jul 25, 2026
Residual flake found and fixed.
cargo test -p jcode-app-core --libis 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.rspolled 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 50msThe 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:
storage::lock_test_env().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.
test(tui): serialize render-state tests on one shared lock (refs #593)
test(selfdev): drop double env-lock acquisition that self-deadlocks (…
test(bash): use wall-clock deadlines for background polling loops (refs
test(tui): serialize render-state tests on one shared lock (refs 1jeh…
test(selfdev): drop double env-lock acquisition that self-deadlocks (…
test(bash): use wall-clock deadlines for background polling loops (refs
test(tui): serialize render-state tests on one shared lock (refs 1jeh…
test(selfdev): drop double env-lock acquisition that self-deadlocks (…
test(bash): use wall-clock deadlines for background polling loops (refs
test(tui): serialize render-state tests on one shared lock (refs 1jeh…