Skip to content

Commit 54c0231

Browse files
committed
Abort earlier upon multi-panics
The double-panic `abort` is run after the logging code, to provide feedback in case of a double-panic. This means that if the panic logging fails with a panic, the `abort` might never be reached. This should not normally occur, but if the `on_panic` function detects more than 2 panics, aborting *before* logging makes panic handling somewhat more robust, as it avoids an infinite recursion, which would eventually crash the process, but also make the problem harder to debug. This handles the FIXME about what to do if the thread printing panics.
1 parent c7b8490 commit 54c0231

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/libstd/panicking.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ fn log_panic(obj: &(Any+Send), file: &'static str, line: u32,
5252
let prev = LOCAL_STDERR.with(|s| s.borrow_mut().take());
5353
match (prev, err.as_mut()) {
5454
(Some(mut stderr), _) => {
55-
// FIXME: what to do when the thread printing panics?
5655
write(&mut *stderr);
5756
let mut s = Some(stderr);
5857
LOCAL_STDERR.with(|slot| {
@@ -71,6 +70,17 @@ pub fn on_panic(obj: &(Any+Send), file: &'static str, line: u32) {
7170
count
7271
});
7372

73+
// If this is the third nested call, on_panic triggered the last panic,
74+
// otherwise the double-panic check would have aborted the process.
75+
// Even if it is likely that on_panic was unable to log the backtrace,
76+
// abort immediately to avoid infinite recursion, so that attaching a
77+
// debugger provides a useable stacktrace.
78+
if panics >= 3 {
79+
util::dumb_print(format_args!("thread panicked while processing \
80+
panic. aborting."));
81+
unsafe { intrinsics::abort() }
82+
}
83+
7484
// If this is a double panic, make sure that we print a backtrace
7585
// for this panic. Otherwise only print it if logging is enabled.
7686
let log_backtrace = panics >= 2 || backtrace::log_enabled();

0 commit comments

Comments
 (0)