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

panic hook to log async backtrace to stderr #43

Open
amunra opened this issue Dec 19, 2024 · 1 comment
Open

panic hook to log async backtrace to stderr #43

amunra opened this issue Dec 19, 2024 · 1 comment

Comments

@amunra
Copy link

amunra commented Dec 19, 2024

I came across this crate because I was looking for a way to debug a panic in my code in a tokio application.

I've annotated all my async functions and I wired up a custom panic handler.

My current hacky code is the following:

static ASYNC_PANIC_OCCURRED: AtomicBool = AtomicBool::new(false);

pub fn install_panic_hook() {
    let default_hook = panic::take_hook();
    panic::set_hook(Box::new(move |panic_info| {
        if tokio::runtime::Handle::try_current().is_ok() {
            if !ASYNC_PANIC_OCCURRED.swap(true, Ordering::SeqCst) {
                let bt = async_backtrace::backtrace();
                let mut bt_buffer = String::new();
                if let Some(bt) = bt {
                    let locations = &*bt;
                    for (index, loc) in locations.iter().enumerate() {
                        write!(bt_buffer, "{index:3}: {loc:?}\n").unwrap();
                    }
                } else {
                    bt_buffer.push_str("[no accessible async backtrace]");
                }
                let all = async_backtrace::taskdump_tree(true);
                eprintln!(
                    "=== Async Backtrace (panic occurred in tokio runtime) ===\n\
                    {bt_buffer}\n\
                    ------- TASK DUMP TREE -------\n\
                    {all}\n\
                    === End Async Backtrace ===\n"
                );
            }
        }

        default_hook(panic_info);
    }));
}

Granted that I should implement this differently not to allocate on panic and trap/supress any panics that may be raised within the handler itsef, but that's a different matter.

More fundamentally, I'm struggling to get let bt = async_backtrace::backtrace(); working: It's always returning None.

Side note: It doesn't get any better if I remove the atomic boolean guard. I suspect tokio is trapping/re-raising the panic a number of times? The boolean is a workaround hack for that.

I would appreciate some guidance.

Thank you!

P.S.: async_backtrace::taskdump_tree(true); does, at least, work.

@amunra amunra changed the title panic_hook panic hook to log async backtrace to stderr Dec 19, 2024
@amunra
Copy link
Author

amunra commented Dec 19, 2024

Update:
It turns out that my code panicked inside a blocking thread, ie. inside a blocking task started with:

tokio::task::spawn_blocking(...)

I guess that explains things: The panicing thread wasn't async.

Nevertheless, having a hook would be very useful indeed for debugging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant