Skip to content

tests: process-global HOME mutation races in parallel tokio::test (cli_tests.rs) #34

@hanwencheng

Description

@hanwencheng

Problem

Several integration tests under crates/agentkeys-cli/tests/cli_tests.rs (and similar adjacent test files) mutate the process-global HOME env var via unsafe { std::env::set_var("HOME", ...) } to sandbox session_store::fallback_path() per-test. Because the cli_tests binary runs under tokio::test(flavor = "multi_thread"), multiple tests can be in flight concurrently. Any test that reads HOME (session_store::fallback_path, cmd_init, etc.) can observe the wrong value mid-execution → flaky/nondeterministic CI.

Codex flagged this on PR #18 and the same pattern was added in PRs #19 (fix-15), #20 (fix-16), #27 (fix-11).

Affected tests (current accounting)

Proposed fix

Add the serial_test crate as a dev-dep in crates/agentkeys-cli/Cargo.toml and annotate every test that touches HOME (or AGENTKEYS_SESSION_STORE, or AGENTKEYS_BIOMETRIC) with #[serial]:

use serial_test::serial;

#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn cmd_revoke_self_clears_local_session() {
    unsafe { std::env::set_var("AGENTKEYS_SESSION_STORE", "file") };
    // ...
}

Per-test serialization is sufficient — these tests are fast (~ms) and the count is small (< 20 across the affected PRs).

Alternative considered: thread an explicit fallback_path parameter through session_store so tests don't need env mutation. More invasive (changes the public API of session_store) and not worth the cost vs the serial_test annotation.

Workaround

Until this lands, run with cargo test -- --test-threads=1 (already documented in PR bodies for affected branches).

Cross-references

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions