Skip to content

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

Open
@alebastr

Description

@alebastr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions