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

@ -18,6 +18,7 @@ test = false
doc = false
[dependencies]
lutra-proc.workspace = true
lutra-text.workspace = true
lutra-vfs.workspace = true
ring = "0.17.14"

View file

@ -20,13 +20,14 @@ use std::ffi::{OsStr, OsString};
use std::io::{BufReader, Write};
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::path::{Path, PathBuf};
use std::process::{Child, ChildStdin, Command, Stdio};
use std::process::{ChildStdin, Command, Stdio};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::mpsc::{self, Receiver, Sender, SyncSender, TrySendError};
use std::sync::{Arc, Mutex, MutexGuard, Weak};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};
use lutra_proc::Child;
use lutra_text::{Buffer, Edit, Position};
use serde_json::{Value, json};
@ -470,7 +471,9 @@ impl Shared {
self.set_status(&mut state, Status::Ready);
}
/// Starts the server process with piped stdio and no console window.
/// Starts the server process with piped stdio and no console window,
/// in a [`lutra_proc`] group, so it and everything it starts die with
/// Lutra even when Lutra is killed.
///
/// # Errors
/// [`LspError::Spawn`] when the executable cannot be started.
@ -490,7 +493,7 @@ impl Shared {
use std::os::windows::process::CommandExt;
command.creation_flags(CREATE_NO_WINDOW);
}
command.spawn().map_err(|error| LspError::Spawn {
lutra_proc::spawn(command).map_err(|error| LspError::Spawn {
command: self.launch.command.to_string_lossy().into_owned(),
detail: error.to_string(),
})
@ -711,11 +714,11 @@ enum Exit {
Crashed(String),
}
/// Kills and reaps a process. Errors are ignored on purpose: the process
/// may have exited already, and there is nothing else to do about it.
/// Kills and reaps a process and everything in its group. Errors are
/// ignored on purpose: the process may have exited already, and there is
/// nothing else to do about it.
fn kill(child: &mut Child) {
let _ = child.kill();
let _ = child.wait();
let _ = child.kill_and_wait();
}
/// Calls a reply outside any lock. A panicking callback is caught so it

View file

@ -188,7 +188,7 @@ pub fn run_version(path: &Path, timeout: Duration) -> Result<Vec<u8>, PhpError>
.stdout(Stdio::piped())
.stderr(Stdio::null());
hide_window(&mut command);
let mut child = command.spawn().map_err(|e| failed(e.to_string()))?;
let mut child = lutra_proc::spawn(command).map_err(|e| failed(e.to_string()))?;
let stdout = child
.stdout
.take()
@ -208,8 +208,7 @@ pub fn run_version(path: &Path, timeout: Duration) -> Result<Vec<u8>, PhpError>
match child.try_wait() {
Ok(Some(status)) => break status,
Ok(None) if Instant::now() >= deadline => {
let _ = child.kill();
let _ = child.wait();
let _ = child.kill_and_wait();
return Err(failed(format!(
"php -v did not finish within {} s",
timeout.as_secs()