1

I have a program that uses std::process::Command::spawn, and it produces zombies.

I know very well that the parent process must read out children exit codes, otherwise zombies happen. This is also warned about in Rustdocs for std::process, and well-explained on SO and the web.

The problem is, subprocesses are identified by pids throughout the program; but I don't see a way to construct a Child referring to known pre-existing pid. Hence I can't call wait() on it.

From std/process.rs source, I can guess that crate::sys::process might expose a directly-callable waitpid() wrapper I could pass the bare pid_t integer to. But also I don't feel easy about reaching into internals of Rust stdlib, especially for this kind of mundane reason. Coding it this way doesn't feel right.

Another solution could be, to try to preserve the Child objects, carry them instead of their pids all over from that spawn() call to termination handler... which is a long way in this program, and would require a chunk of a refactor. It also feels weird to have a process identifier which can't actually be used as an ID, and instead forces me to carry a complete record with all the other fields.

Can this situation be helped? I'm not terribly familiar with Rust dev-process; do I file an issue framing this as an stdlib defect? Am I missing an alternative way?

ulidtko
  • 14,740
  • 10
  • 56
  • 88
  • `Another solution could be, to try to preserve the Child objects` This is the solution. You could do the usual solutions in any linux program: spawn a thread that `wait`s on the child or preferably handle `SIGCHLD` and call `wait` in the handler (the child process ID is in `si_pid` in `siginfo_t`, see `man signal.h`). – KamilCuk Apr 19 '21 at 19:26
  • I fail to follow, why should it be harder to stock the child structure than a integer id ? – Stargateur Apr 19 '21 at 19:36
  • 1
    If you don't want to carry the `Child` object, you can call `waitpid()` directly from the unsafe [`libc`](https://docs.rs/libc/0.2.93/libc/fn.waitpid.html) crate or the safe [`nix`](https://docs.rs/nix/0.20.0/nix/sys/wait/fn.waitpid.html) crate. – user4815162342 Apr 19 '21 at 20:04

0 Answers0