From dada5750ee8ac86aeb82c82eaa8c7755583c37be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Wed, 31 Jan 2024 19:20:09 +0000 Subject: [PATCH] Properly log panics with slog (#5075) * log panics with slog * update set_hook location * Merge branch 'unstable' of https://github.com/sigp/lighthouse into slog-panics --- common/task_executor/src/lib.rs | 16 ++-------------- lighthouse/src/main.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/common/task_executor/src/lib.rs b/common/task_executor/src/lib.rs index 6bf4cc8e0..2b8877b26 100644 --- a/common/task_executor/src/lib.rs +++ b/common/task_executor/src/lib.rs @@ -3,7 +3,7 @@ pub mod test_utils; use futures::channel::mpsc::Sender; use futures::prelude::*; -use slog::{crit, debug, o, trace}; +use slog::{debug, o, trace}; use std::sync::Weak; use tokio::runtime::{Handle, Runtime}; @@ -138,23 +138,11 @@ impl TaskExecutor { name: &'static str, ) { let mut shutdown_sender = self.shutdown_sender(); - let log = self.log.clone(); - if let Some(handle) = self.handle() { handle.spawn(async move { let timer = metrics::start_timer_vec(&metrics::TASKS_HISTOGRAM, &[name]); if let Err(join_error) = task_handle.await { - if let Ok(panic) = join_error.try_into_panic() { - let message = panic.downcast_ref::<&str>().unwrap_or(&""); - - crit!( - log, - "Task panic. This is a bug!"; - "task_name" => name, - "message" => message, - "advice" => "Please check above for a backtrace and notify \ - the developers" - ); + if let Ok(_panic) = join_error.try_into_panic() { let _ = shutdown_sender .try_send(ShutdownReason::Failure("Panic (fatal error)")); } diff --git a/lighthouse/src/main.rs b/lighthouse/src/main.rs index 06eb06fc0..022b20835 100644 --- a/lighthouse/src/main.rs +++ b/lighthouse/src/main.rs @@ -11,6 +11,7 @@ use futures::TryFutureExt; use lighthouse_version::VERSION; use malloc_utils::configure_memory_allocator; use slog::{crit, info}; +use std::backtrace::Backtrace; use std::path::PathBuf; use std::process::exit; use task_executor::ShutdownReason; @@ -528,6 +529,21 @@ fn run( let log = environment.core_context().log().clone(); + // Log panics properly. + { + let log = log.clone(); + std::panic::set_hook(Box::new(move |info| { + crit!( + log, + "Task panic. This is a bug!"; + "location" => info.location().map(ToString::to_string), + "message" => info.payload().downcast_ref::(), + "backtrace" => %Backtrace::capture(), + "advice" => "Please check above for a backtrace and notify the developers", + ); + })); + } + let mut tracing_log_path: Option = clap_utils::parse_optional(matches, "logfile")?; if tracing_log_path.is_none() {