From 9d04dd224242525dad2e47055e56d94d23319959 Mon Sep 17 00:00:00 2001 From: ScriptedAlchemy Date: Thu, 2 Jul 2026 04:25:58 +0000 Subject: [PATCH] fix: hold mcp-suite test env locks until env guards drop Same drop-order hazard flagged by the PR #178 review comment on IsolatedEnv (fixed in 822d921): TestProject, TestEnv, and CrossProjectMemoryEnv declared _env_lock before their HOME/global-DB env guards, so the lock released before the guards restored env vars, letting a waiting test's freshly installed env get clobbered. --- tests/mcp_suite/mcp_handler_test.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/mcp_suite/mcp_handler_test.rs b/tests/mcp_suite/mcp_handler_test.rs index d35d6d72..e265f1a0 100644 --- a/tests/mcp_suite/mcp_handler_test.rs +++ b/tests/mcp_suite/mcp_handler_test.rs @@ -253,9 +253,13 @@ fn test_temp_dir() -> TestTempDir { struct TestProject { dir: Option, - _env_lock: MutexGuard<'static, ()>, _home_guard: HomeEnvGuard, _global_db_guard: GlobalDbEnvGuard, + // Field order is load-bearing: fields drop in declaration order, so the + // env lock must be declared last. Releasing it before the guards restore + // `HOME` / the global DB override would let the next waiting test install + // its own env, only for these guards to clobber it. + _env_lock: MutexGuard<'static, ()>, } impl std::ops::Deref for TestProject { @@ -273,15 +277,19 @@ impl Drop for TestProject { } struct TestEnv { - _env_lock: MutexGuard<'static, ()>, _home_guard: HomeEnvGuard, _global_db_guard: GlobalDbEnvGuard, + // Drop order = declaration order: the env lock must outlive the guards + // above so their env restores happen while the lock is still held. + _env_lock: MutexGuard<'static, ()>, } struct CrossProjectMemoryEnv { _dir: TestTempDir, - _env_lock: MutexGuard<'static, ()>, _storage_guard: common::TraceDecayStorageEnvGuard, + // Drop order = declaration order: the env lock must outlive the storage + // guard above so its env restore happens while the lock is still held. + _env_lock: MutexGuard<'static, ()>, } struct TestTraceDecay {