Log when assertion triggered

In a multi-threaded environment, when an assertion is triggered in one thread, the panic won't be observed until the JoinHandle::join is called.

What is the most idiomatic way to log when an assertion is triggered instead of waiting for the JoinHandle::join?

// log if some_condition is false
if !some_condition {
    error!("something wrong");
    assert("something wrong");

Maybe one option could be to poll JoinHandle::is_finished regularly, though this doesn't seem really good to me. Another alternative I could think of is to use std::panic::catch_unwind in the most outer block of your thread and report the panic from within the thread.

I think a custom reporting mechanism based on catch_unwind plus some channel would be the best.

There is also a process-global panic hook that you could use to observe all panics everywhere. Note that this is a shared global, so setting it will overwrite any other panic handlers.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.