oconnor663 / duct.rs

a Rust library for running child processes
MIT License
819 stars 34 forks source link

some way to kill a running process? #27

Closed oconnor663 closed 7 years ago

oconnor663 commented 7 years ago

Probably start will need to keep more data around. We might need another type of explicit tree to mirror the expression tree, which knows how to wait and kill its children, similar to Rust's stdlib.

oconnor663 commented 7 years ago

This is going to run into an issue with then. If there's a worker thread sitting on wait on the child process, there's no way to kill it, because both methods require &mut self. r/rust question: https://www.reddit.com/r/rust/comments/5lk1lw/hey_rustaceans_got_an_easy_question_ask_here_12017/dc3fft3/

oconnor663 commented 7 years ago

It's possible we could use process groups on Unix and Windows to handle killing. See:

oconnor663 commented 7 years ago

Ah and there's Rust stdlib support for this kind of: https://doc.rust-lang.org/std/os/unix/process/trait.CommandExt.html

oconnor663 commented 7 years ago

Process groups is designed for this sort of thing, and I think I can get it working. However, it has one huge downside, which is that taking child processes out of the parent's group means that Ctrl-C no longer the children automatically (by sending a signal to the parent's entire group). I'm not sure there's a workaround for this that doesn't require the parent to specially handle that signal, as I think shells do.

oconnor663 commented 7 years ago

OMG NO! There's a chance this can work for real. There's a WNOWAIT flag that a bunch of different interfaces support, to allow waiting without reaping. We could use that to get safe kills? (And waiting using our own library calls on the PID would let us call kill later on the handle, which is nice.)

This sort of thing seems to be the norm on Windows, where PIDs don't get recycled until all handles are closed. That's actually a lot nicer than Unix :p https://blogs.msdn.microsoft.com/oldnewthing/20110107-00/?p=11803/

oconnor663 commented 7 years ago

Relevant: https://github.com/rust-lang/rust/issues/38903

oconnor663 commented 7 years ago

Looks like libc includes waitid: https://github.com/rust-lang/libc/pull/489

oconnor663 commented 7 years ago

Maybe it's ok to accept a race condition on OSX here? We kind of have to with no pipe2 support anyway.

oconnor663 commented 7 years ago

0304db7b543a266968ab90941a02a0e25d689346