tokio icon indicating copy to clipboard operation
tokio copied to clipboard

Create a tokio::process::Child in a new process group

Open kpcyrd opened this issue 3 years ago • 7 comments

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

kpcyrd avatar Dec 10 '21 12:12 kpcyrd

Thoughts @ipetkov ?

Darksonn avatar Dec 10 '21 12:12 Darksonn

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

ipetkov avatar Dec 10 '21 17:12 ipetkov

Would it help if I propose this extension in the stdlib?

kpcyrd avatar Dec 10 '21 18:12 kpcyrd

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

ipetkov avatar Dec 10 '21 18:12 ipetkov

The stdlib has process_group in std::os::unix::process::CommandExt but it's unstable for now.

danyspin97 avatar Jul 26 '22 14:07 danyspin97

Looks like process_group was stabilized and will land on 1.64

ipetkov avatar Jul 27 '22 00:07 ipetkov

I think next steps for this are:

  • [ ] wait for 1.64 to be released
  • [ ] add a process_group API behind a tokio_unstable flag
  • [ ] stabilize the API once our MSRV policy allows for requiring a compiler that is >= 1.64

ipetkov avatar Jul 27 '22 01:07 ipetkov