Skip to content

fix(test): startup_file_returns_psqlrc_env_when_set is flaky due to shared env var mutation #717

@NikolayS

Description

@NikolayS

Problem

startup_file_returns_psqlrc_env_when_set in src/repl/mod.rs intermittently fails in CI:

test repl::tests::startup_file_returns_psqlrc_env_when_set ... FAILED

Root cause

The test calls std::env::set_var("PSQLRC", ...) then std::env::remove_var("PSQLRC"). Rust test threads run in parallel by default. If another test reads PSQLRC between the set and remove, it sees an unexpected value. If two such tests run concurrently, one clobbers the other's state.

Same pattern affects RPG_TEST_API_KEY_12345 and RPG_EMPTY_KEY_TEST tests at lines ~7417-7435.

Fix

Add a process-wide mutex for env var mutation in tests:

// In mod tests:
static ENV_MUTEX: std::sync::LazyLock<std::sync::Mutex<()>> =
    std::sync::LazyLock::new(|| std::sync::Mutex::new(()));

Then wrap all env-mutating tests:

fn startup_file_returns_psqlrc_env_when_set() {
    let _guard = ENV_MUTEX.lock().unwrap();
    std::env::set_var("PSQLRC", "/tmp/test_rpg_rc");
    // ...
}

Affected tests

  • startup_file_returns_psqlrc_env_when_set (line ~5750)
  • startup_file_returns_none_when_no_rc_exists_and_no_env (line ~5758)
  • api_key_env_reads_set_variable (line ~7417)
  • api_key_env_returns_none_for_unset_variable (line ~7425)
  • api_key_env_returns_none_for_empty_variable (line ~7432)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions