diff --git a/src/main.rs b/src/main.rs index c727040..d472795 100644 --- a/src/main.rs +++ b/src/main.rs @@ -381,10 +381,27 @@ fn daemon_main(config_set: ConfigSet) { info!("spawning worker process..."); let espanso_path = std::env::current_exe().expect("unable to obtain espanso path location"); - crate::process::spawn_process( + let mut child = crate::process::spawn_process( &espanso_path.to_string_lossy().to_string(), &vec!["worker".to_owned()], - ); + ).expect("unable to create worker process"); + + // Create a monitor thread that will exit with the same non-zero code if + // the worker thread exits + thread::Builder::new() + .name("worker monitor".to_string()) + .spawn(move || { + let result = child.wait(); + if let Ok(status) = result { + if let Some(code) = status.code() { + if code != 0 { + error!("worker process exited with non-zero code: {}, exiting", code); + std::process::exit(code); + } + } + } + }) + .expect("Unable to spawn worker monitor thread"); std::thread::sleep(Duration::from_millis(200)); diff --git a/src/process.rs b/src/process.rs index fdf7930..6737b07 100644 --- a/src/process.rs +++ b/src/process.rs @@ -18,10 +18,13 @@ */ use log::warn; +use std::process::{Command, Stdio, Child}; use widestring::WideCString; +use std::io; #[cfg(target_os = "windows")] pub fn spawn_process(cmd: &str, args: &Vec) { + // TODO: modify with https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html let quoted_args: Vec = args.iter().map(|arg| format!("\"{}\"", arg)).collect(); let quoted_args = quoted_args.join(" "); let final_cmd = format!("\"{}\" {}", cmd, quoted_args); @@ -39,8 +42,6 @@ pub fn spawn_process(cmd: &str, args: &Vec) { } #[cfg(not(target_os = "windows"))] -pub fn spawn_process(cmd: &str, args: &Vec) { - use std::process::{Command, Stdio}; - - Command::new(cmd).args(args).spawn(); +pub fn spawn_process(cmd: &str, args: &Vec) -> io::Result { + Command::new(cmd).args(args).spawn() }