From 925e9ec90beab6f5ae54f1a3a206519476e0773c Mon Sep 17 00:00:00 2001 From: Refrag <38794835+Refragg@users.noreply.github.com> Date: Sat, 19 Aug 2023 19:25:00 +0200 Subject: [PATCH] Fix game processes being in a zombie state when exiting (#33) --- src/lib.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index fef51e6..cec9f71 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -620,7 +620,32 @@ unsafe extern "C" fn start_game_clicked( let mut process = Command::new(state.game_path.clone()); - process.spawn().ok(); + let _child = process.spawn(); + + #[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 + + let mut child = match _child { + Ok(child) => child, + Err(e) => { + warn!("Failure starting the game process {e}"); + return false; + } + }; + + std::thread::spawn(move || match child.wait() { + Ok(exit_status) => { + info!("Game process exited with {}", exit_status); + } + Err(e) => { + warn!("Failure waiting for the game process' exit: {e}"); + } + }); + } + return false; }