Skip to content

Commit 2a12535

Browse files
authored
fix: use atomic counter in pet-pipenv test temp dirs to prevent parallel collisions (#408)
The `unique_temp_dir()` helper in pet-pipenv tests used `SystemTime::now().as_nanos()` to generate temp directory names. On Windows, `SystemTime` has ~15.6ms resolution, so parallel test threads can get the same value — causing one test's cleanup (`remove_dir_all`) to destroy another test's files mid-execution. **Fix:** Replace the nanos-based name with `process_id + AtomicU64 counter`, which is guaranteed unique across parallel test threads within the same test binary. This was observed as a flaky `detect_pipenv_centralized_env_without_project_file_via_naming_pattern` failure on Windows CI (e.g., PR #407 CI run).
1 parent f93254b commit 2a12535

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

crates/pet-pipenv/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,15 @@ impl Locator for PipEnv {
495495
#[cfg(test)]
496496
mod tests {
497497
use super::*;
498-
use std::time::{SystemTime, UNIX_EPOCH};
498+
use std::sync::atomic::{AtomicU64, Ordering};
499+
500+
static TEST_DIR_COUNTER: AtomicU64 = AtomicU64::new(0);
499501

500502
fn unique_temp_dir() -> PathBuf {
501503
let mut dir = std::env::temp_dir();
502-
let nanos = SystemTime::now()
503-
.duration_since(UNIX_EPOCH)
504-
.unwrap()
505-
.as_nanos();
506-
dir.push(format!("pet_pipenv_test_{}", nanos));
504+
let id = std::process::id();
505+
let counter = TEST_DIR_COUNTER.fetch_add(1, Ordering::Relaxed);
506+
dir.push(format!("pet_pipenv_test_{}_{}", id, counter));
507507
dir
508508
}
509509

0 commit comments

Comments
 (0)