Skip to content

Commit

Permalink
Exit with error if any thread panics
Browse files Browse the repository at this point in the history
The threads we run  are crucial for the profiler to work correctly. They
should never panic (and we should ensure that's the case) but if a panic
occurs, hard stopping is better.

Test Plan
=========

panicked a thread and the main thread exited.
  • Loading branch information
javierhonduco committed Apr 18, 2024
1 parent 3d15eaf commit 3b6669f
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use std::error::Error;
use std::path::PathBuf;
use std::time::Duration;
use std::panic;
use std::fmt::Write;
use std::fs::File;
use std::ops::RangeInclusive;

use clap::ArgAction;
use clap::Parser;

use inferno::flamegraph;
use std::fmt::Write;
use std::fs::File;
use std::ops::RangeInclusive;
use tracing::Level;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::FmtSubscriber;
Expand All @@ -14,10 +19,6 @@ use lightswitch::object::build_id;
use lightswitch::profiler::Profiler;
use lightswitch::unwind_info::{compact_printing_callback, UnwindInfoBuilder};
use primal::is_prime;
use std::error::Error;
use std::path::PathBuf;

use std::time::Duration;

const SAMPLE_FREQ_RANGE: RangeInclusive<usize> = 1..=1009;

Expand Down Expand Up @@ -82,7 +83,19 @@ struct Cli {
flamegraph_file: PathBuf,
}

/// Exit the main thread if any thread panics. We prefer this behaviour because pretty much every
/// thread is load bearing for the correct functioning.
fn panic_thread_hook() {
let orig_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic_info| {
orig_hook(panic_info);
std::process::exit(1);
}));
}

fn main() -> Result<(), Box<dyn Error>> {
panic_thread_hook();

let args = Cli::parse();

let subscriber = FmtSubscriber::builder()
Expand Down

0 comments on commit 3b6669f

Please sign in to comment.