Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit on panics, rather than unwinding #3177

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion crates/subspace-farmer/src/bin/subspace-farmer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ mod commands;
mod utils;

use clap::Parser;
use std::fs;
use std::path::PathBuf;
use std::process::exit;
use std::{fs, panic};
use subspace_farmer::single_disk_farm::{ScrubTarget, SingleDiskFarm};
use subspace_proof_of_space::chia::ChiaTable;
use tracing::info;
Expand Down Expand Up @@ -74,6 +75,14 @@ enum Command {

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Exit on panics, rather than unwinding. Unwinding can hang the tokio runtime waiting for
// stuck tasks or threads.
let default_panic_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic_info| {
default_panic_hook(panic_info);
exit(1);
}));

tracing_subscriber::registry()
.with(
fmt::layer()
Expand Down
12 changes: 12 additions & 0 deletions crates/subspace-gateway/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pub(crate) mod run;

use crate::commands::run::RunOptions;
use clap::Parser;
use std::panic;
use std::process::exit;
use tokio::signal;
use tracing::level_filters::LevelFilter;
use tracing::{debug, warn};
Expand All @@ -20,6 +22,16 @@ pub enum Command {
// TODO: subcommand to run various benchmarks
}

/// Install a panic handler which exits on panics, rather than unwinding. Unwinding can hang the
/// tokio runtime waiting for stuck tasks or threads.
pub(crate) fn set_exit_on_panic() {
let default_panic_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic_info| {
default_panic_hook(panic_info);
exit(1);
}));
}

pub(crate) fn init_logger() {
// TODO: Workaround for https://github.com/tokio-rs/tracing/issues/2214, also on
// Windows terminal doesn't support the same colors as bash does
Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ mod node_client;
mod piece_getter;
mod piece_validator;

use crate::commands::{init_logger, raise_fd_limit, Command};
use crate::commands::{init_logger, raise_fd_limit, set_exit_on_panic, Command};
use clap::Parser;

#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
set_exit_on_panic();
init_logger();
raise_fd_limit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::panic;
use std::process::exit;
use std::sync::Arc;
use subspace_metrics::{start_prometheus_metrics_server, RegistryAdapter};
use subspace_networking::libp2p::multiaddr::Protocol;
Expand Down Expand Up @@ -105,6 +107,16 @@ impl KeypairOutput {
}
}

/// Install a panic handler which exits on panics, rather than unwinding. Unwinding can hang the
/// tokio runtime waiting for stuck tasks or threads.
fn set_exit_on_panic() {
let default_panic_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic_info| {
default_panic_hook(panic_info);
exit(1);
}));
}

fn init_logging() {
// set default log to info if the RUST_LOG is not set.
let env_filter = EnvFilter::builder()
Expand All @@ -118,6 +130,7 @@ fn init_logging() {

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
set_exit_on_panic();
init_logging();

let command: Command = Command::parse();
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-node/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pub use domain_key::{
create_domain_key, insert_domain_key, CreateDomainKeyOptions, InsertDomainKeyOptions,
};
pub use run::{run, RunOptions};
pub(crate) use shared::set_exit_on_panic;
pub use wipe::{wipe, WipeOptions};
12 changes: 12 additions & 0 deletions crates/subspace-node/src/commands/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use sp_core::sr25519::Pair;
use sp_core::Pair as PairT;
use sp_domains::KEY_TYPE;
use sp_keystore::Keystore;
use std::panic;
use std::path::PathBuf;
use std::process::exit;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};
Expand Down Expand Up @@ -52,6 +54,16 @@ pub(super) fn store_key_in_keystore(
.map_err(|()| Error::Application("Failed to insert key into keystore".to_string().into()))
}

/// Install a panic handler which exits on panics, rather than unwinding. Unwinding can hang the
/// tokio runtime waiting for stuck tasks or threads.
pub(crate) fn set_exit_on_panic() {
let default_panic_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic_info| {
default_panic_hook(panic_info);
exit(1);
}));
}

pub(super) fn init_logger() {
// TODO: Workaround for https://github.com/tokio-rs/tracing/issues/2214, also on
// Windows terminal doesn't support the same colors as bash does
Expand Down
3 changes: 3 additions & 0 deletions crates/subspace-node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod cli;
mod domain;

use crate::cli::{Cli, SubspaceCliPlaceholder};
use crate::commands::set_exit_on_panic;
use crate::domain::cli::DomainKey;
use crate::domain::{DomainCli, DomainSubcommand};
use clap::Parser;
Expand Down Expand Up @@ -130,6 +131,8 @@ fn derive_pot_external_entropy(
}

fn main() -> Result<(), Error> {
set_exit_on_panic();

match Cli::parse() {
Cli::Run(run_options) => {
commands::run(run_options)?;
Expand Down
Loading