Rollup merge of #149004 - clubby789:compiletest-delete-safe, r=jieyouxu

compiletest: Avoid race condition in file deletion

Fixes rust-lang/rust#149003

There is a TOCTOU when multiple threads attempt to delete the same file. Instead of pre-checking if the file exists, we can simply ignore `NotFound` errors.
This commit is contained in:
Stuart Cook
2025-11-17 16:41:06 +11:00
committed by GitHub
+4 -5
View File
@@ -2766,12 +2766,11 @@ fn load_expected_output_from_path(&self, path: &Utf8Path) -> Result<String, Stri
.map_err(|err| format!("failed to load expected output from `{}`: {}", path, err))
}
/// Attempts to delete a file, succeeding if the file does not exist.
fn delete_file(&self, file: &Utf8Path) {
if !file.exists() {
// Deleting a nonexistent file would error.
return;
}
if let Err(e) = fs::remove_file(file.as_std_path()) {
if let Err(e) = fs::remove_file(file.as_std_path())
&& e.kind() != io::ErrorKind::NotFound
{
self.fatal(&format!("failed to delete `{}`: {}", file, e,));
}
}