Skip to content

Commit

Permalink
Rollup merge of rust-lang#93295 - ChrisDenton:tempdir-double-panic, r…
Browse files Browse the repository at this point in the history
…=dtolnay

Avoid double panics when using `TempDir` in tests

`TempDir` could panic on drop if `remove_dir_all` returns an error. If this happens while already panicking, the test process would abort and therefore not show the test results.

This PR tries to avoid such double panics.
  • Loading branch information
matthiaskrgr authored Jan 28, 2022
2 parents ea75490 + 84c0c9d commit 796ddb4
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion library/std/src/sys_common/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod test {
use crate::env;
use crate::fs;
use crate::path::{Path, PathBuf};
use crate::thread;
use rand::RngCore;

pub struct TempDir(PathBuf);
Expand All @@ -29,7 +30,12 @@ pub mod test {
// Gee, seeing how we're testing the fs module I sure hope that we
// at least implement this correctly!
let TempDir(ref p) = *self;
fs::remove_dir_all(p).unwrap();
let result = fs::remove_dir_all(p);
// Avoid panicking while panicking as this causes the process to
// immediately abort, without displaying test results.
if !thread::panicking() {
result.unwrap();
}
}
}

Expand Down

0 comments on commit 796ddb4

Please sign in to comment.