rust-script icon indicating copy to clipboard operation
rust-script copied to clipboard

rust-script can leave orphaned child processes when killed on Windows

Open alebastr opened this issue 4 years ago • 2 comments

If you kill the rust-script process it will not pass the kill signal to the child process with a compiled binary. With the way I use rust-script, this leaves me with a few stale processes that I have to clean up manually.

It's easy to address this for unix (although that'll likely disrupt deferred cache cleanup)

Example diff
diff --git a/src/main.rs b/src/main.rs
index 6ea81a3..c97b3fc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -27,6 +27,7 @@ use serde::{Deserialize, Serialize};
 use std::ffi::OsString;
 use std::fs;
 use std::io::{Read, Write};
+use std::os::unix::process::CommandExt;
 use std::path::{Path, PathBuf};
 use std::process::Command;

@@ -481,18 +482,17 @@ fn try_main() -> MainResult<i32> {
         })
     };

-    let exit_code = if action.execute {
+    if action.execute {
         let cmd_name = action.build_kind.exec_command();
         info!("running `cargo {}`", cmd_name);
         let run_quietly = !action.cargo_output;
         let mut cmd = action.cargo(cmd_name, &args.script_args, run_quietly)?;

-        cmd.status().map(|st| st.code().unwrap_or(1))?
+        let err = cmd.exec();
+        Err(MainError::from(err))
     } else {
-        0
-    };
-
-    Ok(exit_code)
+        Ok(0)
+    }
 }

 /**

and not so much for Windows: see implementation used by cargo run or wexecvp-based implementation suggested for exec crate.

alebastr avatar Jun 16 '21 05:06 alebastr

@alebastr Thanks for the nice description. Looking into this.

fornwall avatar Jul 10 '21 09:07 fornwall

For reference: we transitioned to unix exec in https://github.com/fornwall/rust-script/commit/445d9cfa3cabcb2ee9cf37102d3029380db51d31 last year - leaving this open for the Windows part (contributions are welcome, as I don't know much Windows myself).

fornwall avatar Mar 24 '23 15:03 fornwall