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:
parent
7ad3005217
commit
ebbc89832c
17 changed files with 468 additions and 36 deletions
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue