tokio
tokio copied to clipboard
Create a tokio::process::Child in a new process group
Is your feature request related to a problem? Please describe.
I want to create a child process and later be able to kill this child with all of it's sub- and sub-sub- children on unix-style operating systems.
Describe the solution you'd like
Something along the lines of:
.set_process_group(0)
Describe alternatives you've considered
I'm currently using this code to implement it:
unsafe {
cmd.pre_exec(|| {
// create a new process group
let pid = nix::unistd::getpid();
if let Err(err) = nix::unistd::setpgid(pid, Pid::from_raw(0)) {
warn!("Failed to create new process group: {:#?}", err);
}
Ok(())
});
}
setpgid needs to be called before exec is called after fork.
Additional context
https://www.gnu.org/software/libc/manual/html_node/Process-Group-Functions.html
Thoughts @ipetkov ?
Unfortunately, I think this is the only way to accomplish this. We pretty much wrap the stdlib types, and there aren't any OS extensions that I could find which do the setpgid calls for us
Would it help if I propose this extension in the stdlib?
It's worth a shot! We don't need to wait on the stdlib to potentially add such a wrapper, but having the feature parity would be nice
The stdlib has process_group in std::os::unix::process::CommandExt but it's unstable for now.
Looks like process_group was stabilized and will land on 1.64
I think next steps for this are:
- [ ] wait for 1.64 to be released
- [ ] add a
process_groupAPI behind atokio_unstableflag - [ ] stabilize the API once our MSRV policy allows for requiring a compiler that is >= 1.64