Skip to content

Commit cd63ce0

Browse files
committed
Fix race condition
Fix race condition in get_mcli_path() at src/util/misc.rs:15. When tests ran in parallel, multiple threads could attempt to create the .mcli directory simultaneously, causing one to fail with "File exists". The fix: - Replaced expect() with proper error handling using pattern matching - Gracefully ignores ErrorKind::AlreadyExists (safe when another thread created it) - Preserves panic behavior for genuine errors (permissions, etc.) - Only prints "Directory created" when actually successful This makes the function thread-safe and eliminates the flaky test failure in GitHub Actions.
1 parent cb5bc35 commit cd63ce0

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

src/util/misc.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ pub fn get_mcli_path() -> String {
1212
let home_dir = dirs::home_dir().expect("Couldn't get home directory");
1313
let mcli_path = format!("{}/.mcli", home_dir.display());
1414
if !Path::new(&mcli_path).exists() {
15-
fs::create_dir(&mcli_path).expect("Couldn't create mostro-cli directory in HOME");
16-
println!("Directory {} created.", mcli_path);
15+
match fs::create_dir(&mcli_path) {
16+
Ok(_) => println!("Directory {} created.", mcli_path),
17+
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
18+
// Directory was created by another thread/process, which is fine
19+
}
20+
Err(e) => panic!("Couldn't create mostro-cli directory in HOME: {}", e),
21+
}
1722
}
1823
mcli_path
1924
}

0 commit comments

Comments
 (0)