Skip to content

Commit 796ddb4

Browse files
authored
Rollup merge of rust-lang#93295 - ChrisDenton:tempdir-double-panic, r=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.
2 parents ea75490 + 84c0c9d commit 796ddb4

File tree

1 file changed

+7
-1
lines changed
  • library/std/src/sys_common

1 file changed

+7
-1
lines changed

library/std/src/sys_common/io.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod test {
88
use crate::env;
99
use crate::fs;
1010
use crate::path::{Path, PathBuf};
11+
use crate::thread;
1112
use rand::RngCore;
1213

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

0 commit comments

Comments
 (0)