Right now my panics are being swallowed. In my use case, I would like it to crash entire program and also print the stack trace. How should I configure it?
Asked
Active
Viewed 4,659 times
1 Answers
16
Panics are generally not swallowed, instead they are returned as an error when awaiting the tokio::task::JoinHandle
returned from tokio::task::spawn()
or tokio::task::spawn_blocking()
and can be handled accordingly.
If a panic occurs within the Tokio runtime an error message is printed to stderr like this: "thread 'tokio-runtime-worker' panicked at 'Panicking...', src\main.rs:26:17". If you run the binary with the environment variable RUST_BACKTRACE
set to 1 a stacktrace is printed as well.
As with all Rust programs you can set your own panic handler with std::panic::set_hook()
to make it exit if any thread panics after printing the panic info like this:
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
default_panic(info);
std::process::exit(1);
}));

HHK
- 4,852
- 1
- 23
- 40
-
Exiting just like that is not ideal, in real world scenarios you first need to make sure to shut down or free any reserved resources, connections etc before exiting. – vasilakisfil Aug 21 '23 at 10:58