Skip to content

Commit

Permalink
Wait for exit and use original error code so crash handler appears tr…
Browse files Browse the repository at this point in the history
…ansparently
  • Loading branch information
MolotovCherry committed Nov 18, 2024
1 parent eb42d35 commit f5b76ad
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
30 changes: 25 additions & 5 deletions crates/yabg3nml/src/autostart.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::{
collections::VecDeque, env, os::windows::process::CommandExt as _, path::Path, process::Command,
collections::VecDeque,
env,
os::windows::process::{CommandExt as _, ExitCodeExt as _},
path::Path,
process::{Command, ExitCode},
};

use eyre::Result;
use eyre::{eyre, Result};
use shared::popup::fatal_popup;
use tracing::{error, trace};

Expand All @@ -19,7 +23,7 @@ use crate::{
single_instance::SingleInstance,
};

pub fn autostart() -> Result<()> {
pub fn autostart() -> Result<ExitCode> {
// This prohibits multiple app instances
let _singleton = SingleInstance::new();
let _event = Event::new()?;
Expand Down Expand Up @@ -63,7 +67,7 @@ pub fn autostart() -> Result<()> {
.envs(env::vars())
.spawn();

let child = match cmd {
let mut child = match cmd {
Ok(v) => v,
Err(e) => {
fatal_popup(
Expand Down Expand Up @@ -91,5 +95,21 @@ pub fn autostart() -> Result<()> {
);
}

Ok(())
match child.wait() {
Ok(status) => {
trace!(code = status.code(), "original child exit code");

let code = status
.code()
.map(|c| ExitCode::from_raw(c as u32))
.unwrap_or(ExitCode::FAILURE);

Ok(code)
}

Err(error) => {
error!(%error, "failed to wait for child");
Err(eyre!("{error}"))
}
}
}
15 changes: 11 additions & 4 deletions crates/yabg3nml/src/bin/bg3_autostart.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use shared::popup::fatal_popup;
use std::process::ExitCode;

fn main() {
if let Err(e) = yabg3nml::autostart() {
fatal_popup("autostart failure", e.to_string());
use shared::popup::{display_popup, MessageBoxIcon};

fn main() -> ExitCode {
match yabg3nml::autostart() {
Ok(c) => c,
Err(e) => {
display_popup("Autostart Failure", e.to_string(), MessageBoxIcon::Error);
// DO NOT signal failure for the crash handler
ExitCode::SUCCESS
}
}
}
2 changes: 2 additions & 0 deletions crates/yabg3nml/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(windows_process_exit_code_from)]

mod autostart;
mod cli;
mod console;
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.82"
channel = "nightly-2024-11-17"
components = ["rustfmt", "clippy"]
profile = "minimal"

0 comments on commit f5b76ad

Please sign in to comment.