about summary refs log tree commit diff
path: root/library/std/src/panicking.rs
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-04-11 22:38:53 +0200
committerGitHub <noreply@github.com>2024-04-11 22:38:53 +0200
commit1e99af514b68d0ff6891fa3cb6a9946c3888caef (patch)
tree9a0e27ae2496b1dceefca827032c57c25ab763b9 /library/std/src/panicking.rs
parentaa6a697a1c75b0aa06954136f7641706edadc2be (diff)
parentc2e5ee40b699608128654f2b67e0fafa8e1ec8dc (diff)
downloadrust-1e99af514b68d0ff6891fa3cb6a9946c3888caef.tar.gz
rust-1e99af514b68d0ff6891fa3cb6a9946c3888caef.zip
Rollup merge of #122882 - Zoxc:panic-output-panic, r=Amanieu
Avoid a panic in `set_output_capture` in the default panic handler

This avoid a panic in the default panic handler by not using `set_output_capture` as `OUTPUT_CAPTURE.with` may panic once `OUTPUT_CAPTURE` is dropped.

A new non-panicking `try_set_output_capture` variant of `set_output_capture` is added for use in the default panic handler.
Diffstat (limited to 'library/std/src/panicking.rs')
-rw-r--r--library/std/src/panicking.rs8
1 files changed, 4 insertions, 4 deletions
diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs
index f46e1e171d2..0052fcbb94a 100644
--- a/library/std/src/panicking.rs
+++ b/library/std/src/panicking.rs
@@ -24,11 +24,11 @@ use crate::sys_common::backtrace;
 use crate::thread;
 
 #[cfg(not(test))]
-use crate::io::set_output_capture;
+use crate::io::try_set_output_capture;
 // make sure to use the stderr output configured
 // by libtest in the real copy of std
 #[cfg(test)]
-use realstd::io::set_output_capture;
+use realstd::io::try_set_output_capture;
 
 // Binary interface to the panic runtime that the standard library depends on.
 //
@@ -284,9 +284,9 @@ fn default_hook(info: &PanicInfo<'_>) {
         }
     };
 
-    if let Some(local) = set_output_capture(None) {
+    if let Ok(Some(local)) = try_set_output_capture(None) {
         write(&mut *local.lock().unwrap_or_else(|e| e.into_inner()));
-        set_output_capture(Some(local));
+        try_set_output_capture(Some(local)).ok();
     } else if let Some(mut out) = panic_output() {
         write(&mut out);
     }