subtale-cortex

Crates.iosubtale-cortex
lib.rssubtale-cortex
version0.1.0
sourcesrc
created_at2023-09-14 15:12:08.645933
updated_at2023-09-14 15:12:08.645933
descriptionCrash handling for Rust applications
homepagehttps://cortex.subtale.com
repositoryhttps://github.com/subtalegames/cortex
max_upload_size
id972748
size22,954
OSS (github:subtalegames:oss)

documentation

README

Cortex

OSS by Subtale MIT License Apache-2.0 License

Cortex is a flexible crash handling solution for applications written in Rust.

Example

use subtale_cortex::CrashHandler;

fn run_application() {
    // Your application logic...
}

fn crash_handler(output: std::process::Output) {
    // Handle the output of the application process when it crashes
}

fn main() {
    let result = CrashHandler::with_process(run_application)
        .crash_handler(crash_handler)
        .full_backtrace() // Use `RUST_BACKTRACE` full in your application process
        .run();

    match result {
        Ok(true) => ..,  // The application process finished successfully
        Ok(false) => .., // The application process crashed, but the error was handled successfully
        Err(e) => ..,    // An error was encountered spawning the application or when crash handling
    }
}

How it works

Cortex uses the crash handling approach described in this blog post by Mason Remaley (Anthropic Studios) from March 2021.

The crash handling implementation is simple and straight forward: when the application is launched, invoke the application again as a subprocess of itself, and monitor the subprocess for non-successful exit codes.

To prevent the application from recursively invoking itself until infinity, a command argument (--cortex-child) is used to identify whether the process is the crash handler (and so a subprocess should be invoked) or a subprocess.

For example, the first time that the application is run, Cortex identifies that the --cortex-child argument is not present. The application is then self-spawned as a subprocess, this time with the --cortex-child argument included so the regular application logic (run_application() in the example above) can start.

License

Cortex is free and open source. Unless explicitly noted otherwise, all code in this repository is dual-licensed under the MIT License and Apache License, Version 2.0.

This licensing approach is the de facto standard within the Rust ecosystem.

Contributions

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 15

cargo fmt