Skip to content

Commit dada575

Browse files
authored
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
1 parent ab6a6e0 commit dada575

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

common/task_executor/src/lib.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub mod test_utils;
33

44
use futures::channel::mpsc::Sender;
55
use futures::prelude::*;
6-
use slog::{crit, debug, o, trace};
6+
use slog::{debug, o, trace};
77
use std::sync::Weak;
88
use tokio::runtime::{Handle, Runtime};
99

@@ -138,23 +138,11 @@ impl TaskExecutor {
138138
name: &'static str,
139139
) {
140140
let mut shutdown_sender = self.shutdown_sender();
141-
let log = self.log.clone();
142-
143141
if let Some(handle) = self.handle() {
144142
handle.spawn(async move {
145143
let timer = metrics::start_timer_vec(&metrics::TASKS_HISTOGRAM, &[name]);
146144
if let Err(join_error) = task_handle.await {
147-
if let Ok(panic) = join_error.try_into_panic() {
148-
let message = panic.downcast_ref::<&str>().unwrap_or(&"<none>");
149-
150-
crit!(
151-
log,
152-
"Task panic. This is a bug!";
153-
"task_name" => name,
154-
"message" => message,
155-
"advice" => "Please check above for a backtrace and notify \
156-
the developers"
157-
);
145+
if let Ok(_panic) = join_error.try_into_panic() {
158146
let _ = shutdown_sender
159147
.try_send(ShutdownReason::Failure("Panic (fatal error)"));
160148
}

lighthouse/src/main.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use futures::TryFutureExt;
1111
use lighthouse_version::VERSION;
1212
use malloc_utils::configure_memory_allocator;
1313
use slog::{crit, info};
14+
use std::backtrace::Backtrace;
1415
use std::path::PathBuf;
1516
use std::process::exit;
1617
use task_executor::ShutdownReason;
@@ -528,6 +529,21 @@ fn run<E: EthSpec>(
528529

529530
let log = environment.core_context().log().clone();
530531

532+
// Log panics properly.
533+
{
534+
let log = log.clone();
535+
std::panic::set_hook(Box::new(move |info| {
536+
crit!(
537+
log,
538+
"Task panic. This is a bug!";
539+
"location" => info.location().map(ToString::to_string),
540+
"message" => info.payload().downcast_ref::<String>(),
541+
"backtrace" => %Backtrace::capture(),
542+
"advice" => "Please check above for a backtrace and notify the developers",
543+
);
544+
}));
545+
}
546+
531547
let mut tracing_log_path: Option<PathBuf> = clap_utils::parse_optional(matches, "logfile")?;
532548

533549
if tracing_log_path.is_none() {

0 commit comments

Comments
 (0)