tokio-rs / tokio

A runtime for writing reliable asynchronous applications with Rust. Provides I/O, networking, scheduling, timers, ...
https://tokio.rs
MIT License
26.77k stars 2.47k forks source link

Add a helper to allow Tokio to send signals to Child processes #3379

Open strottos opened 3 years ago

strottos commented 3 years ago

If I spawn a child process using Tokio, e.g.

let process = Command::new("my_prog")
        .spawn()
        .unwrap();

then it would be helpful to be able to send a signal to that child process of my choosing. Something along the lines of:

process.send_signal(SignalKind::SIGINT);

My understanding is that signal sending is non-blocking (or at least a very fast syscall) so that this can be done using something from the nix crate say (which I think the process flag in Tokio brings in already, but I could be wrong).

You may need to consider what to do on Windows with this as well. Maybe these calls can just be ignored there, I'm not sure what's best offhand.

olix0r commented 3 years ago

If it's helpful, I had to implement something like this recently.

ipetkov commented 3 years ago

tokio does not depend on nix at all today, but fwiw it can be done with a libc::kill call as well (though you should take care to not cache the child PID and only signal if child.id().is_some(), else it is possible to signal some other process if the pid got recycled).

If such an API was to be added it would probably need to be OS specific since the signal semantics (outside of killing the process outright) break down with Windows

carlosb1 commented 3 years ago

I could try to implement some solution.. can it be this useful?¿ I reviewed and the libc doesn't support the kill operation for windows

carlosb1 commented 3 years ago

I did a brief research about how we could implement this for Windows.. I don't develop in Windows so they are assumptions:

In short, for POSIX signals, It should be a problem but for Windows... I seems tricky