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
This commit is contained in:
João Oliveira 2024-01-31 19:20:09 +00:00 committed by GitHub
parent ab6a6e0741
commit dada5750ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 14 deletions

View File

@ -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(&"<none>");
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)"));
}

View File

@ -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<E: EthSpec>(
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::<String>(),
"backtrace" => %Backtrace::capture(),
"advice" => "Please check above for a backtrace and notify the developers",
);
}));
}
let mut tracing_log_path: Option<PathBuf> = clap_utils::parse_optional(matches, "logfile")?;
if tracing_log_path.is_none() {