feat(process-daemon): pipe mode on Windows (#63196)

This commit is contained in:
James Long
2026-11-11 11:09:38 -06:00
committed by GitHub
Unverified
parent 2d2d3169b5
commit 15235f7530
10 changed files with 2728 additions and 595 deletions
+2
View File
@@ -27,10 +27,12 @@ sha2 = "0.10"
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.61.2", features = [
"Win32_Foundation",
"Win32_Globalization",
"Win32_Security",
"Win32_Security_Authorization",
"Win32_Storage_FileSystem",
"Win32_System_IO",
"Win32_System_JobObjects",
"Win32_System_Pipes",
"Win32_System_Threading",
"Win32_System_WindowsProgramming",
+52 -29
View File
@@ -134,23 +134,33 @@ unknown id. Both refresh a PTY's foreground process first.
`create_pty { program, args, cwd, env, group_id, title, cols, rows }` starts a
PTY. `create_pipe { program, args, cwd, env, group_id, output_path, metadata }`
starts a pipe process: any command without a terminal. Both return
`Created { process }`. Pipe processes are Unix-only for now; a Windows daemon
answers `create_pipe` with an error.
`Created { process }`. Pipe processes work on Unix and Windows.
- The child gets exactly `env`: the daemon's own environment is not
inherited, and no `TERM` or `COLORTERM` is added. `cwd` must be absolute.
- The child gets its own process group, an empty signal mask with default
actions (the daemon blocks SIGTERM and SIGINT, and std keeps the mask across
exec), stdin on `/dev/null`, and one pipe for both stdout and stderr, so
their bytes stay in order. Nothing parses the output: there is no emulator,
no terminal replies, no OSC title and no foreground process.
- The child gets no terminal: stdin is `/dev/null` on Unix and `NUL` on
Windows, and both stdout and stderr share one pipe, so their bytes stay in
order. Nothing parses the output: there is no emulator, no terminal replies,
no OSC title and no foreground process.
- On Unix the child gets its own process group and an empty signal mask with
default actions (the daemon blocks SIGTERM and SIGINT, and std keeps the
mask across exec). On Windows it is created with `CREATE_NO_WINDOW` (no
console window and no console) and is placed in a kill-on-close job from
creation. The program is resolved the way `CreateProcessW` resolves a bare
executable name (the application directory, the current directory, the
system directories and `PATH`); the daemon does not run a shell unless the
request itself asks for one, and passing `["/c", command]` to `cmd.exe` is
the caller's job.
- `output_path` must be absolute. The daemon opens it before spawning,
truncating an existing file or creating one with mode `0666` minus the
daemon's umask, and refuses a symlink as the final path component. The
request is validated (program, absolute paths, metadata size) before the
file is opened, so such a rejected request never creates or truncates it; a
path that cannot be opened fails the request without starting the process.
A missing program or `cwd` fails at spawn, after the file was opened.
truncating an existing file or creating one (mode `0666` minus the daemon's
umask on Unix), and refuses a symlink or other reparse point as the final
path component. The request is validated (program, absolute paths, metadata
size) before the file is opened, so such a rejected request never creates or
truncates it; a path that cannot be opened fails the request without
starting the process. A missing program or `cwd` fails at spawn, after the
file was opened. The file is opened for reading, writing and deletion by
other handles, so a reader can follow it and the owner can delete it while
the process still runs.
- The reader writes every output byte to the file before handing it to the
actor. The file is the only copy of the output: a pipe process has no replay
buffer, so `replay` and `subscribe` return an empty replay (with correct
@@ -162,18 +172,22 @@ answers `create_pipe` with an error.
- Exit is published once both the exit status and the end of output are known.
A descendant that keeps the pipe open (`cmd &`, dev servers) delays that by
at most one second after the child exits; the reader then closes the pipe,
so leftover writers get `SIGPIPE`/`EPIPE`. This is deliberate: the process
is reported as finished, and nothing reads what comes later. The file is
closed before the exit is published.
so leftover writers get `SIGPIPE`/`EPIPE` on Unix and a broken pipe on
Windows. This is deliberate: the process is reported as finished, and
nothing reads what comes later. The file is closed before the exit is
published.
- `Exited` and the lifecycle carry `signal`, the signal's name such as
`"SIGTERM"`, when the process was killed by one; `exit_code` is then null.
`signal` is left out when there is none.
- An exited pipe process stays listed, as a zombie, until `terminate`. The
zombie keeps its PID and process group ID reserved, so killing the group can
never reach an unrelated process. `terminate` sends SIGKILL to the group and
to the child, reaps it and removes the process; it never waits on a
descendant that left the group and still holds the pipe. The daemon never
deletes output files.
`"SIGTERM"`, when the daemon terminated the process; `exit_code` is then
null. `signal` is left out when there is none.
- On Unix an exited pipe process stays listed, as a zombie, until `terminate`.
The zombie keeps its PID and process group ID reserved, so killing the group
can never reach an unrelated process. On Windows the process object and job
handle stay open until `terminate`, so the PID is not reused either.
`terminate` kills the whole tree (SIGKILL on Unix; `TerminateJobObject`, or
the main process only when no job could be used, on Windows), reaps it and
removes the process; it never waits on a descendant that left the process
group or job and still holds the pipe. The daemon never deletes output
files.
### Line count
@@ -205,11 +219,20 @@ only; left out for PTYs), `output_error` (left out when there is none) and
`exited_at`. PTY subscriptions send the same response.
`signal { id, signal }` (pipe only; `"SIGTERM"`, `"SIGINT"` or `"SIGKILL"`)
signals the process group and the child, and returns `Ok`. Unlike
`terminate`, it keeps the process: `wait` then gets a normal `Exited` with
`signal`. Signalling an exited process is allowed and reaches whatever is left
of its group (an empty group is not an error); after `terminate` the process
is gone and `signal` fails.
signals the process tree and returns `Ok`. On Unix the signal goes to the
process group and the child, as a real signal. **On Windows every signal is a
hard kill** of the job (or of the main process when no job could be used) with
`TerminateJobObject`/`TerminateProcess`: there is no console and no console
control event, so SIGTERM and SIGINT cannot be caught or trapped, and the exit
reports the signal that was sent, not a platform exit code. Unlike
`terminate`, `signal` keeps the process: `wait` then gets a normal `Exited`
with `signal`. Signalling an exited process is allowed and reaches whatever is
left of its group or job (an empty group is not an error); after `terminate`
the process is gone and `signal` fails. If an outer job refuses the child
(`ERROR_ACCESS_DENIED`, for example a job with UI restrictions), the daemon
runs the command anyway without a job, logs a warning once, and kills only the
main process with `TerminateProcess`; descendants then survive a signal or
terminate.
`metadata` on `create_pipe` is an opaque JSON object the daemon stores and
returns as `pipe.metadata` without interpreting it. `set_metadata { id,
+1 -2
View File
@@ -52,8 +52,7 @@ pub enum Request {
cols: u16,
rows: u16,
},
/// Unix only. The child gets exactly `env`; `cwd` and `output_path` must
/// be absolute.
/// The child gets exactly `env`; `cwd` and `output_path` must be absolute.
CreatePipe {
program: String,
args: Vec<String>,
+31 -39
View File
@@ -17,7 +17,6 @@ use serde::{Deserialize, Serialize};
use crate::ghostty::{Format, Terminal, TerminalOptions};
use crate::protocol::AttachmentRole;
#[cfg(unix)]
mod pipe;
#[cfg(windows)]
mod windows;
@@ -311,7 +310,7 @@ impl TerminalService {
self.insert(|id, order| TerminalHandle::spawn_pty(id, order, request, replay_capacity))
}
/// Starts a pipe process. Unix only.
/// Starts a pipe process.
pub fn create_pipe(&self, request: CreatePipe) -> Result<ProcessInfo> {
// Everything is checked before the output file is opened, so a
// rejected request never creates or truncates it.
@@ -323,10 +322,7 @@ impl TerminalService {
bail!("output_path must be absolute");
}
validate_metadata(&request.metadata)?;
#[cfg(unix)]
return self.insert(|id, order| TerminalHandle::spawn_pipe(id, order, request));
#[cfg(not(unix))]
bail!("pipe processes are not supported on this platform");
self.insert(|id, order| TerminalHandle::spawn_pipe(id, order, request))
}
fn insert(
@@ -399,18 +395,11 @@ impl TerminalService {
/// Signals a pipe process and its process group. The process stays
/// listed; signalling one that already exited is allowed.
pub fn signal(&self, id: TerminalId, signal: ProcessSignal) -> Result<()> {
let process = self.pipe(id)?;
#[cfg(unix)]
return process
self.pipe(id)?
.process
.as_ref()
.ok_or_else(|| anyhow!("process {id} cannot be signalled"))?
.signal(signal);
#[cfg(not(unix))]
{
let _ = (process, signal);
bail!("pipe processes are not supported on this platform");
}
.signal(signal)
}
/// Replaces a pipe process's metadata.
@@ -644,7 +633,6 @@ struct TerminalHandle {
/// Runs after the joins; reaps a pipe process kept as a zombie until now.
reap: Mutex<Option<Box<dyn FnOnce() + Send>>>,
/// Pipe only: the child, for `signal`.
#[cfg(unix)]
process: Option<Arc<pipe::Process>>,
info: Arc<RwLock<ProcessInfo>>,
joins: Mutex<Option<Vec<JoinHandle<()>>>>,
@@ -789,7 +777,7 @@ impl TerminalHandle {
ActorMaster {
pty: Some(pair.master),
#[cfg(windows)]
close_tx,
close_tx: Some(close_tx),
},
writer_tx,
&info,
@@ -880,7 +868,6 @@ impl TerminalHandle {
killer: Mutex::new(killer),
cancel: Mutex::new(None),
reap: Mutex::new(None),
#[cfg(unix)]
process: None,
info,
joins: Mutex::new(Some(vec![
@@ -1017,10 +1004,8 @@ enum ActorMessage {
WriterFailed(String),
ChildExited(Result<Option<u32>, String>),
/// A pipe process ended by a signal; the value is the signal's name.
#[cfg(unix)]
ChildKilled(String),
/// Writing the `output_path` file failed; it stops at this point.
#[cfg(unix)]
OutputFileFailed(String),
#[cfg(not(windows))]
RefreshForegroundProcess {
@@ -1111,10 +1096,19 @@ enum WriterMessage {
struct ActorMaster {
pty: Option<Box<dyn MasterPty + Send>>,
#[cfg(windows)]
close_tx: Sender<Box<dyn MasterPty + Send>>,
close_tx: Option<Sender<Box<dyn MasterPty + Send>>>,
}
impl ActorMaster {
/// A master for a process with no terminal.
fn pipe() -> Self {
Self {
pty: None,
#[cfg(windows)]
close_tx: None,
}
}
fn get(&self) -> Result<&(dyn MasterPty + Send)> {
self.pty
.as_deref()
@@ -1123,15 +1117,20 @@ impl ActorMaster {
#[cfg(windows)]
fn close(&mut self) -> Result<()> {
if let Some(master) = self.pty.take() {
// A capacity-one channel receives this unique master exactly once;
// handoff cannot wait on ClosePseudoConsole or PTY output drainage.
if let Err(error) = self.close_tx.send(master) {
// Keep ownership on failure. The actor stops receiving before
// dropping this fallback master, so its reader can still drain.
self.pty = Some(error.0);
bail!("terminal close worker stopped");
}
let Some(master) = self.pty.take() else {
return Ok(());
};
let Some(close_tx) = &self.close_tx else {
// Only PTYs have a close worker; a pipe process has no master.
return Ok(());
};
// A capacity-one channel receives this unique master exactly once;
// handoff cannot wait on ClosePseudoConsole or PTY output drainage.
if let Err(error) = close_tx.send(master) {
// Keep ownership on failure. The actor stops receiving before
// dropping this fallback master, so its reader can still drain.
self.pty = Some(error.0);
bail!("terminal close worker stopped");
}
Ok(())
}
@@ -1544,7 +1543,6 @@ fn run_actor(config: ActorConfig) -> Result<()> {
}
None => waiters.push(reply),
},
#[cfg(unix)]
Ok(ActorMessage::OutputFileFailed(message)) => {
if let Ok(mut value) = info.write()
&& let Some(pipe) = value.pipe.as_mut()
@@ -1552,7 +1550,6 @@ fn run_actor(config: ActorConfig) -> Result<()> {
pipe.output_error = Some(message);
}
}
#[cfg(unix)]
Ok(ActorMessage::ChildKilled(signal)) => {
child_exit = Some(Ok((None, Some(signal))));
}
@@ -2250,7 +2247,7 @@ mod tests {
.is_pty()
.then(|| Box::new(TestMaster) as Box<dyn MasterPty + Send>),
#[cfg(windows)]
close_tx,
close_tx: mode.is_pty().then_some(close_tx),
},
messages,
writes,
@@ -2274,7 +2271,6 @@ mod tests {
killer: Mutex::new(Box::new(TestKiller)),
cancel: Mutex::new(None),
reap: Mutex::new(None),
#[cfg(unix)]
process: None,
info,
joins: Mutex::new(Some(vec![
@@ -2428,17 +2424,13 @@ mod tests {
for message in [
ActorMessage::Wait { reply },
ActorMessage::ChildExited(Ok(Some(0))),
#[cfg(unix)]
ActorMessage::OutputFileFailed("disk full".into()),
ActorMessage::ReaderFailed("boom".into()),
] {
actor.actor_tx.send(message).unwrap();
}
let exit = wait.recv_timeout(Duration::from_secs(5)).unwrap();
#[cfg(unix)]
assert_eq!(exit.output_error.as_deref(), Some("disk full"));
#[cfg(not(unix))]
assert!(exit.output_error.is_some());
actor.shutdown().unwrap();
}
@@ -2507,7 +2499,7 @@ mod tests {
drop(close_rx);
let mut master = ActorMaster {
pty: Some(Box::new(TestMaster)),
close_tx,
close_tx: Some(close_tx),
};
assert!(master.close().is_err());
assert!(master.get().is_ok());
+226 -523
View File
@@ -1,343 +1,258 @@
//! Non-PTY ("pipe") processes on Unix.
//! Non-PTY ("pipe") processes: a command with no terminal, stdin closed and
//! stdout and stderr sharing one pipe so their bytes keep their order.
//!
//! The child runs in its own process group with stdin on `/dev/null` and
//! stdout and stderr sharing one pipe, so their bytes keep their order. Three
//! choices keep the daemon safe from what the child leaves behind:
//! The platform modules own the child, its process tree and the output pipe:
//! Unix keeps a zombie so the PID and process group stay reserved, and Windows
//! keeps the process and its kill-on-close job. Everything the two must do the
//! same way lives here:
//!
//! - The waiter observes the exit with `WNOWAIT` and leaves the child as a
//! zombie until shutdown. While the zombie exists, its PID and process group
//! ID cannot be reused, so killing the group can never hit an unrelated
//! process, even long after the exit.
//! - The reader writes every byte to the output file before handing it to the
//! actor, which keeps no replay buffer: the file is the only copy of the
//! output.
//! - A descendant can keep the pipe open after the child exits (`cmd &`, build
//! servers). The reader stops [`DRAIN_AFTER_EXIT`] after the exit instead of
//! waiting for an EOF that may never come.
//! - The reader writes every byte to the output file before handing it to the
//! actor, which keeps no replay buffer: the file is the only copy of the
//! output. Leftover writers get `SIGPIPE` once the reader stops.
//! - The reader polls a cancel pipe too, so shutdown never waits on a pipe that
//! a descendant which left the process group still holds open.
//! - The file is closed before `ReaderEof` or `ReaderFailed`, so whoever
//! observes the exit can read everything the file will ever get.
//! - A process the daemon terminated is reported as the signal the daemon sent
//! (`SIGTERM`, `SIGINT`, `SIGKILL`) with no exit code, not as the platform's
//! hard-kill status, so clients format it like a Unix signal death.
use std::fs::{File, OpenOptions};
use std::io::{self, PipeReader, PipeWriter, Read, Write};
use std::os::fd::AsFd;
use std::os::unix::fs::OpenOptionsExt;
use std::os::unix::process::CommandExt;
use std::process::{Command, Stdio};
use std::sync::{Arc, Mutex, PoisonError, RwLock};
use std::thread;
use std::time::{Duration, Instant};
use std::fs::File;
use std::io::{self, Write};
use std::sync::{Arc, Mutex, RwLock};
use std::thread::JoinHandle;
use std::time::Duration;
use anyhow::{Context, Result, bail};
use anyhow::{Context, Result};
use crossbeam_channel::{Sender, bounded};
use nix::errno::Errno;
use nix::poll::{PollFd, PollFlags, PollTimeout, poll};
use portable_pty::ChildKiller;
use super::{
ActorMaster, ActorMessage, CreatePipe, PipeInfo, ProcessInfo, ProcessMode, ProcessSignal,
use crate::service::{
ActorMaster, ActorMessage, ActorStart, CreatePipe, PipeInfo, ProcessInfo, ProcessMode,
TerminalHandle, TerminalId, new_info, start_actor,
};
#[cfg(unix)]
mod unix;
#[cfg(windows)]
mod windows;
#[cfg(unix)]
pub(in crate::service) use unix::Process;
#[cfg(windows)]
pub(in crate::service) use windows::Process;
/// How long output is still read after the child exits.
const DRAIN_AFTER_EXIT: Duration = Duration::from_secs(1);
/// Serializes pipe creation against `spawn`. macOS has no `pipe2`, so std
/// creates a pipe and then marks it close-on-exec; a fork in between would
/// leak the pipe into another child, where it could hold output or cancel
/// pipes open. Pipes are created under the read lock, children spawned under
/// the write lock.
static SPAWN_LOCK: RwLock<()> = RwLock::new(());
struct Pipes {
output: PipeReader,
output_writer: PipeWriter,
stderr: PipeWriter,
cancel: PipeReader,
cancel_writer: PipeWriter,
exited: PipeReader,
exited_writer: PipeWriter,
/// The output file and the actor queue, in the order every byte goes through
/// them: the file first, so it always holds at least what subscribers have
/// seen, even when the actor is backpressured.
struct OutputSink {
file: Option<File>,
actor: Sender<ActorMessage>,
}
impl Pipes {
fn new() -> Result<Self> {
let _guard = SPAWN_LOCK.read().unwrap_or_else(PoisonError::into_inner);
let (output, output_writer) = io::pipe().context("failed to create output pipe")?;
let stderr = output_writer
.try_clone()
.context("failed to share output pipe")?;
let (cancel, cancel_writer) = io::pipe().context("failed to create cancel pipe")?;
let (exited, exited_writer) = io::pipe().context("failed to create exit pipe")?;
Ok(Self {
output,
output_writer,
stderr,
cancel,
cancel_writer,
exited,
exited_writer,
})
impl OutputSink {
fn new(file: File, actor: Sender<ActorMessage>) -> Self {
Self {
file: Some(file),
actor,
}
}
/// Returns false once the actor is gone.
fn write(&mut self, bytes: &[u8]) -> bool {
let mut newlines = 0;
if let Some(file) = &mut self.file
&& let Err(error) = write_counting(file, bytes, &mut newlines)
{
// Ordered before this chunk's output, so the error is visible no
// later than the bytes the file lacks.
self.file = None;
if self
.actor
.send(ActorMessage::OutputFileFailed(format!(
"failed to write output file: {error}"
)))
.is_err()
{
return false;
}
}
self.actor
.send(ActorMessage::Output {
bytes: bytes.to_vec(),
newlines,
})
.is_ok()
}
/// Closes the file before anyone can observe the exit.
fn eof(mut self) {
self.file = None;
let _ = self.actor.send(ActorMessage::ReaderEof);
}
/// Closes the file before anyone can observe the failure.
fn failed(mut self, message: String) {
self.file = None;
let _ = self.actor.send(ActorMessage::ReaderFailed(message));
}
}
impl TerminalHandle {
pub(super) fn spawn_pipe(id: TerminalId, order: u64, request: CreatePipe) -> Result<Self> {
let Pipes {
output,
output_writer,
stderr,
cancel,
cancel_writer,
exited,
exited_writer,
} = Pipes::new()?;
// Before spawning, so a bad path fails the request with nothing to clean up.
// The reader is the file's only writer, so it needs no append mode.
let sink = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
// Never write through a symlink planted at the final component.
.custom_flags(libc::O_NOFOLLOW)
.open(&request.output_path)
.with_context(|| {
format!(
"failed to open output file {}",
request.output_path.display()
)
})?;
let mut command = Command::new(&request.program);
command
.args(&request.args)
.current_dir(&request.cwd)
.env_clear()
.envs(&request.env)
.stdin(Stdio::null())
.stdout(output_writer)
.stderr(stderr)
.process_group(0);
// SAFETY: only async-signal-safe calls run between fork and exec.
unsafe {
command.pre_exec(reset_signals);
}
let child = {
let _guard = SPAWN_LOCK.write().unwrap_or_else(PoisonError::into_inner);
command
.spawn()
.with_context(|| format!("failed to spawn {}", request.program))?
};
// Close the daemon's copies of the write end, or EOF never arrives.
drop(command);
let process = Arc::new(Process {
pid: child.id(),
reaped: Mutex::new(false),
});
// Dropping `Child` neither waits nor kills; `Process` reaps it.
drop(child);
/// How a pipe child ended.
enum Exit {
Code(u32),
/// The daemon terminated the process; the value is the signal it sent.
Signal(String),
}
let info = Arc::new(RwLock::new(ProcessInfo {
pipe: Some(PipeInfo {
output_path: request.output_path.clone(),
output_lines: 0,
output_error: None,
metadata: request.metadata,
}),
..new_info(
id,
ProcessMode::Pipe,
Some(process.pid),
&request.program,
&request.args,
&request.cwd,
&request.group_id,
)
}));
// Nothing is ever written to a pipe process; a disconnected queue
// turns any stray write into an error.
let (writes, _) = bounded(1);
let actor = match start_actor(
/// The one place a pipe exit becomes an actor message, so both platforms
/// report exits and signals the same way.
fn exit_message(result: io::Result<Exit>) -> ActorMessage {
match result {
Ok(Exit::Code(code)) => ActorMessage::ChildExited(Ok(Some(code))),
Ok(Exit::Signal(name)) => ActorMessage::ChildKilled(name),
Err(error) => ActorMessage::ChildExited(Err(error.to_string())),
}
}
/// Info for a pipe process: exactly one `pipe` group, never a `pty`.
fn new_pipe_info(id: TerminalId, pid: u32, request: CreatePipe) -> ProcessInfo {
let CreatePipe {
program,
args,
cwd,
env: _,
group_id,
output_path,
metadata,
} = request;
ProcessInfo {
pipe: Some(PipeInfo {
output_path,
output_lines: 0,
output_error: None,
metadata,
}),
..new_info(
id,
ProcessMode::Pipe,
ActorMaster { pty: None },
writes,
&info,
// The output file is the only buffer.
0,
(0, 0),
) {
Ok(actor) => actor,
Err(error) => {
process.kill();
process.reap();
return Err(error);
}
};
let mut joins = vec![actor.thread];
let reader_actor_tx = actor.actor_tx.clone();
let started = thread::Builder::new()
.name(format!("opencode-pty-reader-{id}"))
.spawn(move || read_output(output, sink, cancel, exited, reader_actor_tx))
.context("failed to spawn terminal reader")
.and_then(|reader| {
joins.push(reader);
let wait_actor_tx = actor.actor_tx.clone();
let pid = process.pid;
// Spawned last: the waiter must be joined before `reap`.
thread::Builder::new()
.name(format!("opencode-pty-wait-{id}"))
.spawn(move || {
let message = match wait_exit(pid) {
Ok(Exit::Code(code)) => ActorMessage::ChildExited(Ok(Some(code))),
Ok(Exit::Signal(name)) => ActorMessage::ChildKilled(name),
Err(error) => ActorMessage::ChildExited(Err(error.to_string())),
};
let _ = wait_actor_tx.send(message);
// Starts the reader's drain deadline.
drop(exited_writer);
})
.context("failed to spawn terminal child waiter")
});
match started {
Ok(waiter) => joins.push(waiter),
Err(error) => {
// Stop what did start, kill the child before any join can wait
// on it, and reap only after every thread is gone.
drop(actor.shutdown_tx);
drop(cancel_writer);
process.kill();
for join in joins {
let _ = join.join();
}
process.reap();
return Err(error);
}
}
let reap = Arc::clone(&process);
Ok(Self {
order,
mode: ProcessMode::Pipe,
actor_tx: actor.actor_tx,
actor_done: actor.actor_done,
shutdown_tx: Mutex::new(Some(actor.shutdown_tx)),
killer: Mutex::new(Box::new(GroupKiller(Arc::clone(&process)))),
cancel: Mutex::new(Some(Box::new(cancel_writer))),
reap: Mutex::new(Some(Box::new(move || reap.reap()))),
process: Some(process),
info,
joins: Mutex::new(Some(joins)),
})
Some(pid),
&program,
&args,
&cwd,
&group_id,
)
}
}
/// Runs in the child between fork and exec. The daemon blocks SIGTERM and
/// SIGINT on all its threads, and std keeps the signal mask across exec, so
/// clear it, and give every signal portable-pty also resets its default action.
fn reset_signals() -> io::Result<()> {
// SAFETY: sigemptyset, sigprocmask and signal are async-signal-safe.
unsafe {
// Dispositions first, so no inherited handler can run once unmasked.
for signal in [
libc::SIGCHLD,
libc::SIGHUP,
libc::SIGINT,
libc::SIGQUIT,
libc::SIGTERM,
libc::SIGALRM,
] {
libc::signal(signal, libc::SIG_DFL);
}
let mut empty: libc::sigset_t = std::mem::zeroed();
if libc::sigemptyset(&mut empty) != 0
|| libc::sigprocmask(libc::SIG_SETMASK, &empty, std::ptr::null_mut()) != 0
{
return Err(io::Error::last_os_error());
}
}
Ok(())
/// The platform reader and waiter, each spawning its named worker thread.
struct Threads {
reader: Box<dyn FnOnce(OutputSink) -> io::Result<JoinHandle<()>>>,
waiter: Box<dyn FnOnce(Sender<ActorMessage>) -> io::Result<JoinHandle<()>>>,
}
/// A pipe child that stays a zombie after it exits, until `reap`.
/// Registers a spawned pipe process with its actor, then runs the platform's
/// reader and waiter. Both are started in that order, and the waiter last, so
/// it is joined before the process is reaped.
fn start_pipe(
id: TerminalId,
order: u64,
request: CreatePipe,
process: Arc<Process>,
sink: File,
cancel: Box<dyn Send>,
threads: Threads,
) -> Result<TerminalHandle> {
let info = Arc::new(RwLock::new(new_pipe_info(id, process.pid(), request)));
// Nothing is ever written to a pipe process; a disconnected queue
// turns any stray write into an error.
let (writes, _) = bounded(1);
let actor = match start_actor(
id,
ProcessMode::Pipe,
ActorMaster::pipe(),
writes,
&info,
// The output file is the only buffer.
0,
(0, 0),
) {
Ok(actor) => actor,
Err(error) => {
process.kill();
process.reap();
return Err(error);
}
};
let ActorStart {
actor_tx,
actor_done,
shutdown_tx,
shutdown: _,
thread,
} = actor;
let mut joins = vec![thread];
let reader = (threads.reader)(OutputSink::new(sink, actor_tx.clone()));
let reader = match reader {
Ok(reader) => reader,
Err(error) => {
stop(shutdown_tx, cancel, &process, joins);
return Err(error).context("failed to spawn terminal reader");
}
};
joins.push(reader);
let waiter = (threads.waiter)(actor_tx.clone());
let waiter = match waiter {
Ok(waiter) => waiter,
Err(error) => {
stop(shutdown_tx, cancel, &process, joins);
return Err(error).context("failed to spawn terminal child waiter");
}
};
joins.push(waiter);
let reap = Arc::clone(&process);
Ok(TerminalHandle {
order,
mode: ProcessMode::Pipe,
actor_tx,
actor_done,
shutdown_tx: Mutex::new(Some(shutdown_tx)),
killer: Mutex::new(Box::new(ProcessKiller(Arc::clone(&process)))),
cancel: Mutex::new(Some(cancel)),
reap: Mutex::new(Some(Box::new(move || reap.reap()))),
process: Some(process),
info,
joins: Mutex::new(Some(joins)),
})
}
/// Stops a partially started pipe process: kill the child before any join can
/// wait on it, and reap only after every thread is gone.
fn stop(
shutdown_tx: Sender<()>,
cancel: Box<dyn Send>,
process: &Process,
joins: Vec<JoinHandle<()>>,
) {
drop(shutdown_tx);
drop(cancel);
process.kill();
for join in joins {
let _ = join.join();
}
process.reap();
}
/// `ChildKiller` for service shutdown, which uses the same kill path as
/// `terminate`.
#[derive(Debug)]
pub(super) struct Process {
pid: u32,
reaped: Mutex<bool>,
}
struct ProcessKiller(Arc<Process>);
impl Process {
/// Kills the whole process group. Never signals after `reap`, when the
/// group ID may belong to someone else.
fn kill(&self) {
let reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner);
if !*reaped && let Ok(pid) = i32::try_from(self.pid) {
// SAFETY: the unreaped child keeps its PID and group ID reserved.
// Signal the child directly too, in case it moved to another group.
unsafe {
libc::killpg(pid, libc::SIGKILL);
libc::kill(pid, libc::SIGKILL);
}
}
}
/// Sends `signal` to the process group and the child. Allowed after the
/// exit, to reach what is left of the group; refused after `reap`.
pub(super) fn signal(&self, signal: ProcessSignal) -> Result<()> {
let signal = match signal {
ProcessSignal::Term => libc::SIGTERM,
ProcessSignal::Int => libc::SIGINT,
ProcessSignal::Kill => libc::SIGKILL,
};
let reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner);
if *reaped {
bail!("process has been reaped");
}
let pid = i32::try_from(self.pid).context("invalid pid")?;
// SAFETY: the unreaped child keeps its PID and group ID reserved.
check_signal(unsafe { libc::killpg(pid, signal) }, self.pid)?;
// Signal the child directly too, in case it moved to another group.
// SAFETY: as above.
check_signal(unsafe { libc::kill(pid, signal) }, self.pid)
}
/// Call only after the waiter has returned: kill the group first, so the
/// child has exited and this does not block.
fn reap(&self) {
let mut reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner);
if *reaped {
return;
}
let Ok(pid) = i32::try_from(self.pid) else {
return;
};
loop {
// SAFETY: waits only for this daemon's own child.
let result = unsafe { libc::waitpid(pid, std::ptr::null_mut(), 0) };
if result != -1 || Errno::last() != Errno::EINTR {
break;
}
}
*reaped = true;
}
}
/// Reads errno right after a `kill`/`killpg` result. A zombie group with no
/// live members gives ESRCH on macOS, which is not an error here.
/// Signalling an exited child whose group is empty fails with `ESRCH`, or
/// with `EPERM` on macOS (the zombie is still there); neither is an error.
fn check_signal(result: libc::c_int, pid: u32) -> Result<()> {
if result == -1 {
let errno = Errno::last();
if errno != Errno::ESRCH && !(errno == Errno::EPERM && has_exited(pid)) {
return Err(io::Error::from(errno)).context("failed to signal process");
}
}
Ok(())
}
#[derive(Debug)]
struct GroupKiller(Arc<Process>);
impl ChildKiller for GroupKiller {
impl ChildKiller for ProcessKiller {
fn kill(&mut self) -> io::Result<()> {
self.0.kill();
Ok(())
@@ -348,218 +263,6 @@ impl ChildKiller for GroupKiller {
}
}
enum Exit {
Code(u32),
Signal(String),
}
/// Whether the child has exited, without reaping it.
fn has_exited(pid: u32) -> bool {
// SAFETY: siginfo_t is plain data, and all-zero is a valid value.
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
// SAFETY: `info` is a valid out pointer; WNOWAIT leaves the zombie.
let result = unsafe {
libc::waitid(
libc::P_PID,
pid as libc::id_t,
&mut info,
libc::WEXITED | libc::WNOWAIT | libc::WNOHANG,
)
};
// SAFETY: waitid filled `info`; a zero PID means the child is still running.
result == 0 && unsafe { info.si_pid() } != 0
}
/// Waits for the child to exit without reaping it.
fn wait_exit(pid: u32) -> io::Result<Exit> {
loop {
// SAFETY: siginfo_t is plain data, and all-zero is a valid value.
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
// SAFETY: `info` is a valid out pointer; WNOWAIT leaves the zombie.
let result = unsafe {
libc::waitid(
libc::P_PID,
pid as libc::id_t,
&mut info,
libc::WEXITED | libc::WNOWAIT,
)
};
if result == -1 {
let error = io::Error::last_os_error();
if error.kind() == io::ErrorKind::Interrupted {
continue;
}
return Err(error);
}
// SAFETY: waitid filled `info` for a SIGCHLD event.
let status = unsafe { info.si_status() };
return Ok(match info.si_code {
libc::CLD_EXITED => Exit::Code(status as u32),
_ => Exit::Signal(signal_name(status)),
});
}
}
fn signal_name(signal: i32) -> String {
let name = match signal {
libc::SIGHUP => "SIGHUP",
libc::SIGINT => "SIGINT",
libc::SIGQUIT => "SIGQUIT",
libc::SIGILL => "SIGILL",
libc::SIGTRAP => "SIGTRAP",
libc::SIGABRT => "SIGABRT",
libc::SIGBUS => "SIGBUS",
libc::SIGFPE => "SIGFPE",
libc::SIGKILL => "SIGKILL",
libc::SIGUSR1 => "SIGUSR1",
libc::SIGSEGV => "SIGSEGV",
libc::SIGUSR2 => "SIGUSR2",
libc::SIGPIPE => "SIGPIPE",
libc::SIGALRM => "SIGALRM",
libc::SIGTERM => "SIGTERM",
libc::SIGXCPU => "SIGXCPU",
libc::SIGXFSZ => "SIGXFSZ",
libc::SIGVTALRM => "SIGVTALRM",
libc::SIGPROF => "SIGPROF",
libc::SIGSYS => "SIGSYS",
_ => return format!("SIG{signal}"),
};
name.to_string()
}
/// Forwards output until EOF, until [`DRAIN_AFTER_EXIT`] after `exited`
/// closes, or until `cancel` closes. Once every writer has closed the output
/// pipe, what is left is finite, so it is read to the end regardless of the
/// deadline.
fn read_output(
output: PipeReader,
sink: File,
cancel: PipeReader,
exited: PipeReader,
actor: Sender<ActorMessage>,
) {
let mut output = output;
// Dropped after a write error: the rest is forwarded but not counted.
let mut sink = Some(sink);
let mut buffer = [0_u8; 8192];
let mut deadline = None::<Instant>;
let mut writers_closed = false;
loop {
let timeout = match deadline {
Some(deadline) if !writers_closed => {
let left = deadline.saturating_duration_since(Instant::now());
if left.is_zero() {
// The reader may have been stalled past the deadline; take
// one last look before giving up on a held-open pipe.
let mut fds = [PollFd::new(output.as_fd(), PollFlags::POLLIN)];
let hung_up = poll(&mut fds, PollTimeout::ZERO).is_ok_and(|count| count > 0)
&& fds[0]
.revents()
.is_some_and(|events| events.contains(PollFlags::POLLHUP));
if !hung_up {
// Close the file before anyone can observe the exit.
drop(sink);
let _ = actor.send(ActorMessage::ReaderEof);
return;
}
writers_closed = true;
PollTimeout::NONE
} else {
// Round up: a sub-millisecond remainder must not become a busy 0.
let millis = left.as_nanos().div_ceil(1_000_000).min(i32::MAX as u128);
PollTimeout::try_from(millis).unwrap_or(PollTimeout::MAX)
}
}
_ => PollTimeout::NONE,
};
let mut fds = [
PollFd::new(output.as_fd(), PollFlags::POLLIN),
PollFd::new(cancel.as_fd(), PollFlags::POLLIN),
PollFd::new(exited.as_fd(), PollFlags::POLLIN),
];
// Once the exit is seen, stop polling its closed pipe.
let count = if deadline.is_some() { 2 } else { 3 };
match poll(&mut fds[..count], timeout) {
Err(Errno::EINTR) | Ok(0) => continue,
Err(error) => {
// Close the file before anyone can observe the exit.
drop(sink);
let _ = actor.send(ActorMessage::ReaderFailed(error.to_string()));
return;
}
Ok(_) => {}
}
let ready = |fd: &PollFd| {
fd.revents().is_none_or(|events| {
events.intersects(
PollFlags::POLLIN
| PollFlags::POLLHUP
| PollFlags::POLLERR
| PollFlags::POLLNVAL,
)
})
};
if ready(&fds[1]) {
return;
}
if count == 3 && ready(&fds[2]) {
deadline = Some(Instant::now() + DRAIN_AFTER_EXIT);
}
if fds[0]
.revents()
.is_some_and(|events| events.contains(PollFlags::POLLHUP))
{
writers_closed = true;
}
if ready(&fds[0]) {
match output.read(&mut buffer) {
Ok(0) => {
// Close the file before anyone can observe the exit.
drop(sink);
let _ = actor.send(ActorMessage::ReaderEof);
return;
}
Ok(length) => {
// The file first, so it always holds at least what
// subscribers have seen, even if the actor is backpressured.
let mut newlines = 0;
if let Some(file) = &mut sink
&& let Err(error) =
write_counting(&mut *file, &buffer[..length], &mut newlines)
{
// Ordered before this chunk's output, so the error is
// visible no later than the bytes the file lacks.
sink = None;
if actor
.send(ActorMessage::OutputFileFailed(format!(
"failed to write output file: {error}"
)))
.is_err()
{
return;
}
}
if actor
.send(ActorMessage::Output {
bytes: buffer[..length].to_vec(),
newlines,
})
.is_err()
{
return;
}
}
Err(error) if error.kind() == io::ErrorKind::Interrupted => {}
Err(error) => {
drop(sink);
let _ = actor.send(ActorMessage::ReaderFailed(error.to_string()));
return;
}
}
}
}
}
/// Writes all of `bytes`, adding to `newlines` the `\n` bytes that reached
/// the file, including those of a partial write before an error.
fn write_counting(mut file: impl Write, bytes: &[u8], newlines: &mut u64) -> io::Result<()> {
@@ -595,7 +298,7 @@ mod tests {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
let room = self.accept - self.written.len();
if room == 0 {
return Err(io::Error::from_raw_os_error(libc::ENOSPC));
return Err(io::Error::new(io::ErrorKind::StorageFull, "no space"));
}
let length = bytes.len().min(room).min(3);
self.written.extend_from_slice(&bytes[..length]);
@@ -615,7 +318,7 @@ mod tests {
};
let mut newlines = 0;
let error = write_counting(&mut writer, b"a\nb\nc\nd\ne\n", &mut newlines).unwrap_err();
assert_eq!(error.raw_os_error(), Some(libc::ENOSPC));
assert_eq!(error.kind(), io::ErrorKind::StorageFull);
assert_eq!(writer.written, b"a\nb\nc\nd");
assert_eq!(newlines, 3);
@@ -0,0 +1,435 @@
//! Pipe processes on Unix.
//!
//! The child runs in its own process group with stdin on `/dev/null`. Three
//! choices keep the daemon safe from what the child leaves behind:
//!
//! - The waiter observes the exit with `WNOWAIT` and leaves the child as a
//! zombie until shutdown. While the zombie exists, its PID and process group
//! ID cannot be reused, so killing the group can never hit an unrelated
//! process, even long after the exit.
//! - The reader polls a cancel pipe as well as the output pipe, so shutdown
//! never waits on a pipe that a descendant which left the process group
//! still holds open.
//! - A cancelled reader closes the pipe, so leftover writers get `SIGPIPE`
//! once the drain deadline passes.
use std::fs::OpenOptions;
use std::io::{self, PipeReader, PipeWriter, Read};
use std::os::fd::AsFd;
use std::os::unix::fs::OpenOptionsExt;
use std::os::unix::process::CommandExt;
use std::process::{Command, Stdio};
use std::sync::{Arc, Mutex, PoisonError, RwLock};
use std::thread;
use std::time::Instant;
use anyhow::{Context, Result, bail};
use nix::errno::Errno;
use nix::poll::{PollFd, PollFlags, PollTimeout, poll};
use super::{DRAIN_AFTER_EXIT, Exit, OutputSink, exit_message, start_pipe};
use crate::service::{CreatePipe, ProcessSignal, TerminalHandle, TerminalId};
/// Serializes pipe creation against `spawn`. macOS has no `pipe2`, so std
/// creates a pipe and then marks it close-on-exec; a fork in between would
/// leak the pipe into another child, where it could hold output or cancel
/// pipes open. Pipes are created under the read lock, children spawned under
/// the write lock.
static SPAWN_LOCK: RwLock<()> = RwLock::new(());
struct Pipes {
output: PipeReader,
output_writer: PipeWriter,
stderr: PipeWriter,
cancel: PipeReader,
cancel_writer: PipeWriter,
exited: PipeReader,
exited_writer: PipeWriter,
}
impl Pipes {
fn new() -> Result<Self> {
let _guard = SPAWN_LOCK.read().unwrap_or_else(PoisonError::into_inner);
let (output, output_writer) = io::pipe().context("failed to create output pipe")?;
let stderr = output_writer
.try_clone()
.context("failed to share output pipe")?;
let (cancel, cancel_writer) = io::pipe().context("failed to create cancel pipe")?;
let (exited, exited_writer) = io::pipe().context("failed to create exit pipe")?;
Ok(Self {
output,
output_writer,
stderr,
cancel,
cancel_writer,
exited,
exited_writer,
})
}
}
impl TerminalHandle {
pub(in crate::service) fn spawn_pipe(
id: TerminalId,
order: u64,
request: CreatePipe,
) -> Result<Self> {
let Pipes {
output,
output_writer,
stderr,
cancel,
cancel_writer,
exited,
exited_writer,
} = Pipes::new()?;
// Before spawning, so a bad path fails the request with nothing to clean up.
// The reader is the file's only writer, so it needs no append mode.
let sink = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
// Never write through a symlink planted at the final component.
.custom_flags(libc::O_NOFOLLOW)
.open(&request.output_path)
.with_context(|| {
format!(
"failed to open output file {}",
request.output_path.display()
)
})?;
let mut command = Command::new(&request.program);
command
.args(&request.args)
.current_dir(&request.cwd)
.env_clear()
.envs(&request.env)
.stdin(Stdio::null())
.stdout(output_writer)
.stderr(stderr)
.process_group(0);
// SAFETY: only async-signal-safe calls run between fork and exec.
unsafe {
command.pre_exec(reset_signals);
}
let child = {
let _guard = SPAWN_LOCK.write().unwrap_or_else(PoisonError::into_inner);
command
.spawn()
.with_context(|| format!("failed to spawn {}", request.program))?
};
// Close the daemon's copies of the write end, or EOF never arrives.
drop(command);
let process = Arc::new(Process {
pid: child.id(),
reaped: Mutex::new(false),
});
// Dropping `Child` neither waits nor kills; `Process` reaps it.
drop(child);
let reader_id = id;
let waiter_id = id;
start_pipe(
id,
order,
request,
Arc::clone(&process),
sink,
Box::new(cancel_writer),
super::Threads {
reader: Box::new(move |sink| {
thread::Builder::new()
.name(format!("opencode-pty-reader-{reader_id}"))
.spawn(move || read_output(output, sink, cancel, exited))
}),
waiter: Box::new(move |actor| {
let pid = process.pid;
// Spawned last: the waiter must be joined before `reap`.
thread::Builder::new()
.name(format!("opencode-pty-wait-{waiter_id}"))
.spawn(move || {
let _ = actor.send(exit_message(wait_exit(pid)));
// Starts the reader's drain deadline.
drop(exited_writer);
})
}),
},
)
}
}
/// Runs in the child between fork and exec. The daemon blocks SIGTERM and
/// SIGINT on all its threads, and std keeps the signal mask across exec, so
/// clear it, and give every signal portable-pty also resets its default action.
fn reset_signals() -> io::Result<()> {
// SAFETY: sigemptyset, sigprocmask and signal are async-signal-safe.
unsafe {
// Dispositions first, so no inherited handler can run once unmasked.
for signal in [
libc::SIGCHLD,
libc::SIGHUP,
libc::SIGINT,
libc::SIGQUIT,
libc::SIGTERM,
libc::SIGALRM,
] {
libc::signal(signal, libc::SIG_DFL);
}
let mut empty: libc::sigset_t = std::mem::zeroed();
if libc::sigemptyset(&mut empty) != 0
|| libc::sigprocmask(libc::SIG_SETMASK, &empty, std::ptr::null_mut()) != 0
{
return Err(io::Error::last_os_error());
}
}
Ok(())
}
/// A pipe child that stays a zombie after it exits, until `reap`.
#[derive(Debug)]
pub(in crate::service) struct Process {
pid: u32,
reaped: Mutex<bool>,
}
impl Process {
pub(super) fn pid(&self) -> u32 {
self.pid
}
/// Kills the whole process group. Never signals after `reap`, when the
/// group ID may belong to someone else.
pub(super) fn kill(&self) {
let reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner);
if !*reaped && let Ok(pid) = i32::try_from(self.pid) {
// SAFETY: the unreaped child keeps its PID and group ID reserved.
// Signal the child directly too, in case it moved to another group.
unsafe {
libc::killpg(pid, libc::SIGKILL);
libc::kill(pid, libc::SIGKILL);
}
}
}
/// Sends `signal` to the process group and the child. Allowed after the
/// exit, to reach what is left of the group; refused after `reap`.
pub(in crate::service) fn signal(&self, signal: ProcessSignal) -> Result<()> {
let signal = match signal {
ProcessSignal::Term => libc::SIGTERM,
ProcessSignal::Int => libc::SIGINT,
ProcessSignal::Kill => libc::SIGKILL,
};
let reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner);
if *reaped {
bail!("process has been reaped");
}
let pid = i32::try_from(self.pid).context("invalid pid")?;
// SAFETY: the unreaped child keeps its PID and group ID reserved.
check_signal(unsafe { libc::killpg(pid, signal) }, self.pid)?;
// Signal the child directly too, in case it moved to another group.
// SAFETY: as above.
check_signal(unsafe { libc::kill(pid, signal) }, self.pid)
}
/// Call only after the waiter has returned: kill the group first, so the
/// child has exited and this does not block.
pub(super) fn reap(&self) {
let mut reaped = self.reaped.lock().unwrap_or_else(PoisonError::into_inner);
if *reaped {
return;
}
let Ok(pid) = i32::try_from(self.pid) else {
return;
};
loop {
// SAFETY: waits only for this daemon's own child.
let result = unsafe { libc::waitpid(pid, std::ptr::null_mut(), 0) };
if result != -1 || Errno::last() != Errno::EINTR {
break;
}
}
*reaped = true;
}
}
/// Reads errno right after a `kill`/`killpg` result. A zombie group with no
/// live members gives ESRCH on macOS, which is not an error here.
/// Signalling an exited child whose group is empty fails with `ESRCH`, or
/// with `EPERM` on macOS (the zombie is still there); neither is an error.
fn check_signal(result: libc::c_int, pid: u32) -> Result<()> {
if result == -1 {
let errno = Errno::last();
if errno != Errno::ESRCH && !(errno == Errno::EPERM && has_exited(pid)) {
return Err(io::Error::from(errno)).context("failed to signal process");
}
}
Ok(())
}
/// Whether the child has exited, without reaping it.
fn has_exited(pid: u32) -> bool {
// SAFETY: siginfo_t is plain data, and all-zero is a valid value.
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
// SAFETY: `info` is a valid out pointer; WNOWAIT leaves the zombie.
let result = unsafe {
libc::waitid(
libc::P_PID,
pid as libc::id_t,
&mut info,
libc::WEXITED | libc::WNOWAIT | libc::WNOHANG,
)
};
// SAFETY: waitid filled `info`; a zero PID means the child is still running.
result == 0 && unsafe { info.si_pid() } != 0
}
/// Waits for the child to exit without reaping it.
fn wait_exit(pid: u32) -> io::Result<Exit> {
loop {
// SAFETY: siginfo_t is plain data, and all-zero is a valid value.
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
// SAFETY: `info` is a valid out pointer; WNOWAIT leaves the zombie.
let result = unsafe {
libc::waitid(
libc::P_PID,
pid as libc::id_t,
&mut info,
libc::WEXITED | libc::WNOWAIT,
)
};
if result == -1 {
let error = io::Error::last_os_error();
if error.kind() == io::ErrorKind::Interrupted {
continue;
}
return Err(error);
}
// SAFETY: waitid filled `info` for a SIGCHLD event.
let status = unsafe { info.si_status() };
return Ok(match info.si_code {
libc::CLD_EXITED => Exit::Code(status as u32),
_ => Exit::Signal(signal_name(status)),
});
}
}
fn signal_name(signal: i32) -> String {
let name = match signal {
libc::SIGHUP => "SIGHUP",
libc::SIGINT => "SIGINT",
libc::SIGQUIT => "SIGQUIT",
libc::SIGILL => "SIGILL",
libc::SIGTRAP => "SIGTRAP",
libc::SIGABRT => "SIGABRT",
libc::SIGBUS => "SIGBUS",
libc::SIGFPE => "SIGFPE",
libc::SIGKILL => "SIGKILL",
libc::SIGUSR1 => "SIGUSR1",
libc::SIGSEGV => "SIGSEGV",
libc::SIGUSR2 => "SIGUSR2",
libc::SIGPIPE => "SIGPIPE",
libc::SIGALRM => "SIGALRM",
libc::SIGTERM => "SIGTERM",
libc::SIGXCPU => "SIGXCPU",
libc::SIGXFSZ => "SIGXFSZ",
libc::SIGVTALRM => "SIGVTALRM",
libc::SIGPROF => "SIGPROF",
libc::SIGSYS => "SIGSYS",
_ => return format!("SIG{signal}"),
};
name.to_string()
}
/// Forwards output until EOF, until [`DRAIN_AFTER_EXIT`] after `exited`
/// closes, or until `cancel` closes. Once every writer has closed the output
/// pipe, what is left is finite, so it is read to the end regardless of the
/// deadline.
fn read_output(output: PipeReader, sink: OutputSink, cancel: PipeReader, exited: PipeReader) {
let mut output = output;
let mut sink = sink;
let mut buffer = [0_u8; 8192];
let mut deadline = None::<Instant>;
let mut writers_closed = false;
loop {
let timeout = match deadline {
Some(deadline) if !writers_closed => {
let left = deadline.saturating_duration_since(Instant::now());
if left.is_zero() {
// The reader may have been stalled past the deadline; take
// one last look before giving up on a held-open pipe.
let mut fds = [PollFd::new(output.as_fd(), PollFlags::POLLIN)];
let hung_up = poll(&mut fds, PollTimeout::ZERO).is_ok_and(|count| count > 0)
&& fds[0]
.revents()
.is_some_and(|events| events.contains(PollFlags::POLLHUP));
if !hung_up {
sink.eof();
return;
}
writers_closed = true;
PollTimeout::NONE
} else {
// Round up: a sub-millisecond remainder must not become a busy 0.
let millis = left.as_nanos().div_ceil(1_000_000).min(i32::MAX as u128);
PollTimeout::try_from(millis).unwrap_or(PollTimeout::MAX)
}
}
_ => PollTimeout::NONE,
};
let mut fds = [
PollFd::new(output.as_fd(), PollFlags::POLLIN),
PollFd::new(cancel.as_fd(), PollFlags::POLLIN),
PollFd::new(exited.as_fd(), PollFlags::POLLIN),
];
// Once the exit is seen, stop polling its closed pipe.
let count = if deadline.is_some() { 2 } else { 3 };
match poll(&mut fds[..count], timeout) {
Err(Errno::EINTR) | Ok(0) => continue,
Err(error) => {
sink.failed(error.to_string());
return;
}
Ok(_) => {}
}
let ready = |fd: &PollFd| {
fd.revents().is_none_or(|events| {
events.intersects(
PollFlags::POLLIN
| PollFlags::POLLHUP
| PollFlags::POLLERR
| PollFlags::POLLNVAL,
)
})
};
if ready(&fds[1]) {
return;
}
if count == 3 && ready(&fds[2]) {
deadline = Some(Instant::now() + DRAIN_AFTER_EXIT);
}
if fds[0]
.revents()
.is_some_and(|events| events.contains(PollFlags::POLLHUP))
{
writers_closed = true;
}
if ready(&fds[0]) {
match output.read(&mut buffer) {
Ok(0) => {
sink.eof();
return;
}
Ok(length) => {
if !sink.write(&buffer[..length]) {
return;
}
}
Err(error) if error.kind() == io::ErrorKind::Interrupted => {}
Err(error) => {
sink.failed(error.to_string());
return;
}
}
}
}
}
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -18,7 +18,7 @@ mod unix;
pub(crate) use unix::{Cancellation, Connection, Listener, Readiness, Wake};
#[cfg(windows)]
mod retained;
pub(crate) mod retained;
#[cfg(windows)]
pub(crate) mod windows;
@@ -540,7 +540,7 @@ impl Drop for Operation {
}
}
fn event() -> io::Result<OwnedHandle> {
pub(crate) fn event() -> io::Result<OwnedHandle> {
// SAFETY: create an unnamed, non-inheritable, initially unset manual-reset event.
owned_handle(unsafe { CreateEventW(null(), 1, 0, null()) })
}
@@ -0,0 +1,778 @@
#![cfg(windows)]
//! Pipe mode on Windows: no console, one output pipe, job-based kills.
//!
//! The Unix suite in `tests/pipe.rs` keeps its own cases; these only run on
//! Windows. Commands are passed the way OpenCode passes them (`cmd.exe /c
//! <command>`), and longer checks use a PowerShell script file. Windows
//! PowerShell needs `SystemRoot` in its environment to start, so test
//! children get a PATH, SystemRoot and TEMP, never the daemon's own.
use std::collections::HashMap;
use std::env;
use std::fs;
use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use opencode_pty::service::{
CreatePipe, MAX_METADATA_BYTES, PipeMetadata, ProcessExit, ProcessInfo, ProcessMode,
ProcessSignal, TerminalId, TerminalLifecycle, TerminalService,
};
use windows_sys::Win32::Foundation::{ERROR_PRIVILEGE_NOT_HELD, WAIT_OBJECT_0};
use windows_sys::Win32::System::Threading::{
OpenProcess, PROCESS_SYNCHRONIZE, WaitForSingleObject,
};
/// A unique temporary path, removed when dropped even if a test fails.
struct TempPath(PathBuf);
impl TempPath {
fn new(name: &str) -> Self {
Self(env::temp_dir().join(format!(
"opencode-pty-pipe-{name}-{:032x}",
rand::random::<u128>()
)))
}
fn script(name: &str, extension: &str) -> Self {
Self(env::temp_dir().join(format!(
"opencode-pty-pipe-{name}-{:032x}.{extension}",
rand::random::<u128>()
)))
}
}
impl Drop for TempPath {
fn drop(&mut self) {
let _ = fs::remove_file(&self.0);
let _ = fs::remove_dir_all(&self.0);
}
}
/// A PowerShell script on disk.
struct Script(TempPath);
impl Script {
fn powershell(name: &str, body: &str) -> Self {
let path = TempPath::script(name, "ps1");
fs::write(&path.0, body).unwrap();
Self(path)
}
/// A request that runs the script; `env` replaces the whole environment.
fn request(&self, output: &Path, env: HashMap<String, String>) -> CreatePipe {
pipe(
"powershell.exe",
vec![
"-NoProfile".into(),
"-NonInteractive".into(),
"-ExecutionPolicy".into(),
"Bypass".into(),
"-File".into(),
self.0.0.display().to_string(),
],
output,
env,
)
}
}
fn pipe(
program: &str,
args: Vec<String>,
output: &Path,
env: HashMap<String, String>,
) -> CreatePipe {
CreatePipe {
program: program.into(),
args,
cwd: env::current_dir().expect("cwd"),
env,
group_id: "pipe".into(),
output_path: output.to_path_buf(),
metadata: PipeMetadata::new(),
}
}
/// A `cmd.exe /c <command>` request, the way OpenCode runs shell commands.
fn cmd(command: &str, output: &Path) -> CreatePipe {
pipe(
"cmd.exe",
vec!["/d".into(), "/c".into(), command.into()],
output,
base_env(),
)
}
/// The daemon's own environment is never inherited, so children get just what
/// their program needs. Windows PowerShell fails to load without SystemRoot.
fn base_env() -> HashMap<String, String> {
["PATH", "SystemRoot", "TEMP"]
.into_iter()
.filter_map(|name| Some((name.to_string(), env::var(name).ok()?)))
.collect()
}
/// Starts a request with a fresh output file.
fn start(
service: &TerminalService,
request: impl FnOnce(&Path) -> CreatePipe,
) -> (ProcessInfo, TempPath) {
let file = TempPath::new("out");
let info = service.create_pipe(request(&file.0)).unwrap();
(info, file)
}
struct Finished {
output: String,
exit: ProcessExit,
elapsed: Duration,
}
/// Waits for the exit and reads the output file.
fn run(service: &TerminalService, id: TerminalId, file: &TempPath) -> Finished {
let started = Instant::now();
let exit = service.wait(id).unwrap().recv().unwrap();
Finished {
output: String::from_utf8_lossy(&fs::read(&file.0).unwrap()).into_owned(),
exit,
elapsed: started.elapsed(),
}
}
/// Polls the output file until it holds a whole first line.
fn first_line(file: &TempPath) -> String {
let deadline = Instant::now() + Duration::from_secs(10);
loop {
let output = fs::read_to_string(&file.0).unwrap_or_default();
if let Some((line, _)) = output.split_once("\r\n") {
return line.to_string();
}
assert!(Instant::now() < deadline, "no output line");
std::thread::sleep(Duration::from_millis(10));
}
}
/// Polls a file written by a child until it holds a PID.
fn read_pid(file: &Path) -> u32 {
let deadline = Instant::now() + Duration::from_secs(10);
loop {
if let Ok(text) = fs::read_to_string(file)
&& let Ok(pid) = text.trim().parse()
{
return pid;
}
assert!(Instant::now() < deadline, "child did not write its PID");
std::thread::sleep(Duration::from_millis(20));
}
}
fn alive(pid: u32) -> bool {
// SAFETY: a successful handle is adopted immediately; it is only waited on.
let handle = unsafe { OpenProcess(PROCESS_SYNCHRONIZE, 0, pid) };
if handle.is_null() {
return false;
}
// SAFETY: OpenProcess returned a new, uniquely owned handle.
let handle = unsafe { OwnedHandle::from_raw_handle(handle) };
// SAFETY: the owned handle stays valid through this zero-timeout wait.
unsafe { WaitForSingleObject(handle.as_raw_handle(), 0) != WAIT_OBJECT_0 }
}
fn wait_dead(pid: u32) {
let deadline = Instant::now() + Duration::from_secs(10);
while alive(pid) {
assert!(Instant::now() < deadline, "process {pid} is still running");
std::thread::sleep(Duration::from_millis(10));
}
}
fn newlines(bytes: &[u8]) -> u64 {
bytes.iter().filter(|&&byte| byte == b'\n').count() as u64
}
/// A path as a single-quoted PowerShell string literal.
fn ps_path(path: &Path) -> String {
path.display().to_string().replace('\'', "''")
}
/// Writes a batch file. Paths are quoted inside the file, where cmd parses
/// them normally; quoting them inside the `/c` argument would be mangled by
/// the MSVCRT escaping applied to command-line arguments.
fn batch(name: &str, lines: &[String]) -> TempPath {
let path = TempPath::script(name, "cmd");
fs::write(&path.0, lines.join("\r\n") + "\r\n").unwrap();
path
}
#[test]
fn reports_ordered_output_and_exit_code_without_a_terminal() {
let service = TerminalService::default();
let (info, file) = start(&service, |file| {
cmd(
"echo out1&echo err1>&2&echo out2&echo err2>&2&echo out3&echo err3>&2&set /p line=&echo stdin-closed&exit /b 7",
file,
)
});
assert_eq!(info.mode, ProcessMode::Pipe);
assert!(info.pty.is_none());
let pipe = info.pipe.as_ref().unwrap();
assert_eq!(pipe.output_path, file.0);
assert_eq!(info.exited_at, None);
let finished = run(&service, info.id, &file);
assert_eq!(
finished.output,
"out1\r\nerr1\r\nout2\r\nerr2\r\nout3\r\nerr3\r\nstdin-closed\r\n"
);
assert_eq!(finished.exit.exit_code, Some(7));
assert_eq!(finished.exit.signal, None);
assert_eq!(finished.exit.output_error, None);
assert_eq!(finished.exit.output_lines, Some(7));
assert_eq!(finished.exit.final_offset, finished.output.len() as u64);
let listed = service.list(ProcessMode::Pipe).unwrap();
assert_eq!(
listed[0].lifecycle,
TerminalLifecycle::Exited {
exit_code: Some(7),
signal: None
}
);
assert_eq!(listed[0].exited_at, Some(finished.exit.exited_at));
assert!(listed[0].started_at <= finished.exit.exited_at);
assert_eq!(listed[0].pipe.as_ref().unwrap().output_lines, 7);
assert_eq!(listed[0].output_tail, finished.exit.final_offset);
service.terminate(info.id).unwrap();
}
#[test]
fn fast_exit_without_output_is_reported() {
let service = TerminalService::default();
let (info, file) = start(&service, |file| cmd("exit /b 3", file));
let finished = run(&service, info.id, &file);
assert_eq!(finished.output, "");
assert_eq!(finished.exit.exit_code, Some(3));
assert_eq!(finished.exit.final_offset, 0);
assert_eq!(finished.exit.output_lines, Some(0));
service.terminate(info.id).unwrap();
}
#[test]
fn the_child_gets_exactly_the_given_environment() {
let service = TerminalService::default();
let system_root = env::var("SystemRoot").unwrap();
let (info, file) = start(&service, |file| {
pipe(
"cmd.exe",
vec!["/d".into(), "/c".into(), "set".into()],
file,
// No PATH and nothing else from the daemon.
HashMap::from([
("ONLY".to_string(), "this".to_string()),
("SystemRoot".to_string(), system_root.clone()),
]),
)
});
let finished = run(&service, info.id, &file);
// `set` also prints each drive's current directory as an `=C:` entry, and
// cmd defines COMSPEC, PATHEXT and PROMPT itself when they are missing;
// everything else must come from the request.
let variables = finished
.output
.lines()
.map(|line| line.trim_end_matches('\r'))
.filter(|line| {
!line.starts_with('=')
&& !line.starts_with("COMSPEC=")
&& !line.starts_with("PATHEXT=")
&& !line.starts_with("PROMPT=")
&& !line.starts_with("PROCESSOR_ARCHITECTURE=")
})
.collect::<Vec<_>>();
let expected = format!("SystemRoot={system_root}");
assert_eq!(variables, ["ONLY=this", expected.as_str()]);
service.terminate(info.id).unwrap();
}
#[test]
fn output_file_keeps_everything_and_is_truncated_on_open() {
let service = TerminalService::default();
let file = TempPath::new("full");
fs::write(&file.0, "earlier output that must go\r\n").unwrap();
let script = Script::powershell(
"full",
r#"
$lines = (1..20000) -join "`r`n"
[Console]::Out.Write($lines + "`r`n")
[Console]::Error.Write("done`r`n")
"#,
);
let info = service
.create_pipe(script.request(&file.0, base_env()))
.unwrap();
let exit = service.wait(info.id).unwrap().recv().unwrap();
let expected = (1..=20000).map(|n| format!("{n}\r\n")).collect::<String>() + "done\r\n";
assert_eq!(fs::read_to_string(&file.0).unwrap(), expected);
assert_eq!(exit.final_offset, expected.len() as u64);
assert_eq!(exit.output_lines, Some(20001));
assert_eq!(exit.output_error, None);
service.terminate(info.id).unwrap();
}
#[test]
fn rejected_requests_never_touch_the_output_file_or_run() {
let service = TerminalService::default();
let missing = TempPath::new("missing-directory").0.join("out");
let marker = TempPath::new("should-not-run");
let target = TempPath::new("target");
fs::write(&target.0, "kept").unwrap();
let existing = TempPath::new("existing");
fs::write(&existing.0, "kept").unwrap();
let touch = format!("echo ran> \"{}\"", marker.0.display());
let mut huge = PipeMetadata::new();
huge.insert("command".into(), "x".repeat(MAX_METADATA_BYTES).into());
let mut cases = vec![
(
CreatePipe {
output_path: PathBuf::from("relative.out"),
..cmd(&touch, &existing.0)
},
"must be absolute",
),
(
CreatePipe {
cwd: "relative".into(),
..cmd(&touch, &existing.0)
},
"cwd must be absolute",
),
(
CreatePipe {
metadata: huge,
..cmd(&touch, &existing.0)
},
"metadata exceeds",
),
(cmd(&touch, &missing), "failed to open output file"),
];
let link = TempPath::new("link");
let reparse = match std::os::windows::fs::symlink_file(&target.0, &link.0) {
Ok(()) => {
cases.push((cmd(&touch, &link.0), "failed to open output file"));
true
}
// Symbolic link creation needs Developer Mode or an elevated account.
Err(error) if error.raw_os_error() == Some(ERROR_PRIVILEGE_NOT_HELD as i32) => {
eprintln!("skipping the reparse point case: {error}");
false
}
Err(error) => panic!("failed to create a symlink: {error}"),
};
for (request, error) in cases {
let message = format!("{:#}", service.create_pipe(request).unwrap_err());
assert!(message.contains(error), "{message}");
}
assert!(service.list(ProcessMode::Pipe).unwrap().is_empty());
assert_eq!(fs::read_to_string(&existing.0).unwrap(), "kept");
assert_eq!(fs::read_to_string(&target.0).unwrap(), "kept");
if reparse {
assert_eq!(fs::read_to_string(&link.0).unwrap(), "kept");
}
// Give a wrongly spawned child time to run before checking.
std::thread::sleep(Duration::from_millis(100));
assert!(!marker.0.exists());
}
#[test]
fn output_lines_count_newlines_in_the_file() {
let service = TerminalService::default();
for (name, body, expected) in [
("two", r#"[Console]::Out.Write("a`nb`nc")"#, Some(2)),
("blanks", r#"[Console]::Out.Write("a`n`n`nb`n")"#, Some(4)),
("empty", r#"[Console]::Out.Write("")"#, Some(0)),
(
"many",
r#"[Console]::Out.Write(((1..200000) -join "`n"))"#,
Some(199999),
),
(
"small",
r#"for ($i = 1; $i -le 2000; $i++) { [Console]::Out.WriteLine($i) }"#,
Some(2000),
),
(
"random",
r#"
$bytes = New-Object byte[] 300000
(New-Object Random).NextBytes($bytes)
[Console]::OpenStandardOutput().Write($bytes, 0, $bytes.Length)
"#,
None,
),
] {
let script = Script::powershell(name, body);
let (info, file) = start(&service, |file| script.request(file, base_env()));
let exit = run(&service, info.id, &file).exit;
let bytes = fs::read(&file.0).unwrap();
assert_eq!(exit.output_lines, Some(newlines(&bytes)), "{name}");
if let Some(expected) = expected {
assert_eq!(exit.output_lines, Some(expected), "{name}");
}
assert_eq!(exit.final_offset, bytes.len() as u64, "{name}");
let listed = service.get(info.id).unwrap();
assert_eq!(
listed.pipe.unwrap().output_lines,
exit.output_lines.unwrap()
);
service.terminate(info.id).unwrap();
}
}
#[test]
fn wait_blocks_until_the_exit_and_answers_every_waiter() {
let service = TerminalService::default();
let script = Script::powershell(
"wait",
r#"
Start-Sleep -Milliseconds 300
[Console]::Out.WriteLine("done")
exit 2
"#,
);
let (info, file) = start(&service, |file| script.request(file, base_env()));
let waits = (0..3)
.map(|_| service.wait(info.id).unwrap())
.collect::<Vec<_>>();
assert!(waits[0].exit.try_recv().is_err());
let exits = waits
.iter()
.map(|wait| wait.recv().unwrap())
.collect::<Vec<_>>();
assert!(exits.iter().all(|exit| *exit == exits[0]));
assert_eq!(exits[0].exit_code, Some(2));
assert_eq!(exits[0].output_lines, Some(1));
assert_eq!(fs::read_to_string(&file.0).unwrap(), "done\r\n");
// After the exit, a new waiter is answered at once.
let started = Instant::now();
assert_eq!(service.wait(info.id).unwrap().recv().unwrap(), exits[0]);
assert!(started.elapsed() < Duration::from_millis(500));
service.terminate(info.id).unwrap();
assert!(service.wait(info.id).is_err());
}
#[test]
fn terminate_ends_a_pending_wait_with_an_error() {
let service = TerminalService::default();
let script = Script::powershell("sleep", "Start-Sleep -Seconds 30");
let (info, _file) = start(&service, |file| script.request(file, base_env()));
let wait = service.wait(info.id).unwrap();
service.terminate(info.id).unwrap();
assert_eq!(wait.recv().unwrap_err().to_string(), "terminated");
}
#[test]
fn signal_stops_a_process_and_keeps_its_record() {
let service = TerminalService::default();
let script = Script::powershell(
"signal",
r#"
[Console]::Out.WriteLine("started")
Start-Sleep -Seconds 30
"#,
);
let (info, file) = start(&service, |file| script.request(file, base_env()));
first_line(&file);
service.signal(info.id, ProcessSignal::Term).unwrap();
let exit = service.wait(info.id).unwrap().recv().unwrap();
assert_eq!(exit.exit_code, None);
assert_eq!(exit.signal.as_deref(), Some("SIGTERM"));
assert_eq!(exit.output_lines, Some(1));
assert_eq!(
service.get(info.id).unwrap().lifecycle,
TerminalLifecycle::Exited {
exit_code: None,
signal: Some("SIGTERM".into())
}
);
// What is left of the job is killed again without an error.
service.signal(info.id, ProcessSignal::Kill).unwrap();
service.terminate(info.id).unwrap();
assert!(service.signal(info.id, ProcessSignal::Kill).is_err());
}
#[test]
fn signal_reaches_a_grandchild_in_the_job() {
let service = TerminalService::default();
let pid_file = TempPath::new("grandchild-pid");
let inner = Script::powershell(
"grandchild",
&format!(
r#"
[System.IO.File]::WriteAllText('{}', "$PID")
Start-Sleep -Seconds 60
"#,
ps_path(&pid_file.0)
),
);
let outer = batch(
"grandchild-outer",
&[
"@echo off".to_string(),
format!(
"start /b powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{}\"",
inner.0.0.display()
),
"ping -n 60 127.0.0.1".to_string(),
],
);
let (info, _file) = start(&service, |file| cmd(&outer.0.display().to_string(), file));
let grandchild = read_pid(&pid_file.0);
assert!(alive(grandchild));
service.signal(info.id, ProcessSignal::Term).unwrap();
let exit = service.wait(info.id).unwrap().recv().unwrap();
assert_eq!(exit.signal.as_deref(), Some("SIGTERM"));
wait_dead(grandchild);
service.terminate(info.id).unwrap();
}
#[test]
fn a_grandchild_holding_the_pipe_delays_the_exit_by_at_most_the_drain() {
let service = TerminalService::default();
let (info, file) = start(&service, |file| cmd("start /b ping -n 60 127.0.0.1", file));
let finished = run(&service, info.id, &file);
assert_eq!(finished.exit.exit_code, Some(0));
assert!(
finished.elapsed < Duration::from_secs(10),
"{:?}",
finished.elapsed
);
// Terminate kills the rest of the job.
service.terminate(info.id).unwrap();
}
#[test]
fn terminate_does_not_hang_on_a_grandchild() {
let service = TerminalService::default();
let pid_file = TempPath::new("terminate-pid");
let inner = Script::powershell(
"terminate-grandchild",
&format!(
r#"
[System.IO.File]::WriteAllText('{}', "$PID")
Start-Sleep -Seconds 60
"#,
ps_path(&pid_file.0)
),
);
let outer = batch(
"terminate-outer",
&[
"@echo off".to_string(),
format!(
"start /b powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{}\"",
inner.0.0.display()
),
"ping -n 60 127.0.0.1".to_string(),
],
);
let (info, _file) = start(&service, |file| cmd(&outer.0.display().to_string(), file));
let grandchild = read_pid(&pid_file.0);
assert!(alive(grandchild));
let started = Instant::now();
service.terminate(info.id).unwrap();
assert!(
started.elapsed() < Duration::from_secs(10),
"{:?}",
started.elapsed()
);
wait_dead(grandchild);
}
#[test]
fn a_noisy_grandchild_still_exits_after_the_drain() {
let service = TerminalService::default();
let marker = TempPath::new("noisy-marker");
let inner = Script::powershell(
"noisy-grandchild",
&format!(
r#"
# Prove the pipe is live before the parent exits.
[Console]::Out.Write('x')
[Console]::Out.Flush()
[System.IO.File]::WriteAllText('{}', 'started')
while ($true) {{
[Console]::Out.Write('x')
}}
"#,
ps_path(&marker.0)
),
);
// A batch parent: it waits for the marker, so the grandchild is already
// writing when the parent exits and the drain deadline is exercised.
let outer = batch(
"noisy-outer",
&[
"@echo off".to_string(),
format!(
"start /b powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{}\"",
inner.0.0.display()
),
":wait".to_string(),
format!("if exist \"{}\" goto done", marker.0.display()),
"ping -n 2 127.0.0.1 >nul".to_string(),
"goto wait".to_string(),
":done".to_string(),
"exit /b 0".to_string(),
],
);
let (info, file) = start(&service, |file| cmd(&outer.0.display().to_string(), file));
let finished = run(&service, info.id, &file);
assert_eq!(finished.exit.exit_code, Some(0));
assert!(
finished.elapsed < Duration::from_secs(10),
"{:?}",
finished.elapsed
);
service.terminate(info.id).unwrap();
}
#[test]
fn signal_after_a_normal_exit_kills_a_leftover_grandchild() {
let service = TerminalService::default();
let pid_file = TempPath::new("leftover-pid");
let inner = Script::powershell(
"leftover-grandchild",
&format!(
r#"
[System.IO.File]::WriteAllText('{}', "$PID")
Start-Sleep -Seconds 60
"#,
ps_path(&pid_file.0)
),
);
let outer = batch(
"leftover-outer",
&[
"@echo off".to_string(),
format!(
"start /b powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{}\"",
inner.0.0.display()
),
],
);
let (info, _file) = start(&service, |file| cmd(&outer.0.display().to_string(), file));
let grandchild = read_pid(&pid_file.0);
let exit = service.wait(info.id).unwrap().recv().unwrap();
assert_eq!(exit.exit_code, Some(0));
assert_eq!(exit.signal, None);
// The process keeps its record after the exit; the grandchild still runs.
assert!(alive(grandchild));
assert_eq!(
service.get(info.id).unwrap().lifecycle,
TerminalLifecycle::Exited {
exit_code: Some(0),
signal: None
}
);
assert_eq!(service.list(ProcessMode::Pipe).unwrap().len(), 1);
service.signal(info.id, ProcessSignal::Kill).unwrap();
wait_dead(grandchild);
service.terminate(info.id).unwrap();
assert!(service.list(ProcessMode::Pipe).unwrap().is_empty());
}
#[test]
fn the_stdout_survives_closing_stderr() {
let service = TerminalService::default();
let script = Script::powershell(
"stderr",
r#"Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class StandardHandles {
[DllImport("kernel32.dll")]
public static extern IntPtr GetStdHandle(int handle);
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr handle);
}
"@
# STD_ERROR_HANDLE - stdout must be a separate handle to the same pipe.
[StandardHandles]::CloseHandle([StandardHandles]::GetStdHandle(-12)) | Out-Null
Start-Sleep -Milliseconds 200
[Console]::Out.WriteLine("after-stderr-closed")
"#,
);
let (info, file) = start(&service, |file| script.request(file, base_env()));
let finished = run(&service, info.id, &file);
assert_eq!(finished.output, "after-stderr-closed\r\n");
assert_eq!(finished.exit.exit_code, Some(0));
service.terminate(info.id).unwrap();
}
#[test]
fn arguments_round_trip_through_the_command_line() {
let service = TerminalService::default();
let script = Script::powershell(
"quoting",
r#"for ($i = 0; $i -lt $args.Count; $i++) { [Console]::Out.WriteLine("$i<$($args[$i])>") }"#,
);
// A trailing backslash needs doubling only when the argument is quoted.
let args = ["plain", "with space", "say \"hi\"", r"C:\with space\"];
let (info, file) = start(&service, |file| {
let mut request = script.request(file, base_env());
request.args.extend(args.iter().map(|arg| arg.to_string()));
request
});
let finished = run(&service, info.id, &file);
assert_eq!(
finished.output,
"0<plain>\r\n1<with space>\r\n2<say \"hi\">\r\n3<C:\\with space\\>\r\n"
);
assert_eq!(finished.exit.exit_code, Some(0));
service.terminate(info.id).unwrap();
}
#[test]
fn the_child_has_no_console_window() {
let service = TerminalService::default();
let script = Script::powershell(
"console",
r#"Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class ConsoleWindow {
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
}
"@
$window = [ConsoleWindow]::GetConsoleWindow()
if ($window -eq [IntPtr]::Zero) {
[Console]::Out.WriteLine("no-console")
} else {
[Console]::Out.WriteLine("console")
}
"#,
);
let (info, file) = start(&service, |file| script.request(file, base_env()));
let finished = run(&service, info.id, &file);
assert_eq!(finished.output, "no-console\r\n");
assert_eq!(finished.exit.exit_code, Some(0));
service.terminate(info.id).unwrap();
}
#[test]
fn the_output_file_can_be_read_and_deleted_while_open() {
let service = TerminalService::default();
let script = Script::powershell(
"shared",
r#"
[Console]::Out.WriteLine("started")
Start-Sleep -Seconds 30
"#,
);
let (info, file) = start(&service, |file| script.request(file, base_env()));
first_line(&file);
assert!(fs::read_to_string(&file.0).unwrap().starts_with("started"));
fs::remove_file(&file.0).unwrap();
service.terminate(info.id).unwrap();
}