From eeb77e26dcb542b334abba8e7f39fa734cd297eb Mon Sep 17 00:00:00 2001 From: Refrag Date: Sat, 19 Aug 2023 15:12:02 +0200 Subject: [PATCH] Wait for the game process to exit on Unix systems This patch fixes the issue where the spawned game processes would never be removed from the system process list on Unix systems. The processes would stay in a zombie state because we weren't waiting for their exit as we should be doing under Unix systems. --- src/lib.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index fef51e6..2295190 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,9 @@ use std::{ }, }; +#[cfg(all(unix, feature = "auto-splitting"))] +use std::thread; + #[cfg(feature = "auto-splitting")] mod auto_splitters; mod ffi; @@ -620,7 +623,29 @@ unsafe extern "C" fn start_game_clicked( let mut process = Command::new(state.game_path.clone()); - process.spawn().ok(); + let child = process.spawn().ok(); + + #[cfg(unix)] + { + // For Unix systems only, spawn a new thread that waits for the process to exit. + // This avoids keeping the process in a zombie state and never letting go of it until + // the plugin is unloaded + thread::spawn(move || { + if child.is_none() { + warn!("Failure starting the game process"); + return; + }; + + let exit_status = child.unwrap().wait(); + if let Err(e) = exit_status { + warn!("Failure waiting for the game process' exit: {e}"); + return; + }; + + info!("Game process exited with {}", exit_status.unwrap()) + }); + } + return false; }