F3: suspended Phpactor start, ConPTY shell in a job, cancel superseded completions, syntax proptest

- lutra_proc::spawn_suspended: CREATE_SUSPENDED, join the kill-on-close
  job, resume via a Tool Help thread snapshot. Opt-in for long-lived
  children (Phpactor); git, php -l and the probe keep spawn because the
  snapshot costs ~60 ms per start.
- lutra_proc::adopt + Guard (Windows): the terminal's ConPTY shell joins a
  job, so processes it started end with the tab or with Lutra.
- lsp.completion cancels the previous pending completion on the same
  server ($/cancelRequest); the cancelled command resolves empty.
- Syntax proptests compare leaf kinds, outline and folding instead of the
  S-expression (tree-sitter-php 0.25.1 keeps a union_type wrapper after
  an undo); Malte's seed added.
- docs/development.md: data dirs, target dir inside the repo, parallel
  builds, deleting ui/node_modules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-27 17:28:54 +01:00
parent 7ad3005217
commit ebbc89832c
17 changed files with 468 additions and 36 deletions

View file

@ -13,10 +13,13 @@
//! process prints `ready` and sleeps. A test reading that output sees
//! end of file only once the grandchild is gone too, which is how it
//! checks that no process was left behind.
//! - `tree-on-input`: reads one line from standard input first, then acts
//! as `tree-plain`. A test adopts the helper in between, so the
//! grandchild starts only after the helper is in its job.
//!
//! Any other argument exits with code 2.
use std::io::Write;
use std::io::{BufRead, Write};
use std::process::{Command, ExitCode, Stdio};
use std::thread;
use std::time::Duration;
@ -29,6 +32,13 @@ fn main() -> ExitCode {
let role = std::env::args().nth(1).unwrap_or_default();
match role.as_str() {
"tree-plain" | "tree-proc" => tree(role == "tree-proc"),
"tree-on-input" => {
let mut line = String::new();
if std::io::stdin().lock().read_line(&mut line).is_err() {
return ExitCode::from(3);
}
tree(false)
}
_ => ExitCode::from(2),
}
}

View file

@ -25,10 +25,17 @@
//! by an explicit [`Child::kill`] or drop, which then signal the direct
//! child only.
//!
//! A child starts running before it is put into its job (Windows) or before
//! its watchdog exists (Unix), so a grandchild it starts in that window
//! (microseconds) can escape; language servers and git start their own
//! children much later.
//! A child started by [`spawn`] runs before it is put into its job (Windows)
//! or before its watchdog exists (Unix), so a grandchild it starts in that
//! window (microseconds) can escape. Short-lived tools (git, `php -l`, the
//! PHP probe) accept that: they start their own children (git hooks) much
//! later, when they are in the job already. [`spawn_suspended`] closes the
//! window on Windows for long-lived children (Phpactor) at the cost of a
//! system-wide thread snapshot (about 60 ms per start).
//!
//! [`adopt`] (Windows) puts a process started by someone else, the
//! integrated terminal's ConPTY shell, into a kill-on-close job and returns
//! a [`Guard`] that holds it.
use std::io;
use std::ops::{Deref, DerefMut};
@ -90,10 +97,46 @@ pub struct Child {
/// starting the watchdog). When the group cannot be set up, the child that
/// was already started is killed and reaped before the error is returned,
/// so it never runs unguarded.
pub fn spawn(mut command: Command) -> io::Result<Child> {
Group::prepare(&mut command);
pub fn spawn(command: Command) -> io::Result<Child> {
spawn_with(command, Group::prepare, Group::attach)
}
/// Like [`spawn`], but on Windows the child is created suspended and resumed
/// only once it is in its job, so not even its first instruction runs
/// outside it. Meant for long-lived children: finding the child's primary
/// thread to resume it takes a snapshot of every thread on the machine
/// (about 60 ms). On other platforms it is [`spawn`].
///
/// On Windows the creation flags of `command` are replaced by
/// `CREATE_SUSPENDED | CREATE_NO_WINDOW`.
///
/// # Errors
/// The errors of [`spawn`], and on Windows the error of resuming the child;
/// the child is killed and reaped before an error is returned.
pub fn spawn_suspended(command: Command) -> io::Result<Child> {
#[cfg(windows)]
{
spawn_with(command, Group::prepare_suspended, Group::attach_suspended)
}
#[cfg(not(windows))]
{
spawn(command)
}
}
/// Starts `command` after `prepare` and puts it into the group `attach`
/// creates; kills and reaps the child when `attach` fails.
///
/// # Errors
/// The error of [`Command::spawn`] or of `attach`.
fn spawn_with(
mut command: Command,
prepare: fn(&mut Command),
attach: fn(&std::process::Child) -> io::Result<Group>,
) -> io::Result<Child> {
prepare(&mut command);
let mut inner = command.spawn()?;
match Group::attach(&inner) {
match attach(&inner) {
Ok(group) => Ok(Child { inner, group }),
Err(error) => {
let _ = inner.kill();
@ -103,6 +146,53 @@ pub fn spawn(mut command: Command) -> io::Result<Child> {
}
}
/// A kill-on-close job for a process Lutra did not start through [`spawn`]
/// (Windows only, see [`adopt`]). Dropping it kills the process and
/// everything it started.
#[cfg(windows)]
pub struct Guard {
/// The job object.
group: Group,
}
/// Puts the process behind `process` into a new kill-on-close job, so it
/// and everything it starts from now on die with Lutra or with the returned
/// [`Guard`]. Processes it started before are not adopted.
///
/// `process` needs `PROCESS_SET_QUOTA | PROCESS_TERMINATE` access (a handle
/// from `CreateProcess` has all access) and stays owned by the caller.
///
/// # Errors
/// The OS error of creating the job or assigning the process to it.
#[cfg(windows)]
pub fn adopt(process: std::os::windows::io::RawHandle) -> io::Result<Guard> {
Group::adopt(process).map(|group| Guard { group })
}
#[cfg(windows)]
impl Guard {
/// Terminates every process in the job.
pub fn kill(&mut self) {
self.group.kill();
}
}
#[cfg(windows)]
impl std::fmt::Debug for Guard {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Guard").finish_non_exhaustive()
}
}
#[cfg(windows)]
impl Drop for Guard {
/// Kills the job's processes; closing the job handle right after would
/// do the same.
fn drop(&mut self) {
self.group.kill();
}
}
impl Child {
/// Kills the child and every process in its group, without waiting for
/// the child; call [`std::process::Child::wait`] to reap it.

View file

@ -1,23 +1,46 @@
//! 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.
//! A child started with [`Group::prepare`] runs before it joins its job (a
//! window of microseconds). [`Group::prepare_suspended`] closes that window:
//! the child is created suspended (`CREATE_SUSPENDED`), put into its job and
//! only then resumed. The standard library keeps the primary thread's handle
//! to itself, so the thread is found through a Tool Help snapshot of the
//! system's threads (`CreateToolhelp32Snapshot`, `Thread32First` /
//! `Thread32Next`) and resumed with `OpenThread` + `ResumeThread`. The
//! snapshot covers every thread on the machine and costs about 60 ms
//! (measured 2026-10-27: 43 → 107 ms per `cmd /c exit`), which is why only
//! long-lived children pay for it.
//!
//! [`Group::adopt`] puts a process someone else started (the terminal's
//! ConPTY shell) into a new job by its handle.
//!
//! This is the one module in Lutra that allows `unsafe`: the job object,
//! Tool Help and thread APIs are only reachable through FFI. Every call gets
//! valid arguments by construction: handles come from `CreateJobObjectW`,
//! `CreateToolhelp32Snapshot` or `OpenThread` (owned by an [`OwnedHandle`]
//! right away) or from a live [`std::process::Child`], and the limit and
//! thread entry structures are plain C structs that are valid when zeroed.
#![allow(unsafe_code)]
use std::io;
use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};
use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle, RawHandle};
use std::os::windows::process::CommandExt;
use std::process::Command;
use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE;
use windows_sys::Win32::System::Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, TH32CS_SNAPTHREAD, THREADENTRY32, Thread32First, Thread32Next,
};
use windows_sys::Win32::System::JobObjects::{
AssignProcessToJobObject, CreateJobObjectW, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JobObjectExtendedLimitInformation,
SetInformationJobObject, TerminateJobObject,
};
use windows_sys::Win32::System::Threading::{
CREATE_NO_WINDOW, CREATE_SUSPENDED, OpenThread, ResumeThread, THREAD_SUSPEND_RESUME,
};
/// The exit code a process killed with its job reports.
const KILLED_EXIT_CODE: u32 = 1;
@ -40,6 +63,40 @@ impl Group {
/// The OS error of `CreateJobObjectW`, `SetInformationJobObject` or
/// `AssignProcessToJobObject`.
pub(crate) fn attach(child: &std::process::Child) -> io::Result<Self> {
Self::adopt(child.as_raw_handle())
}
/// Makes the process start suspended and without a console window
/// (`CREATE_SUSPENDED | CREATE_NO_WINDOW`). These replace any creation
/// flags set on `command` before: the standard library cannot read them
/// back, and every tool Lutra runs is a console program whose window
/// must stay hidden.
pub(crate) fn prepare_suspended(command: &mut Command) {
command.creation_flags(CREATE_SUSPENDED | CREATE_NO_WINDOW);
}
/// Creates a kill-on-close job, puts the suspended `child` into it and
/// resumes it.
///
/// # Errors
/// The errors of [`Group::attach`] and of resuming the child (see
/// [`resume`]). The job is closed on error, which kills the child once
/// it is in the job.
pub(crate) fn attach_suspended(child: &std::process::Child) -> io::Result<Self> {
let group = Self::attach(child)?;
resume(child.id())?;
Ok(group)
}
/// Creates a kill-on-close job and puts the process behind `process`
/// into it. The handle needs `PROCESS_SET_QUOTA | PROCESS_TERMINATE`
/// access (a handle from `CreateProcess` has all access) and stays owned
/// by the caller.
///
/// # Errors
/// The OS error of `CreateJobObjectW`, `SetInformationJobObject` or
/// `AssignProcessToJobObject`.
pub(crate) fn adopt(process: RawHandle) -> io::Result<Self> {
let raw = unsafe { CreateJobObjectW(std::ptr::null(), std::ptr::null()) };
if raw.is_null() {
return Err(io::Error::last_os_error());
@ -60,8 +117,7 @@ impl Group {
if set == 0 {
return Err(io::Error::last_os_error());
}
let assigned =
unsafe { AssignProcessToJobObject(job.as_raw_handle(), child.as_raw_handle()) };
let assigned = unsafe { AssignProcessToJobObject(job.as_raw_handle(), process) };
if assigned == 0 {
return Err(io::Error::last_os_error());
}
@ -75,3 +131,52 @@ impl Group {
let _ = unsafe { TerminateJobObject(self.job.as_raw_handle(), KILLED_EXIT_CODE) };
}
}
/// Resumes every thread of the suspended process `pid`. A process created
/// with `CREATE_SUSPENDED` has exactly one thread, its primary thread, with
/// a suspend count of one.
///
/// # Errors
/// The OS error of `CreateToolhelp32Snapshot`, `OpenThread` or
/// `ResumeThread`, or `NotFound` when the snapshot holds no thread of `pid`.
fn resume(pid: u32) -> io::Result<()> {
let raw = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0) };
if raw == INVALID_HANDLE_VALUE || raw.is_null() {
return Err(io::Error::last_os_error());
}
let snapshot = unsafe { OwnedHandle::from_raw_handle(raw) };
let mut entry: THREADENTRY32 = unsafe { std::mem::zeroed() };
entry.dwSize = u32::try_from(std::mem::size_of::<THREADENTRY32>()).map_err(io::Error::other)?;
let mut resumed = 0usize;
let mut more = unsafe { Thread32First(snapshot.as_raw_handle(), &raw mut entry) } != 0;
while more {
if entry.th32OwnerProcessID == pid {
resume_thread(entry.th32ThreadID)?;
resumed += 1;
}
more = unsafe { Thread32Next(snapshot.as_raw_handle(), &raw mut entry) } != 0;
}
if resumed == 0 {
return Err(io::Error::new(
io::ErrorKind::NotFound,
format!("no thread of process {pid} to resume"),
));
}
Ok(())
}
/// Opens thread `tid` and decrements its suspend count once.
///
/// # Errors
/// The OS error of `OpenThread` or `ResumeThread`.
fn resume_thread(tid: u32) -> io::Result<()> {
let raw = unsafe { OpenThread(THREAD_SUSPEND_RESUME, 0, tid) };
if raw.is_null() {
return Err(io::Error::last_os_error());
}
let thread = unsafe { OwnedHandle::from_raw_handle(raw) };
if unsafe { ResumeThread(thread.as_raw_handle()) } == u32::MAX {
return Err(io::Error::last_os_error());
}
Ok(())
}