New layer-0 crate lutra-proc: spawn(Command) puts the child into a kill-on-close group. Windows: one job object per child with JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE (windows-sys), nested jobs work. Unix: a new process group plus a /bin/sh watchdog that kills the group when Lutra's pipe closes (drop, kill, quit or SIGKILL of Lutra). Child::kill and Drop take the whole group. Phpactor (supervision spawn) and the php -v probe in lutra-lsp start through it. Tests kill a parent hard and check that its child is gone (with and without the parent in a group), plus kill and drop of a tree. Docs: architecture "Child processes", G6 note. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
103 lines
3.2 KiB
Rust
103 lines
3.2 KiB
Rust
//! Process trees started through `lutra_proc` die with their owner.
|
|
//!
|
|
//! Each test starts `lutra-proc-helper` in a tree role: the helper starts a
|
|
//! sleeping grandchild that holds its standard output, prints `ready`
|
|
//! and sleeps. Reading that output to end of file proves that every
|
|
//! process holding it, the grandchild included, has exited. Without the
|
|
//! groups the grandchild sleeps for two minutes and the read times out.
|
|
|
|
#![allow(clippy::unwrap_used)]
|
|
|
|
use std::io::{BufRead, BufReader, Read};
|
|
use std::process::{ChildStdout, Command, Stdio};
|
|
use std::sync::mpsc;
|
|
use std::thread;
|
|
use std::time::Duration;
|
|
|
|
/// How long a test waits for a tree to go away.
|
|
const GONE_WITHIN: Duration = Duration::from_secs(15);
|
|
|
|
/// The helper in `role`, with its output piped.
|
|
fn helper(role: &str) -> Command {
|
|
let mut command = Command::new(env!("CARGO_BIN_EXE_lutra-proc-helper"));
|
|
command
|
|
.arg(role)
|
|
.stdin(Stdio::null())
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::null());
|
|
command
|
|
}
|
|
|
|
/// Waits for the helper's `ready` line and returns the rest of its output.
|
|
fn ready(stdout: ChildStdout) -> BufReader<ChildStdout> {
|
|
let mut reader = BufReader::new(stdout);
|
|
let mut line = String::new();
|
|
reader.read_line(&mut line).unwrap();
|
|
assert_eq!(line.trim(), "ready");
|
|
reader
|
|
}
|
|
|
|
/// Asserts that the output reaches end of file within [`GONE_WITHIN`],
|
|
/// i.e. that no process holding it is left.
|
|
fn assert_gone(mut reader: BufReader<ChildStdout>) {
|
|
let (tx, rx) = mpsc::channel();
|
|
thread::spawn(move || {
|
|
let mut rest = Vec::new();
|
|
let _ = reader.read_to_end(&mut rest);
|
|
let _ = tx.send(());
|
|
});
|
|
assert!(
|
|
rx.recv_timeout(GONE_WITHIN).is_ok(),
|
|
"a grandchild still holds the pipe after {GONE_WITHIN:?}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn hard_kill_of_the_parent_takes_its_guarded_child() {
|
|
let mut parent = helper("tree-proc").spawn().unwrap();
|
|
let reader = ready(parent.stdout.take().unwrap());
|
|
parent.kill().unwrap();
|
|
parent.wait().unwrap();
|
|
assert_gone(reader);
|
|
}
|
|
|
|
#[test]
|
|
fn hard_kill_works_when_the_parent_is_in_a_group_itself() {
|
|
let mut parent = lutra_proc::spawn(helper("tree-proc")).unwrap();
|
|
let reader = ready(parent.stdout.take().unwrap());
|
|
std::process::Child::kill(&mut parent).unwrap();
|
|
parent.wait().unwrap();
|
|
assert_gone(reader);
|
|
drop(parent);
|
|
}
|
|
|
|
#[test]
|
|
fn kill_takes_the_whole_tree() {
|
|
let mut parent = lutra_proc::spawn(helper("tree-plain")).unwrap();
|
|
let reader = ready(parent.stdout.take().unwrap());
|
|
parent.kill().unwrap();
|
|
parent.wait().unwrap();
|
|
assert_gone(reader);
|
|
}
|
|
|
|
#[test]
|
|
fn drop_takes_the_whole_tree() {
|
|
let mut parent = lutra_proc::spawn(helper("tree-plain")).unwrap();
|
|
let reader = ready(parent.stdout.take().unwrap());
|
|
drop(parent);
|
|
assert_gone(reader);
|
|
}
|
|
|
|
#[test]
|
|
fn a_child_that_exits_reports_its_status() {
|
|
let mut child = lutra_proc::spawn(helper("unknown-role")).unwrap();
|
|
let status = child.wait().unwrap();
|
|
assert_eq!(status.code(), Some(2));
|
|
child.kill().unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn a_missing_program_is_an_error() {
|
|
let error = lutra_proc::spawn(Command::new("lutra-proc-no-such-program")).unwrap_err();
|
|
assert_eq!(error.kind(), std::io::ErrorKind::NotFound);
|
|
}
|