H1: child processes die with Lutra (crates/proc)

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>
This commit is contained in:
danny 2026-10-25 12:59:57 +01:00
parent e5891ddeb4
commit 90d03284f2
13 changed files with 555 additions and 14 deletions

View file

@ -0,0 +1,77 @@
//! Windows: one job object per child with `JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE`
//! (<https://learn.microsoft.com/windows/win32/procthread/job-objects>).
//!
//! This is the one module in Lutra that allows `unsafe`: the job object API
//! is only reachable through FFI. Every call gets valid arguments by
//! construction: handles come from `CreateJobObjectW` (owned by an
//! [`OwnedHandle`] right away) or from a live [`std::process::Child`], and
//! the limit structure is a plain C struct that is valid when zeroed.
#![allow(unsafe_code)]
use std::io;
use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};
use std::process::Command;
use windows_sys::Win32::System::JobObjects::{
AssignProcessToJobObject, CreateJobObjectW, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JobObjectExtendedLimitInformation,
SetInformationJobObject, TerminateJobObject,
};
/// The exit code a process killed with its job reports.
const KILLED_EXIT_CODE: u32 = 1;
/// The job a child runs in. Lutra holds the only handle; closing it (on
/// drop, or when Lutra's process ends) kills every process in the job.
pub(crate) struct Group {
/// The job object.
job: OwnedHandle,
}
impl Group {
/// Nothing to set on the command: the process joins its job right
/// after it starts.
pub(crate) fn prepare(_command: &mut Command) {}
/// Creates a kill-on-close job and puts `child` into it.
///
/// # Errors
/// The OS error of `CreateJobObjectW`, `SetInformationJobObject` or
/// `AssignProcessToJobObject`.
pub(crate) fn attach(child: &std::process::Child) -> io::Result<Self> {
let raw = unsafe { CreateJobObjectW(std::ptr::null(), std::ptr::null()) };
if raw.is_null() {
return Err(io::Error::last_os_error());
}
let job = unsafe { OwnedHandle::from_raw_handle(raw) };
let mut limits: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = unsafe { std::mem::zeroed() };
limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
let size = u32::try_from(std::mem::size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>())
.map_err(io::Error::other)?;
let set = unsafe {
SetInformationJobObject(
job.as_raw_handle(),
JobObjectExtendedLimitInformation,
std::ptr::from_ref(&limits).cast(),
size,
)
};
if set == 0 {
return Err(io::Error::last_os_error());
}
let assigned =
unsafe { AssignProcessToJobObject(job.as_raw_handle(), child.as_raw_handle()) };
if assigned == 0 {
return Err(io::Error::last_os_error());
}
Ok(Self { job })
}
/// Terminates every process in the job. The result is ignored on
/// purpose: the job may be empty already, and closing the handle on
/// drop kills whatever is left anyway.
pub(crate) fn kill(&mut self) {
let _ = unsafe { TerminateJobObject(self.job.as_raw_handle(), KILLED_EXIT_CODE) };
}
}