Exit cleanly on panic! so the process don't limp along in a half-dead state (#9690) (#9692)

automerge
This commit is contained in:
mergify[bot]
2020-04-23 14:07:12 -07:00
committed by GitHub
parent 253272d757
commit 38c31c3b4e

View File

@ -5,13 +5,17 @@ use gethostname::gethostname;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::*; use log::*;
use solana_sdk::hash::hash; use solana_sdk::hash::hash;
use std::collections::HashMap; use std::{
use std::convert::Into; collections::HashMap,
use std::sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender}; convert::Into,
use std::sync::{Arc, Barrier, Mutex, Once, RwLock}; sync::{
use std::thread; mpsc::{channel, Receiver, RecvTimeoutError, Sender},
use std::time::{Duration, Instant}; Arc, Barrier, Mutex, Once, RwLock,
use std::{cmp, env}; },
thread,
time::{Duration, Instant},
{cmp, env},
};
type CounterMap = HashMap<(&'static str, u64), CounterPoint>; type CounterMap = HashMap<(&'static str, u64), CounterPoint>;
@ -416,11 +420,10 @@ pub fn flush() {
/// Hook the panic handler to generate a data point on each panic /// Hook the panic handler to generate a data point on each panic
pub fn set_panic_hook(program: &'static str) { pub fn set_panic_hook(program: &'static str) {
use std::panic;
static SET_HOOK: Once = Once::new(); static SET_HOOK: Once = Once::new();
SET_HOOK.call_once(|| { SET_HOOK.call_once(|| {
let default_hook = panic::take_hook(); let default_hook = std::panic::take_hook();
panic::set_hook(Box::new(move |ono| { std::panic::set_hook(Box::new(move |ono| {
default_hook(ono); default_hook(ono);
let location = match ono.location() { let location = match ono.location() {
Some(location) => location.to_string(), Some(location) => location.to_string(),
@ -438,9 +441,11 @@ pub fn set_panic_hook(program: &'static str) {
.to_owned(), .to_owned(),
Level::Error, Level::Error,
); );
// Flush metrics immediately in case the process exits immediately // Flush metrics immediately
// upon return
flush(); flush();
// Exit cleanly so the process don't limp along in a half-dead state
std::process::exit(1);
})); }));
}); });
} }