zng::app

Module crash_handler

source
Expand description

App-process crash handler.

In builds with "crash_handler" feature the crash handler takes over the first “app-process” turning it into the monitor-process, it spawns another process that is the monitored app-process. If the app-process crashes the monitor-process spawns a dialog-process that calls the dialog handler to show an error message, upload crash reports, etc.

The dialog handler can be set using crash_handler_config!.

§Examples

The example below demonstrates an app setup to show a custom crash dialog.

use zng::prelude::*;

fn main() {
    // tracing applied to all processes.
    zng::app::print_tracing(tracing::Level::INFO);

    // monitor-process spawns app-process and if needed dialog-process here.
    zng::env::init!();

    // app-process:
    app_main();
}

fn app_main() {
    APP.defaults().run_window(async {
        Window! {
            child_align = Align::CENTER;
            child = Stack! {
                direction = StackDirection::top_to_bottom();
                spacing = 5;
                children = ui_vec![
                    Button! {
                        child = Text!("Crash (panic)");
                        on_click = hn_once!(|_| {
                            panic!("Test panic!");
                        });
                    },
                    Button! {
                        child = Text!("Crash (access violation)");
                        on_click = hn_once!(|_| {
                            // SAFETY: deliberate access violation
                            #[expect(deref_nullptr)]
                            unsafe {
                                *std::ptr::null_mut() = true;
                            }
                        });
                    }
                ]
            };
        }
    });
}

zng::app::crash_handler::crash_handler_config!(|cfg| {
    // monitor-process and dialog-process

    cfg.dialog(|args| {
        // dialog-process
        APP.defaults().run_window(async move {
            Window! {
                title = "App Crashed!";
                auto_size = true;
                min_size = (300, 100);
                start_position = window::StartPosition::CenterMonitor;
                on_load = hn_once!(|_| WINDOW.bring_to_top());
                padding = 10;
                child = Text!(args.latest().message());
                child_bottom = Stack! {
                    direction = StackDirection::start_to_end();
                    layout::align = Align::BOTTOM_END;
                    spacing = 5;
                    children = ui_vec![
                        Button! {
                            child = Text!("Restart App");
                            on_click = hn_once!(args, |_| {
                                args.restart();
                            });
                        },
                        Button! {
                            child = Text!("Exit App");
                            on_click = hn_once!(|_| {
                                args.exit(0);
                            });
                        },
                    ]
                }, 10;
            }
        });
    });
});

§Debugger

Note that because the crash handler spawns a different process for the app debuggers will not stop at break points in the app code. You can configure your debugger to set the NO_ZNG_CRASH_HANDLER environment variable to not use a crash handler in debug runs.

On VS Code with the CodeLLDB extension you can add this workspace configuration:

"lldb.launch.env": {
   "ZNG_NO_CRASH_HANDLER": ""
}

§Full API

See zng_app::crash_handler and zng_wgt_inspector::crash_handler for the full API.

Macros§

  • Register a FnOnce(&mut CrashConfig) closure to be called on process init to configure the crash handler.

Structs§

Functions§