From 601367b1bea81b25cc5535107675fd152157ac01 Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Oct 2026 15:50:52 -0400 Subject: [PATCH 1/9] fix(process-daemon): stop cleanly on SIGTERM and SIGINT --- packages/process-daemon/Cargo.toml | 2 +- packages/process-daemon/src/daemon/server.rs | 35 ++++++++++++++++ packages/process-daemon/tests/ownership.rs | 42 ++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/packages/process-daemon/Cargo.toml b/packages/process-daemon/Cargo.toml index 35cc8547a8d7..698bbe1096e0 100644 --- a/packages/process-daemon/Cargo.toml +++ b/packages/process-daemon/Cargo.toml @@ -21,7 +21,7 @@ shell-words = "1.1" [target.'cfg(unix)'.dependencies] libc = "0.2" -nix = { version = "0.28", features = ["poll", "process", "term", "user"] } +nix = { version = "0.28", features = ["poll", "process", "signal", "term", "user"] } sha2 = "0.10" [target.'cfg(windows)'.dependencies] diff --git a/packages/process-daemon/src/daemon/server.rs b/packages/process-daemon/src/daemon/server.rs index 7f07ebaee8cf..6ecb44145102 100644 --- a/packages/process-daemon/src/daemon/server.rs +++ b/packages/process-daemon/src/daemon/server.rs @@ -24,9 +24,44 @@ pub fn run(directory: &Path) -> Result<()> { let (runtime, listener) = platform::Runtime::bind(directory)?; // Created after bind so bind I/O does not consume the acquire window. let control = Arc::new(Control::new(wake, Ownership::new(Instant::now()))); + #[cfg(unix)] + stop_on_signals(&control)?; serve(runtime, listener, control, Settings::default()) } +/// Turns SIGTERM and SIGINT into an orderly stop, so children are killed and the +/// runtime directory is removed instead of the daemon dying with both left behind. +/// +/// The signals are blocked on this thread before any worker exists, so every later +/// thread inherits the mask and only the waiter receives them. Children never +/// inherit it: std and portable-pty reset the signal mask before `exec`. +#[cfg(unix)] +fn stop_on_signals(control: &Arc) -> Result<()> { + use nix::sys::signal::{SigSet, Signal}; + + let mut signals = SigSet::empty(); + signals.add(Signal::SIGTERM); + signals.add(Signal::SIGINT); + signals + .thread_block() + .context("failed to block stop signals")?; + let control = Arc::clone(control); + thread::Builder::new() + .name("opencode-pty-signals".into()) + .spawn(move || { + if signals.wait().is_err() { + return; + } + control.request_stop(); + // A second signal means the orderly stop is stuck; exit like the default action. + if let Ok(signal) = signals.wait() { + std::process::exit(128 + signal as i32); + } + }) + .context("failed to start the signal thread")?; + Ok(()) +} + struct Settings { /// Longest the loop sleeps without a wake or deadline. safety_net: Duration, diff --git a/packages/process-daemon/tests/ownership.rs b/packages/process-daemon/tests/ownership.rs index f74f162c5cbe..157e0f8c4b6d 100644 --- a/packages/process-daemon/tests/ownership.rs +++ b/packages/process-daemon/tests/ownership.rs @@ -536,3 +536,45 @@ fn shutdown_keeps_a_replaced_socket() { drop(replacement); std::fs::remove_file(&daemon.registration.socket).unwrap(); } + +#[test] +fn stop_signals_stop_terminals_and_clean_up() { + for signal in [libc::SIGTERM, libc::SIGINT] { + let mut daemon = Daemon::start(); + let (_owner, response) = daemon.own(None); + assert!(matches!(response, Response::Owned)); + let terminal = daemon.terminal("exec sleep 1000"); + + // The daemon blocks these signals for its own threads; its children must not + // inherit that mask, so a terminal's process still dies from SIGTERM. + let probe = daemon.terminal("exec sleep 1000"); + // SAFETY: the probe PID belongs to this test's live terminal. + assert_eq!( + unsafe { libc::kill(probe.pid.unwrap() as i32, libc::SIGTERM) }, + 0 + ); + let deadline = Instant::now() + Duration::from_secs(3); + loop { + let Response::Terminals { terminals } = daemon.request(Request::List) else { + panic!("list failed"); + }; + let exited = terminals.iter().any(|value| { + value.id == probe.id + && !matches!( + value.lifecycle, + opencode_pty::service::TerminalLifecycle::Running + ) + }); + if exited { + break; + } + assert!(Instant::now() < deadline, "probe ignored SIGTERM"); + thread::sleep(Duration::from_millis(10)); + } + + // SAFETY: the PID belongs to the daemon this test spawned. + assert_eq!(unsafe { libc::kill(daemon.child.id() as i32, signal) }, 0); + daemon.wait(); + assert_terminal_stopped(&terminal); + } +} From c540d8ff1ebef0a72edad443d39d44a4d74fbe78 Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Oct 2026 16:05:20 -0400 Subject: [PATCH 2/9] fix(process-daemon): block stop signals before startup threads and avoid nix sigwait --- packages/process-daemon/Cargo.toml | 2 +- packages/process-daemon/src/daemon/server.rs | 122 ++++++++++++++----- packages/process-daemon/tests/ownership.rs | 11 +- 3 files changed, 99 insertions(+), 36 deletions(-) diff --git a/packages/process-daemon/Cargo.toml b/packages/process-daemon/Cargo.toml index 698bbe1096e0..35cc8547a8d7 100644 --- a/packages/process-daemon/Cargo.toml +++ b/packages/process-daemon/Cargo.toml @@ -21,7 +21,7 @@ shell-words = "1.1" [target.'cfg(unix)'.dependencies] libc = "0.2" -nix = { version = "0.28", features = ["poll", "process", "signal", "term", "user"] } +nix = { version = "0.28", features = ["poll", "process", "term", "user"] } sha2 = "0.10" [target.'cfg(windows)'.dependencies] diff --git a/packages/process-daemon/src/daemon/server.rs b/packages/process-daemon/src/daemon/server.rs index 6ecb44145102..8c7ce9130683 100644 --- a/packages/process-daemon/src/daemon/server.rs +++ b/packages/process-daemon/src/daemon/server.rs @@ -19,47 +19,113 @@ const ACCEPT_BACKOFF: Duration = Duration::from_millis(100); const MAX_ACCEPTS_PER_PASS: usize = 64; pub fn run(directory: &Path) -> Result<()> { + // First, before bind starts the sweeper thread: every thread created after + // this inherits the mask, so only the signal thread ever receives them. + #[cfg(unix)] + let signals = signals::block()?; // Before bind, so a failure here leaves nothing to clean up. let wake = Wake::new()?; let (runtime, listener) = platform::Runtime::bind(directory)?; // Created after bind so bind I/O does not consume the acquire window. let control = Arc::new(Control::new(wake, Ownership::new(Instant::now()))); #[cfg(unix)] - stop_on_signals(&control)?; + if let Err(error) = signals::stop_on(signals, &control) { + let _ = runtime.finish(); + return Err(error); + } serve(runtime, listener, control, Settings::default()) } -/// Turns SIGTERM and SIGINT into an orderly stop, so children are killed and the -/// runtime directory is removed instead of the daemon dying with both left behind. +/// Turns SIGTERM and SIGINT into an orderly stop, so children are killed and +/// the runtime directory is removed instead of the daemon dying with both left +/// behind. /// -/// The signals are blocked on this thread before any worker exists, so every later -/// thread inherits the mask and only the waiter receives them. Children never -/// inherit it: std and portable-pty reset the signal mask before `exec`. +/// Children inherit the signal mask across `exec`. portable-pty resets it for +/// PTY children; anything else the daemon spawns must reset it itself. #[cfg(unix)] -fn stop_on_signals(control: &Arc) -> Result<()> { - use nix::sys::signal::{SigSet, Signal}; - - let mut signals = SigSet::empty(); - signals.add(Signal::SIGTERM); - signals.add(Signal::SIGINT); - signals - .thread_block() - .context("failed to block stop signals")?; - let control = Arc::clone(control); - thread::Builder::new() - .name("opencode-pty-signals".into()) - .spawn(move || { - if signals.wait().is_err() { - return; +mod signals { + use std::io; + use std::sync::Arc; + use std::thread; + use std::time::{Duration, Instant}; + + use anyhow::{Context, Result}; + + use super::Control; + + const STOP: [libc::c_int; 2] = [libc::SIGTERM, libc::SIGINT]; + /// A second signal this soon after the first is part of the same stop + /// (Ctrl-C plus a wrapper's SIGTERM), not a sign that the stop is stuck. + const REPEAT_GRACE: Duration = Duration::from_secs(1); + + pub(super) struct Blocked(libc::sigset_t); + + // SAFETY: a sigset_t is plain data. + unsafe impl Send for Blocked {} + + /// Blocks the stop signals on the calling thread, and restores their + /// default action: macOS never delivers an ignored signal to `sigwait`, + /// and a launcher may have left SIGINT ignored. + pub(super) fn block() -> Result { + // SAFETY: plain libc calls on a local, initialized signal set. + unsafe { + let mut set: libc::sigset_t = std::mem::zeroed(); + libc::sigemptyset(&mut set); + for signal in STOP { + libc::sigaddset(&mut set, signal); } - control.request_stop(); - // A second signal means the orderly stop is stuck; exit like the default action. - if let Ok(signal) = signals.wait() { - std::process::exit(128 + signal as i32); + let result = libc::pthread_sigmask(libc::SIG_BLOCK, &set, std::ptr::null_mut()); + if result != 0 { + return Err(io::Error::from_raw_os_error(result)) + .context("failed to block stop signals"); } - }) - .context("failed to start the signal thread")?; - Ok(()) + // Only after blocking, so there is no window where the default + // action could kill the daemon. + for signal in STOP { + libc::signal(signal, libc::SIG_DFL); + } + Ok(Blocked(set)) + } + } + + pub(super) fn stop_on(signals: Blocked, control: &Arc) -> Result<()> { + let control = Arc::clone(control); + thread::Builder::new() + .name("opencode-pty-signals".into()) + .spawn(move || { + wait(&signals); + let stopped = Instant::now(); + control.request_stop(); + loop { + let signal = wait(&signals); + if stopped.elapsed() >= REPEAT_GRACE { + // The orderly stop is stuck. Exit without running exit + // handlers, which other threads may be blocking. + // SAFETY: `_exit` is always safe to call. + unsafe { libc::_exit(128 + signal) }; + } + } + }) + .context("failed to start the signal thread")?; + Ok(()) + } + + fn wait(signals: &Blocked) -> libc::c_int { + loop { + let mut signal = 0; + // SAFETY: valid set and out pointer; `sigwait` returns an error + // number instead of setting errno, and fills `signal` on success. + let result = unsafe { libc::sigwait(&signals.0, &mut signal) }; + if result == 0 { + return signal; + } + // EINTR (after SIGSTOP and SIGCONT) just retries. Nothing else is + // expected for valid signals; pause so it can't spin. + if result != libc::EINTR { + thread::sleep(Duration::from_millis(100)); + } + } + } } struct Settings { diff --git a/packages/process-daemon/tests/ownership.rs b/packages/process-daemon/tests/ownership.rs index 157e0f8c4b6d..0cade5397a31 100644 --- a/packages/process-daemon/tests/ownership.rs +++ b/packages/process-daemon/tests/ownership.rs @@ -545,14 +545,11 @@ fn stop_signals_stop_terminals_and_clean_up() { assert!(matches!(response, Response::Owned)); let terminal = daemon.terminal("exec sleep 1000"); - // The daemon blocks these signals for its own threads; its children must not - // inherit that mask, so a terminal's process still dies from SIGTERM. + // The daemon blocks these signals on its own threads; its children must not + // inherit that mask, so a terminal's process still dies from the signal. let probe = daemon.terminal("exec sleep 1000"); // SAFETY: the probe PID belongs to this test's live terminal. - assert_eq!( - unsafe { libc::kill(probe.pid.unwrap() as i32, libc::SIGTERM) }, - 0 - ); + assert_eq!(unsafe { libc::kill(probe.pid.unwrap() as i32, signal) }, 0); let deadline = Instant::now() + Duration::from_secs(3); loop { let Response::Terminals { terminals } = daemon.request(Request::List) else { @@ -568,7 +565,7 @@ fn stop_signals_stop_terminals_and_clean_up() { if exited { break; } - assert!(Instant::now() < deadline, "probe ignored SIGTERM"); + assert!(Instant::now() < deadline, "probe ignored signal {signal}"); thread::sleep(Duration::from_millis(10)); } From 573c83f77dd2bf801d5d698222df035b60a49152 Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Oct 2026 16:08:40 -0400 Subject: [PATCH 3/9] chore(process-daemon): drop a redundant Send impl --- packages/process-daemon/src/daemon/server.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/process-daemon/src/daemon/server.rs b/packages/process-daemon/src/daemon/server.rs index 8c7ce9130683..d54b4d1b3c60 100644 --- a/packages/process-daemon/src/daemon/server.rs +++ b/packages/process-daemon/src/daemon/server.rs @@ -60,9 +60,6 @@ mod signals { pub(super) struct Blocked(libc::sigset_t); - // SAFETY: a sigset_t is plain data. - unsafe impl Send for Blocked {} - /// Blocks the stop signals on the calling thread, and restores their /// default action: macOS never delivers an ignored signal to `sigwait`, /// and a launcher may have left SIGINT ignored. From 6a3472d92c47d4145502ea1808f25cdb0d8f9cec Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Oct 2026 16:00:56 -0400 Subject: [PATCH 4/9] feat(process-daemon): run commands without a PTY on Unix --- .github/workflows/check-process-daemon.yml | 2 +- packages/process-daemon/SPEC.md | 38 +- packages/process-daemon/src/client.rs | 7 +- packages/process-daemon/src/daemon/server.rs | 8 +- packages/process-daemon/src/protocol.rs | 103 ++++- packages/process-daemon/src/service.rs | 403 +++++++++++++----- packages/process-daemon/src/service/pipe.rs | 323 ++++++++++++++ packages/process-daemon/src/transport/unix.rs | 2 + .../process-daemon/src/transport/windows.rs | 1 + packages/process-daemon/tests/ownership.rs | 101 ++++- packages/process-daemon/tests/pipe.rs | 294 +++++++++++++ packages/process-daemon/tests/runtime.rs | 4 +- packages/process-daemon/tests/service.rs | 1 + .../tests/support/resize_fixture.rs | 1 + .../tests/support/terminal_fixture.rs | 1 + .../process-daemon/tests/windows-daemon.rs | 17 +- 16 files changed, 1180 insertions(+), 126 deletions(-) create mode 100644 packages/process-daemon/src/service/pipe.rs create mode 100644 packages/process-daemon/tests/pipe.rs diff --git a/.github/workflows/check-process-daemon.yml b/.github/workflows/check-process-daemon.yml index 45473dc3086f..46e25428a4fc 100644 --- a/.github/workflows/check-process-daemon.yml +++ b/.github/workflows/check-process-daemon.yml @@ -74,7 +74,7 @@ jobs: # Match the release workflow's macOS playground exclusion. - name: Test macOS if: runner.os == 'macOS' - run: cargo test --locked --lib --test service --test runtime --test ownership --test rows --test ghostty-effects + run: cargo test --locked --lib --test service --test runtime --test ownership --test pipe --test rows --test ghostty-effects windows: name: Windows (${{ matrix.arch }}) diff --git a/packages/process-daemon/SPEC.md b/packages/process-daemon/SPEC.md index d4b88f08f1f9..47b8fef39841 100644 --- a/packages/process-daemon/SPEC.md +++ b/packages/process-daemon/SPEC.md @@ -107,6 +107,42 @@ when it changes; an OSC change hidden behind a user title publishes nothing. `list` returns terminals in creation order. +## Pipe Processes + +`Create` takes `mode: "pty" | "pipe"` (default `"pty"`). A pipe process runs +any command without a terminal; it is Unix-only for now, and only daemons that +list the `pipe` feature accept it. Older daemons ignore `mode` and would start +a PTY instead, so clients check `features` first. + +- `title`, `cols` and `rows` are optional for pipe processes; the title + defaults to the program's file name and the size is reported as `0x0`. +- The child gets its own process group, stdin on `/dev/null`, and one pipe for + both stdout and stderr, so their bytes stay in order. No `TERM` or + `COLORTERM` is added, and nothing parses the output: there is no emulator, + no terminal replies, no OSC title and no foreground process. +- Output goes through the same raw replay and subscriptions as a PTY, with the + same absolute offsets. A pipe has no controller or observer distinction for + writing, since it accepts no input. +- `write`, `resize`, `control`, input frames, `snapshot`, `read_rows` and + `rename` fail for pipe processes. `replay`, `subscribe` and `terminate` + work. +- Exit is published once both the exit status and the end of output are + known. A descendant that keeps the pipe open (`cmd &`) delays that by at + most one second after the child exits; output after that is not read. +- `Exited` (the lifecycle and the subscription response) carries `signal`, + the signal's name such as `"SIGTERM"`, when a pipe process was killed by + one; `exit_code` is then null. The field is left out when there is no + signal, so PTY exits serialize as before. +- An exited pipe process stays listed, as a zombie, until `terminate`. The + zombie keeps its PID and process group ID reserved, so `terminate` can kill + any descendants left in the group without reaching an unrelated process. + `terminate` kills the whole group with SIGKILL, reaps the child, and never + waits on a descendant that left the group and still holds the pipe. + +`List` takes `mode` too. Without it, `list` returns PTYs only, so servers that +predate pipe processes never see them. Clients leave `mode` out of the request +when it is unset: older daemons reject a `list` with any extra field. + ## Failure Boundary Losing the owning server connection stops `opencode-pty` and its terminals. @@ -131,7 +167,7 @@ subscriptions, title updates, rename, controller takeover, terminate, and destru shutdown, ownership acquisition, and restart handoff preparation. `Pong` and the registration carry `features`, the daemon's additive -capabilities (currently `["rename"]`). Additive changes, such as a new request +capabilities (currently `["rename", "pipe"]` on Unix and `["rename"]` on Windows). Additive changes, such as a new request or field older clients can ignore, extend `features` and keep the protocol version; clients check `features` before sending a new request, since a daemon that cannot parse one drops the connection. Only breaking changes bump diff --git a/packages/process-daemon/src/client.rs b/packages/process-daemon/src/client.rs index ea7503d7c0e3..b2eebb59a730 100644 --- a/packages/process-daemon/src/client.rs +++ b/packages/process-daemon/src/client.rs @@ -13,7 +13,7 @@ use crate::protocol::{ AttachmentRole, Envelope, SubscriptionEvent, read_frame, read_subscription_event, write_frame, }; use crate::protocol::{PROTOCOL_VERSION, Request, Response}; -use crate::service::{CreateTerminal, TerminalId, TerminalInfo, TerminalRows}; +use crate::service::{CreateTerminal, ProcessMode, TerminalId, TerminalInfo, TerminalRows}; #[cfg(unix)] use crate::transport::Connection; @@ -165,6 +165,7 @@ impl TerminalClient { env: request.env, cols: request.cols, rows: request.rows, + mode: request.mode, })? { Response::Created { terminal } => Ok(terminal), response => unexpected(response), @@ -172,7 +173,7 @@ impl TerminalClient { } pub fn list(&self) -> Result> { - match self.request(Request::List)? { + match self.request(Request::List { mode: None })? { Response::Terminals { terminals } => Ok(terminals), response => unexpected(response), } @@ -588,6 +589,7 @@ fn play(directory: &Path) -> Result<()> { cwd: env::current_dir()?, cols: 100, rows: 30, + mode: ProcessMode::Pty, } }; let info = client.create(request)?; @@ -665,6 +667,7 @@ fn play(directory: &Path) -> Result<()> { cwd: env::current_dir()?, cols: 100, rows: 30, + mode: ProcessMode::Pty, })?; active = Some(info.id); thread::sleep(Duration::from_millis(200)); diff --git a/packages/process-daemon/src/daemon/server.rs b/packages/process-daemon/src/daemon/server.rs index d54b4d1b3c60..f1686ab04b48 100644 --- a/packages/process-daemon/src/daemon/server.rs +++ b/packages/process-daemon/src/daemon/server.rs @@ -503,9 +503,11 @@ fn stream_subscription( }, StreamEvent::Exited { exit_code, + signal, final_offset, } => Response::Exited { exit_code, + signal, final_offset, }, StreamEvent::ControllerChanged { @@ -556,6 +558,7 @@ fn dispatch( env, cols, rows, + mode, } => Response::Created { terminal: service.create(CreateTerminal { program, @@ -566,10 +569,11 @@ fn dispatch( env, cols, rows, + mode, })?, }, - Request::List => Response::Terminals { - terminals: service.list()?, + Request::List { mode } => Response::Terminals { + terminals: service.list_mode(mode.unwrap_or_default())?, }, Request::Write { id, diff --git a/packages/process-daemon/src/protocol.rs b/packages/process-daemon/src/protocol.rs index 80e5c0be537a..64e16990a8ca 100644 --- a/packages/process-daemon/src/protocol.rs +++ b/packages/process-daemon/src/protocol.rs @@ -4,10 +4,14 @@ use std::path::PathBuf; use anyhow::{Context, Result, bail}; use serde::{Deserialize, Serialize}; -use crate::service::{TerminalId, TerminalInfo}; +use crate::service::{ProcessMode, TerminalId, TerminalInfo}; pub const PROTOCOL_VERSION: u32 = 7; /// Additive capabilities of this daemon. Breaking changes bump `PROTOCOL_VERSION`. +/// `pipe` (non-PTY processes) is Unix-only for now. +#[cfg(unix)] +pub const FEATURES: &[&str] = &["rename", "pipe"]; +#[cfg(not(unix))] pub const FEATURES: &[&str] = &["rename"]; pub fn features() -> Vec { @@ -38,17 +42,31 @@ pub enum Request { ticket: Option, }, PrepareHandoff, + /// Old daemons ignore `mode` and would start a PTY, so clients send + /// `"pipe"` only to a daemon that lists the `pipe` feature. Create { program: String, args: Vec, cwd: PathBuf, + #[serde(default)] title: String, group_id: String, env: std::collections::HashMap, + /// Terminal size; required for `pty`, ignored for `pipe`. + #[serde(default)] cols: u16, + #[serde(default)] rows: u16, + #[serde(default, skip_serializing_if = "ProcessMode::is_pty")] + mode: ProcessMode, + }, + /// Without `mode`, lists PTYs only, so older servers never see pipe processes. + /// `mode` is left out of the JSON when unset: older daemons reject a `list` + /// with any extra key. + List { + #[serde(default, skip_serializing_if = "Option::is_none")] + mode: Option, }, - List, Write { id: TerminalId, attachment_id: Option, @@ -161,6 +179,9 @@ pub enum Response { }, Exited { exit_code: Option, + /// Signal name such as `SIGTERM` when a pipe process was killed by one. + #[serde(default, skip_serializing_if = "Option::is_none")] + signal: Option, final_offset: u64, }, ControllerChanged { @@ -335,4 +356,82 @@ mod tests { serde_json::Value::Null ); } + + #[test] + fn pipe_fields_keep_the_wire_compatible_with_older_peers() { + use crate::service::{ProcessMode, TerminalLifecycle}; + + // An older daemon rejects `list` with any extra key, and an older + // client sends a bare `list` and a `create` with no mode. + assert_eq!( + serde_json::to_value(Request::List { mode: None }).unwrap(), + serde_json::json!({"op": "list"}) + ); + assert!(matches!( + serde_json::from_str::(r#"{"op":"list"}"#).unwrap(), + Request::List { mode: None } + )); + assert!(matches!( + serde_json::from_str::(r#"{"op":"list","mode":"pipe"}"#).unwrap(), + Request::List { + mode: Some(ProcessMode::Pipe) + } + )); + let create = serde_json::json!({ + "op": "create", "program": "sh", "args": [], "cwd": "/", + "title": "t", "group_id": "g", "env": {}, "cols": 80, "rows": 24, + }); + assert!(matches!( + serde_json::from_value::(create.clone()).unwrap(), + Request::Create { + mode: ProcessMode::Pty, + cols: 80, + .. + } + )); + // A PTY create serializes exactly as before. + assert_eq!( + serde_json::to_value(serde_json::from_value::(create.clone()).unwrap()) + .unwrap(), + create + ); + assert!(matches!( + serde_json::from_str::( + r#"{"op":"create","program":"sh","args":[],"cwd":"/","group_id":"g","env":{},"mode":"pipe"}"# + ) + .unwrap(), + Request::Create { + mode: ProcessMode::Pipe, + cols: 0, + rows: 0, + .. + } + )); + // Exits without a signal look exactly as before. + assert_eq!( + serde_json::to_value(Response::Exited { + exit_code: Some(1), + signal: None, + final_offset: 2, + }) + .unwrap(), + serde_json::json!({"type": "exited", "exit_code": 1, "final_offset": 2}) + ); + assert_eq!( + serde_json::to_value(TerminalLifecycle::Exited { + exit_code: Some(1), + signal: None, + }) + .unwrap(), + serde_json::json!({"status": "exited", "exit_code": 1}) + ); + assert_eq!( + serde_json::from_str::(r#"{"status":"exited","exit_code":null}"#) + .unwrap(), + TerminalLifecycle::Exited { + exit_code: None, + signal: None, + } + ); + } } diff --git a/packages/process-daemon/src/service.rs b/packages/process-daemon/src/service.rs index bea34d7fd8f1..803b7c41e5ed 100644 --- a/packages/process-daemon/src/service.rs +++ b/packages/process-daemon/src/service.rs @@ -17,6 +17,8 @@ use serde::{Deserialize, Serialize}; use crate::ghostty::{Format, Terminal, TerminalOptions}; use crate::protocol::AttachmentRole; +#[cfg(unix)] +mod pipe; #[cfg(windows)] mod windows; @@ -33,17 +35,43 @@ const MAX_ROWS_BYTES: usize = 1024 * 1024; pub type TerminalId = u64; +/// How a process is attached. `Pty` runs under a pseudo-terminal parsed by +/// libghostty; `Pipe` runs with stdin closed and stdout and stderr merged into +/// one pipe, and supports only output, replay, subscription and termination. +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ProcessMode { + #[default] + Pty, + Pipe, +} + +impl ProcessMode { + pub fn is_pty(&self) -> bool { + *self == Self::Pty + } +} + #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "snake_case", tag = "status")] pub enum TerminalLifecycle { Running, - Exited { exit_code: Option }, - Failed { message: String }, + Exited { + exit_code: Option, + /// Signal name such as `SIGTERM` when a pipe process was killed by one. + #[serde(default, skip_serializing_if = "Option::is_none")] + signal: Option, + }, + Failed { + message: String, + }, } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct TerminalInfo { pub id: TerminalId, + #[serde(default)] + pub mode: ProcessMode, pub pid: Option, /// Effective title: the user title, else the latest non-empty OSC title, /// else the title given at creation. @@ -102,6 +130,7 @@ pub enum StreamEvent { }, Exited { exit_code: Option, + signal: Option, final_offset: u64, }, ControllerChanged { @@ -149,6 +178,7 @@ pub struct CreateTerminal { pub env: HashMap, pub cols: u16, pub rows: u16, + pub mode: ProcessMode, } impl CreateTerminal { @@ -168,6 +198,7 @@ impl CreateTerminal { env: HashMap::new(), cols: DEFAULT_COLS, rows: DEFAULT_ROWS, + mode: ProcessMode::Pty, }) } } @@ -194,7 +225,13 @@ impl TerminalService { } pub fn create(&self, request: CreateTerminal) -> Result { - validate_size(request.cols, request.rows)?; + match request.mode { + ProcessMode::Pty => validate_size(request.cols, request.rows)?, + #[cfg(not(unix))] + ProcessMode::Pipe => bail!("pipe processes are not supported on this platform"), + #[cfg(unix)] + ProcessMode::Pipe => {} + } if request.program.trim().is_empty() { bail!("terminal program cannot be empty"); } @@ -222,12 +259,20 @@ impl TerminalService { Ok(info) } + /// Lists PTYs; see [`Self::list_mode`] for pipe processes. pub fn list(&self) -> Result> { + self.list_mode(ProcessMode::Pty) + } + + pub fn list_mode(&self, mode: ProcessMode) -> Result> { + // Filter before refreshing, so a busy actor of the other mode never + // delays this listing. let mut handles = self .terminals .lock() .map_err(|_| anyhow!("terminal registry lock poisoned"))? .values() + .filter(|terminal| terminal.mode == mode) .cloned() .collect::>(); handles.sort_by_key(|terminal| terminal.order); @@ -236,7 +281,9 @@ impl TerminalService { // Windows has no foreground query; do not wait behind actor // backpressure just to read its already-cached metadata. #[cfg(not(windows))] - terminal.request(|reply| ActorMessage::RefreshForegroundProcess { reply })?; + if terminal.mode.is_pty() { + terminal.request(|reply| ActorMessage::RefreshForegroundProcess { reply })?; + } terminals.push(terminal.info()); } Ok(terminals) @@ -255,7 +302,7 @@ impl TerminalService { if bytes.len() > MAX_INPUT_BYTES { bail!("input exceeds {MAX_INPUT_BYTES} byte limit"); } - self.get(id)?.request(|reply| ActorMessage::Write { + self.pty(id)?.request(|reply| ActorMessage::Write { attachment_id, bytes, reply, @@ -274,7 +321,7 @@ impl TerminalService { rows: u16, ) -> Result<()> { validate_size(cols, rows)?; - self.get(id)?.request(|reply| ActorMessage::Resize { + self.pty(id)?.request(|reply| ActorMessage::Resize { attachment_id, cols, rows, @@ -290,7 +337,7 @@ impl TerminalService { rows: u16, ) -> Result<()> { validate_size(cols, rows)?; - self.get(id)?.request(|reply| ActorMessage::Control { + self.pty(id)?.request(|reply| ActorMessage::Control { attachment_id, cols, rows, @@ -311,7 +358,7 @@ impl TerminalService { if bytes.len() > MAX_INPUT_BYTES { bail!("input exceeds {MAX_INPUT_BYTES} byte limit"); } - self.get(id)?.request(|reply| ActorMessage::Control { + self.pty(id)?.request(|reply| ActorMessage::Control { attachment_id, cols, rows, @@ -321,19 +368,19 @@ impl TerminalService { } pub fn snapshot(&self, id: TerminalId) -> Result { - self.get(id)? + self.pty(id)? .request(|reply| ActorMessage::Snapshot { reply }) } pub fn read_rows(&self, id: TerminalId, rows: Option) -> Result { - self.get(id)? + self.pty(id)? .request(|reply| ActorMessage::ReadRows { rows, reply }) } /// Sets or clears (`None` or empty) the user title, which takes precedence /// over titles set by the terminal's programs. pub fn rename(&self, id: TerminalId, title: Option) -> Result<()> { - self.get(id)? + self.pty(id)? .request(|reply| ActorMessage::Rename { title, reply }) } @@ -385,6 +432,15 @@ impl TerminalService { terminal.shutdown() } + /// Like `get`, but for requests that need a terminal, which pipe processes lack. + fn pty(&self, id: TerminalId) -> Result> { + let terminal = self.get(id)?; + if !terminal.mode.is_pty() { + bail!("terminal {id} is a pipe process, which has no terminal"); + } + Ok(terminal) + } + fn get(&self, id: TerminalId) -> Result> { self.terminals .lock() @@ -426,21 +482,110 @@ impl Drop for TerminalService { struct TerminalHandle { /// Creation sequence number; orders `list` results. order: u64, + mode: ProcessMode, actor_tx: Sender, /// Disconnects when the actor thread exits; never carries a message. actor_done: Receiver<()>, shutdown_tx: Mutex>>, killer: Mutex>, + /// Dropped at shutdown, before the joins, to cancel a pipe reader that a + /// surviving descendant would otherwise keep blocked. + cancel: Mutex>>, + /// Runs after the joins; reaps a pipe process kept as a zombie until now. + reap: Mutex>>, info: Arc>, joins: Mutex>>>, } +/// The actor thread and the channels other workers share with it. +struct ActorStart { + actor_tx: Sender, + actor_done: Receiver<()>, + shutdown_tx: Sender<()>, + shutdown: Receiver<()>, + thread: JoinHandle<()>, +} + +/// Starts the actor and waits for it to initialize. On error the actor has +/// stopped; the caller still owns, and must stop, the child. +fn start_actor( + id: TerminalId, + mode: ProcessMode, + master: ActorMaster, + writes: Sender, + info: &Arc>, + replay_capacity: usize, + (cols, rows): (u16, u16), +) -> Result { + let (actor_tx, actor_rx) = bounded(ACTOR_QUEUE_CAPACITY); + // Disconnecting this channel wakes every cancellable queue operation, + // even when the actor cannot consume an ordinary shutdown message. + let (shutdown_tx, shutdown) = bounded(0); + let (init_tx, init_rx) = std_mpsc::sync_channel(1); + let actor_info = Arc::clone(info); + let actor_shutdown = shutdown.clone(); + let (actor_done_tx, actor_done) = bounded::<()>(0); + let thread = thread::Builder::new() + .name(format!("opencode-pty-actor-{id}")) + .spawn(move || { + let _actor_done = actor_done_tx; + let result = run_actor(ActorConfig { + mode, + master, + messages: actor_rx, + writes, + shutdown: actor_shutdown, + info: actor_info, + replay_capacity, + cols, + rows, + init_tx, + }); + if let Err(error) = result { + eprintln!("terminal {id} actor failed: {error:#}"); + } + }) + .context("failed to spawn terminal actor")?; + match init_rx + .recv() + .context("terminal actor stopped during initialization")? + { + Ok(()) => Ok(ActorStart { + actor_tx, + actor_done, + shutdown_tx, + shutdown, + thread, + }), + Err(error) => { + drop(shutdown_tx); + let _ = thread.join(); + bail!(error); + } + } +} + impl TerminalHandle { fn spawn( id: TerminalId, order: u64, request: CreateTerminal, replay_capacity: usize, + ) -> Result { + match request.mode { + ProcessMode::Pty => Self::spawn_pty(id, order, request, replay_capacity), + #[cfg(unix)] + ProcessMode::Pipe => Self::spawn_pipe(id, order, request, replay_capacity), + #[cfg(not(unix))] + ProcessMode::Pipe => bail!("pipe processes are not supported on this platform"), + } + } + + fn spawn_pty( + id: TerminalId, + order: u64, + request: CreateTerminal, + replay_capacity: usize, ) -> Result { let pty_system = native_pty_system(); let pair = pty_system @@ -477,82 +622,41 @@ impl TerminalHandle { .context("failed to take PTY writer")?; drop(pair.slave); - let info = Arc::new(RwLock::new(TerminalInfo { - id, - pid, - title: request.title, - user_title: None, - foreground_process: None, - group_id: request.group_id, - command: std::iter::once(request.program) - .chain(request.args) - .collect(), - cwd: request.cwd, - cols: request.cols, - rows: request.rows, - lifecycle: TerminalLifecycle::Running, - output_head: 0, - output_tail: 0, - })); + let info = new_info(id, ProcessMode::Pty, pid, &request); - let (actor_tx, actor_rx) = bounded(ACTOR_QUEUE_CAPACITY); let (writer_tx, writer_rx) = bounded(WRITER_QUEUE_CAPACITY); - // Disconnecting this channel wakes every cancellable queue operation, - // even when the actor cannot consume an ordinary shutdown message. - let (shutdown_tx, shutdown) = bounded(0); #[cfg(windows)] let (close_tx, close_rx) = bounded(1); - let (init_tx, init_rx) = std_mpsc::sync_channel(1); - - let actor_info = Arc::clone(&info); - let actor_writer_tx = writer_tx.clone(); - let actor_shutdown = shutdown.clone(); - let (actor_done_tx, actor_done) = bounded::<()>(0); - let actor_thread = thread::Builder::new() - .name(format!("opencode-pty-actor-{id}")) - .spawn(move || { - let _actor_done = actor_done_tx; - let result = run_actor(ActorConfig { - master: ActorMaster { - pty: Some(pair.master), - #[cfg(windows)] - close_tx, - }, - messages: actor_rx, - writes: actor_writer_tx, - shutdown: actor_shutdown, - info: actor_info, - replay_capacity, - cols: request.cols, - rows: request.rows, - init_tx, - }); - if let Err(error) = result { - eprintln!("terminal {id} actor failed: {error:#}"); - } - }) - .context("failed to spawn terminal actor")?; - - match init_rx - .recv() - .context("terminal actor stopped during initialization")? - { - Ok(()) => {} + let actor = start_actor( + id, + ProcessMode::Pty, + ActorMaster { + pty: Some(pair.master), + #[cfg(windows)] + close_tx, + }, + writer_tx, + &info, + replay_capacity, + (request.cols, request.rows), + ); + let actor = match actor { + Ok(actor) => actor, Err(error) => { let mut killer = killer; let _ = killer.kill(); - let _ = actor_thread.join(); - bail!(error); + return Err(error); } - } + }; - let writer_actor_tx = actor_tx.clone(); + let writer_actor_tx = actor.actor_tx.clone(); + let writer_shutdown = actor.shutdown.clone(); let writer_thread = thread::Builder::new() .name(format!("opencode-pty-writer-{id}")) - .spawn(move || run_writer(writer, writer_rx, writer_actor_tx, shutdown)) + .spawn(move || run_writer(writer, writer_rx, writer_actor_tx, writer_shutdown)) .context("failed to spawn terminal writer")?; - let reader_actor_tx = actor_tx.clone(); + let reader_actor_tx = actor.actor_tx.clone(); let reader_thread = thread::Builder::new() .name(format!("opencode-pty-reader-{id}")) .spawn(move || { @@ -594,7 +698,7 @@ impl TerminalHandle { }) .context("failed to spawn terminal reader")?; - let wait_actor_tx = actor_tx.clone(); + let wait_actor_tx = actor.actor_tx.clone(); let wait_thread = thread::Builder::new() .name(format!("opencode-pty-wait-{id}")) .spawn(move || { @@ -613,13 +717,16 @@ impl TerminalHandle { Ok(Self { order, - actor_tx, - actor_done, - shutdown_tx: Mutex::new(Some(shutdown_tx)), + mode: ProcessMode::Pty, + actor_tx: actor.actor_tx, + actor_done: actor.actor_done, + shutdown_tx: Mutex::new(Some(actor.shutdown_tx)), killer: Mutex::new(killer), + cancel: Mutex::new(None), + reap: Mutex::new(None), info, joins: Mutex::new(Some(vec![ - actor_thread, + actor.thread, writer_thread, reader_thread, wait_thread, @@ -659,13 +766,59 @@ impl TerminalHandle { if let Ok(mut killer) = self.killer.lock() { let _ = killer.kill(); } + self.cancel + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .take(); for join in handles { let _ = join.join(); } + if let Some(reap) = self + .reap + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + .take() + { + reap(); + } Ok(()) } } +fn new_info( + id: TerminalId, + mode: ProcessMode, + pid: Option, + request: &CreateTerminal, +) -> Arc> { + let title = match mode { + ProcessMode::Pipe if request.title.is_empty() => PathBuf::from(&request.program) + .file_name() + .and_then(|value| value.to_str()) + .unwrap_or(&request.program) + .to_string(), + _ => request.title.clone(), + }; + Arc::new(RwLock::new(TerminalInfo { + id, + mode, + pid, + title, + user_title: None, + foreground_process: None, + group_id: request.group_id.clone(), + command: std::iter::once(request.program.clone()) + .chain(request.args.iter().cloned()) + .collect(), + cwd: request.cwd.clone(), + cols: request.cols, + rows: request.rows, + lifecycle: TerminalLifecycle::Running, + output_head: 0, + output_tail: 0, + })) +} + type Reply = Sender; /// A request accepted into the actor queue may never be handled: shutdown @@ -687,6 +840,9 @@ enum ActorMessage { ReaderFailed(String), WriterFailed(String), ChildExited(Result, String>), + /// A pipe process ended by a signal; the value is the signal's name. + #[cfg(unix)] + ChildKilled(String), #[cfg(not(windows))] RefreshForegroundProcess { reply: Reply>, @@ -800,6 +956,7 @@ impl Drop for ActorMaster { } struct ActorConfig { + mode: ProcessMode, master: ActorMaster, messages: Receiver, writes: Sender, @@ -813,6 +970,7 @@ struct ActorConfig { fn run_actor(config: ActorConfig) -> Result<()> { let ActorConfig { + mode, master, messages, writes, @@ -825,11 +983,15 @@ fn run_actor(config: ActorConfig) -> Result<()> { } = config; #[cfg(windows)] let mut master = master; - let mut terminal = Terminal::new(TerminalOptions { - cols, - rows, - max_scrollback: replay_capacity, - })?; + // Pipe processes have no terminal: no emulator, replies, title or size. + let mut terminal = match mode { + ProcessMode::Pty => Some(Terminal::new(TerminalOptions { + cols, + rows, + max_scrollback: replay_capacity, + })?), + ProcessMode::Pipe => None, + }; let create_title = info .read() @@ -841,7 +1003,7 @@ fn run_actor(config: ActorConfig) -> Result<()> { let mut subscribers = HashMap::::new(); let mut controller = None::; let mut controller_generation = 0_u64; - let mut child_exit = None::, String>>; + let mut child_exit = None::, Option), String>>; let mut reader_eof = false; let mut final_exit = None::; let mut pending_message = None; @@ -865,18 +1027,20 @@ fn run_actor(config: ActorConfig) -> Result<()> { } Ok(ActorMessage::Output(bytes)) => { let (start, end) = replay.append(&bytes); - terminal.vt_write(&bytes); - // Once the Windows master is handed off for closing, the writer - // may already have stopped; replies to the closing console are - // discarded so final output still reaches the real EOF. - #[cfg(windows)] - let closing = master.get().is_err(); - #[cfg(not(windows))] - let closing = false; - if closing { - terminal.take_writes(); - } else if let Err(error) = forward_replies(&mut terminal, &writes, &shutdown) { - break Err(error); + if let Some(terminal) = terminal.as_mut() { + terminal.vt_write(&bytes); + // Once the Windows master is handed off for closing, the writer + // may already have stopped; replies to the closing console are + // discarded so final output still reaches the real EOF. + #[cfg(windows)] + let closing = master.get().is_err(); + #[cfg(not(windows))] + let closing = false; + if closing { + terminal.take_writes(); + } else if let Err(error) = forward_replies(terminal, &writes, &shutdown) { + break Err(error); + } } update_offsets(&info, &replay); broadcast( @@ -886,7 +1050,7 @@ fn run_actor(config: ActorConfig) -> Result<()> { &mut controller_generation, StreamEvent::Output { start, end, bytes }, ); - if let Some(title) = terminal.take_title() { + if let Some(title) = terminal.as_mut().and_then(Terminal::take_title) { osc_title = Some(title).filter(|title| !title.is_empty()); if let Ok(Some(title)) = refresh_title(&info, None, osc_title.as_deref(), &create_title) @@ -922,6 +1086,7 @@ fn run_actor(config: ActorConfig) -> Result<()> { }) => { let result = (|| { authorize_controller(&controller, attachment_id.as_deref())?; + let terminal = emulator(&mut terminal)?; master.get()?.resize(PtySize { rows, cols, @@ -929,14 +1094,14 @@ fn run_actor(config: ActorConfig) -> Result<()> { pixel_height: 0, })?; let resized = terminal.resize(cols, rows); - forward_replies(&mut terminal, &writes, &shutdown)?; + forward_replies(terminal, &writes, &shutdown)?; resized?; if let Ok(mut value) = info.write() { value.cols = cols; value.rows = rows; } let generation = controller_generation; - let checkpoint = format_terminal(&terminal, Format::Vt)?.into_bytes(); + let checkpoint = format_terminal(terminal, Format::Vt)?.into_bytes(); broadcast( &shutdown, &mut subscribers, @@ -993,6 +1158,7 @@ fn run_actor(config: ActorConfig) -> Result<()> { .map(|value| value.cols != cols || value.rows != rows) .unwrap_or(true); if changed { + let terminal = emulator(&mut terminal)?; master.get()?.resize(PtySize { rows, cols, @@ -1000,14 +1166,14 @@ fn run_actor(config: ActorConfig) -> Result<()> { pixel_height: 0, })?; let resized = terminal.resize(cols, rows); - forward_replies(&mut terminal, &writes, &shutdown)?; + forward_replies(terminal, &writes, &shutdown)?; resized?; if let Ok(mut value) = info.write() { value.cols = cols; value.rows = rows; } let generation = controller_generation; - let checkpoint = format_terminal(&terminal, Format::Vt)?.into_bytes(); + let checkpoint = format_terminal(terminal, Format::Vt)?.into_bytes(); broadcast( &shutdown, &mut subscribers, @@ -1031,12 +1197,15 @@ fn run_actor(config: ActorConfig) -> Result<()> { let _ = reply.send(result); } Ok(ActorMessage::Snapshot { reply }) => { - let _ = reply.send(make_snapshot(&terminal, &info)); + let _ = reply.send( + emulator(&mut terminal).and_then(|terminal| make_snapshot(terminal, &info)), + ); } Ok(ActorMessage::ReadRows { rows, reply }) => { let result = (|| { + let terminal = emulator(&mut terminal)?; Ok(TerminalRows { - lines: format_rows(&terminal, rows)?, + lines: format_rows(terminal, rows)?, terminal: info .read() .map_err(|_| anyhow!("terminal info lock poisoned"))? @@ -1166,8 +1335,12 @@ fn run_actor(config: ActorConfig) -> Result<()> { ); } } + #[cfg(unix)] + Ok(ActorMessage::ChildKilled(signal)) => { + child_exit = Some(Ok((None, Some(signal)))); + } Ok(ActorMessage::ChildExited(result)) => { - child_exit = Some(result); + child_exit = Some(result.map(|exit_code| (exit_code, None))); // ConPTY keeps its output pipe open until HPCON is closed. // The wait worker closes it while this actor and the reader // keep processing output, including the real final EOF. @@ -1195,15 +1368,16 @@ fn run_actor(config: ActorConfig) -> Result<()> { && final_exit.is_none() && let Some(result) = child_exit.take() { - let exit_code = result.as_ref().ok().copied().flatten(); + let (exit_code, signal) = result.clone().unwrap_or_default(); if let Ok(mut value) = info.write() { value.lifecycle = match result { - Ok(exit_code) => TerminalLifecycle::Exited { exit_code }, + Ok((exit_code, signal)) => TerminalLifecycle::Exited { exit_code, signal }, Err(message) => TerminalLifecycle::Failed { message }, }; } let event = StreamEvent::Exited { exit_code, + signal, final_offset: replay.tail, }; broadcast( @@ -1233,6 +1407,12 @@ fn run_actor(config: ActorConfig) -> Result<()> { } } +fn emulator(terminal: &mut Option) -> Result<&mut Terminal> { + terminal + .as_mut() + .ok_or_else(|| anyhow!("pipe processes have no terminal")) +} + fn receive_actor_message( messages: &Receiver, pending: &mut Option, @@ -1736,6 +1916,7 @@ mod tests { fn test_info() -> TerminalInfo { TerminalInfo { id: 1, + mode: ProcessMode::Pty, pid: None, title: "test".into(), user_title: None, @@ -1772,6 +1953,7 @@ mod tests { let actor = thread::spawn(move || { let _actor_done = actor_done_tx; run_actor(ActorConfig { + mode: ProcessMode::Pty, master: ActorMaster { pty: Some(Box::new(TestMaster)), #[cfg(windows)] @@ -1792,10 +1974,13 @@ mod tests { ( TerminalHandle { order: 0, + mode: ProcessMode::Pty, actor_tx, actor_done, shutdown_tx: Mutex::new(Some(shutdown_tx)), killer: Mutex::new(Box::new(TestKiller)), + cancel: Mutex::new(None), + reap: Mutex::new(None), info, joins: Mutex::new(Some(vec![ actor, @@ -1859,7 +2044,8 @@ mod tests { assert_eq!( snapshot.info.lifecycle, TerminalLifecycle::Exited { - exit_code: Some(23) + exit_code: Some(23), + signal: None, } ); if !eof_first { @@ -1926,6 +2112,7 @@ mod tests { events.as_slice(), [StreamEvent::Exited { exit_code: Some(23), + signal: None, final_offset: 5 }] )); diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs new file mode 100644 index 000000000000..bfead87b138e --- /dev/null +++ b/packages/process-daemon/src/service/pipe.rs @@ -0,0 +1,323 @@ +//! Non-PTY ("pipe") processes on Unix. +//! +//! 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 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. +//! - 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 polls a cancel pipe too, so shutdown never waits on a pipe that +//! a descendant which left the process group still holds open. + +use std::io::{self, PipeReader, Read}; +use std::os::fd::AsFd; +use std::os::unix::process::CommandExt; +use std::process::{Command, Stdio}; +use std::sync::{Arc, Mutex, PoisonError}; +use std::thread; +use std::time::{Duration, Instant}; + +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, CreateTerminal, ProcessMode, TerminalHandle, TerminalId, new_info, + start_actor, +}; + +/// How long output is still read after the child exits. +const DRAIN_AFTER_EXIT: Duration = Duration::from_secs(1); + +impl TerminalHandle { + pub(super) fn spawn_pipe( + id: TerminalId, + order: u64, + request: CreateTerminal, + replay_capacity: usize, + ) -> Result { + 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 mut command = Command::new(&request.program); + command + .args(&request.args) + .current_dir(&request.cwd) + .envs(&request.env) + .stdin(Stdio::null()) + .stdout(output_writer) + .stderr(stderr) + .process_group(0); + let child = 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); + + match Self::start_pipe(id, order, &request, replay_capacity, &process, output) { + Ok(handle) => Ok(handle), + Err(error) => { + process.kill(); + process.reap(); + Err(error) + } + } + } + + fn start_pipe( + id: TerminalId, + order: u64, + request: &CreateTerminal, + replay_capacity: usize, + process: &Arc, + output: PipeReader, + ) -> Result { + let info = new_info(id, ProcessMode::Pipe, Some(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 = start_actor( + id, + ProcessMode::Pipe, + ActorMaster { pty: None }, + writes, + &info, + replay_capacity, + (request.cols, request.rows), + )?; + let (cancel, cancel_writer) = io::pipe().context("failed to create cancel pipe")?; + let (exited, exited_writer) = io::pipe().context("failed to create exit pipe")?; + + let reader_actor_tx = actor.actor_tx.clone(); + let reader = thread::Builder::new() + .name(format!("opencode-pty-reader-{id}")) + .spawn(move || read_output(output, cancel, exited, reader_actor_tx)); + let wait_actor_tx = actor.actor_tx.clone(); + let pid = process.pid; + let waiter = 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); + }); + let mut joins = vec![actor.thread]; + let started = (|| -> Result<()> { + joins.push(reader.context("failed to spawn terminal reader")?); + joins.push(waiter.context("failed to spawn terminal child waiter")?); + Ok(()) + })(); + if let Err(error) = started { + // Stop what did start: the actor through its shutdown channel, the + // reader through its cancel pipe. The caller kills and reaps. + drop(actor.shutdown_tx); + drop(cancel_writer); + for join in joins { + let _ = join.join(); + } + 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()))), + info, + joins: Mutex::new(Some(joins)), + }) + } +} + +/// A pipe child that stays a zombie after it exits, until `reap`. +#[derive(Debug)] +struct Process { + pid: u32, + reaped: Mutex, +} + +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 this process group ID reserved. + unsafe { libc::killpg(pid, libc::SIGKILL) }; + } + } + + /// 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; + } +} + +#[derive(Debug)] +struct GroupKiller(Arc); + +impl ChildKiller for GroupKiller { + fn kill(&mut self) -> io::Result<()> { + self.0.kill(); + Ok(()) + } + + fn clone_killer(&self) -> Box { + Box::new(Self(Arc::clone(&self.0))) + } +} + +enum Exit { + Code(u32), + Signal(String), +} + +/// Waits for the child to exit without reaping it. +fn wait_exit(pid: u32) -> io::Result { + 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 { + nix::sys::signal::Signal::try_from(signal) + .map(|signal| signal.as_str().to_string()) + .unwrap_or_else(|_| format!("SIG{signal}")) +} + +/// Forwards output until EOF, until [`DRAIN_AFTER_EXIT`] after `exited` +/// closes, or until `cancel` closes. +fn read_output( + output: PipeReader, + cancel: PipeReader, + exited: PipeReader, + actor: Sender, +) { + let mut output = output; + let mut buffer = [0_u8; 8192]; + let mut deadline = None::; + loop { + let timeout = match deadline { + None => PollTimeout::NONE, + Some(deadline) => { + let left = deadline.saturating_duration_since(Instant::now()); + if left.is_zero() { + let _ = actor.send(ActorMessage::ReaderEof); + return; + } + // 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) + } + }; + 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) => { + 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) + }) + }; + if ready(&fds[1]) { + return; + } + if count == 3 && ready(&fds[2]) { + deadline = Some(Instant::now() + DRAIN_AFTER_EXIT); + } + if ready(&fds[0]) { + match output.read(&mut buffer) { + Ok(0) => { + let _ = actor.send(ActorMessage::ReaderEof); + return; + } + Ok(length) => { + if actor + .send(ActorMessage::Output(buffer[..length].to_vec())) + .is_err() + { + return; + } + } + Err(error) if error.kind() == io::ErrorKind::Interrupted => {} + Err(error) => { + let _ = actor.send(ActorMessage::ReaderFailed(error.to_string())); + return; + } + } + } + } +} diff --git a/packages/process-daemon/src/transport/unix.rs b/packages/process-daemon/src/transport/unix.rs index d66ead3da36d..5be4764267a6 100644 --- a/packages/process-daemon/src/transport/unix.rs +++ b/packages/process-daemon/src/transport/unix.rs @@ -391,6 +391,7 @@ mod tests { &mut server, &Response::Exited { exit_code: Some(0), + signal: None, final_offset: 123, }, ) @@ -401,6 +402,7 @@ mod tests { read_frame(&mut client).unwrap(), Response::Exited { exit_code: Some(0), + signal: None, final_offset: 123, } )); diff --git a/packages/process-daemon/src/transport/windows.rs b/packages/process-daemon/src/transport/windows.rs index edfdc3d92df6..cc9d04405d23 100644 --- a/packages/process-daemon/src/transport/windows.rs +++ b/packages/process-daemon/src/transport/windows.rs @@ -703,6 +703,7 @@ mod tests { &mut server, &Response::Exited { exit_code: Some(0), + signal: None, final_offset: 123, }, ) diff --git a/packages/process-daemon/tests/ownership.rs b/packages/process-daemon/tests/ownership.rs index 0cade5397a31..d14d6997564a 100644 --- a/packages/process-daemon/tests/ownership.rs +++ b/packages/process-daemon/tests/ownership.rs @@ -110,6 +110,7 @@ impl Daemon { env: Default::default(), cols: 80, rows: 24, + mode: Default::default(), }) { Response::Created { terminal } => terminal, response => panic!("create: {response:?}"), @@ -261,6 +262,7 @@ fn subscription_delivers_final_exit_frame_before_closing() { Response::Exited { exit_code, final_offset, + .. } => { assert_eq!(exit_code, Some(0)); assert_eq!(final_offset, output.len() as u64); @@ -313,10 +315,10 @@ fn rename_is_advertised_and_served() { use opencode_pty::protocol::{SubscriptionEvent, read_subscription_event}; let mut daemon = Daemon::start(); - assert_eq!(daemon.registration.features, ["rename"]); + assert_eq!(daemon.registration.features, ["rename", "pipe"]); assert!(matches!( daemon.request(Request::Ping), - Response::Pong { features, .. } if features == ["rename"] + Response::Pong { features, .. } if features == ["rename", "pipe"] )); let (owner, response) = daemon.own(None); assert!(matches!(response, Response::Owned)); @@ -352,7 +354,7 @@ fn rename_is_advertised_and_served() { } } assert!( - matches!(daemon.request(Request::List), Response::Terminals { terminals } + matches!(daemon.request(Request::List { mode: None }), Response::Terminals { terminals } if terminals.len() == 1 && terminals[0].title == "renamed" && terminals[0].user_title.as_deref() == Some("renamed")) ); @@ -408,7 +410,7 @@ fn handoff_preserves_daemon_and_terminal_and_shutdown_overrides_it() { if instance_id == daemon.registration.instance_id && pid == daemon.child.id()) ); assert!( - matches!(daemon.request(Request::List), Response::Terminals { terminals } + matches!(daemon.request(Request::List { mode: None }), Response::Terminals { terminals } if terminals.len() == 1 && terminals[0].id == terminal.id && terminals[0].pid == terminal.pid) ); assert!(matches!( @@ -552,7 +554,8 @@ fn stop_signals_stop_terminals_and_clean_up() { assert_eq!(unsafe { libc::kill(probe.pid.unwrap() as i32, signal) }, 0); let deadline = Instant::now() + Duration::from_secs(3); loop { - let Response::Terminals { terminals } = daemon.request(Request::List) else { + let Response::Terminals { terminals } = daemon.request(Request::List { mode: None }) + else { panic!("list failed"); }; let exited = terminals.iter().any(|value| { @@ -575,3 +578,91 @@ fn stop_signals_stop_terminals_and_clean_up() { assert_terminal_stopped(&terminal); } } + +#[test] +fn pipe_processes_survive_handoff_and_stay_out_of_default_lists() { + use opencode_pty::protocol::{SubscriptionEvent, read_subscription_event}; + use opencode_pty::service::ProcessMode; + + let mut daemon = Daemon::start(); + let (owner, response) = daemon.own(None); + assert!(matches!(response, Response::Owned)); + let terminal = daemon.terminal("exec cat"); + let Response::Created { terminal: process } = daemon.request(Request::Create { + program: "/bin/sh".into(), + args: vec![ + "-c".into(), + "echo before; sleep 1; echo after; exit 4".into(), + ], + cwd: daemon.directory.clone(), + title: String::new(), + group_id: "ownership".into(), + env: Default::default(), + cols: 0, + rows: 0, + mode: ProcessMode::Pipe, + }) else { + panic!("create pipe"); + }; + assert_eq!(process.mode, ProcessMode::Pipe); + let ids = |mode| match daemon.request(Request::List { mode }) { + Response::Terminals { terminals } => terminals + .into_iter() + .map(|terminal| terminal.id) + .collect::>(), + response => panic!("list: {response:?}"), + }; + assert_eq!(ids(None), vec![terminal.id]); + assert_eq!(ids(Some(ProcessMode::Pty)), vec![terminal.id]); + assert_eq!(ids(Some(ProcessMode::Pipe)), vec![process.id]); + assert!(matches!( + daemon.request(Request::Snapshot { id: process.id }), + Response::Error { message } if message.contains("pipe process") + )); + + // Restart handoff: the old owner goes away while the process runs. + let mut old = owner; + let Response::Handoff { ticket, .. } = daemon.send(&mut old, Request::PrepareHandoff) else { + panic!("handoff"); + }; + let (successor, response) = daemon.own(Some(ticket)); + assert!(matches!(response, Response::Owned)); + drop(old); + + let mut subscription = daemon.connect(); + let Response::Attached { replay_base64, .. } = daemon.send( + &mut subscription, + Request::Subscribe { + id: process.id, + offset: 0, + attachment_id: "pipe-observer".into(), + role: AttachmentRole::Observer, + takeover: false, + }, + ) else { + panic!("subscribe"); + }; + let mut output = base64::engine::general_purpose::STANDARD + .decode(replay_base64) + .unwrap(); + let exit = loop { + match read_subscription_event(&mut subscription).unwrap() { + SubscriptionEvent::Output { bytes, .. } => output.extend(bytes), + SubscriptionEvent::Response(response) => { + if let Response::Exited { exit_code, .. } = *response { + break exit_code; + } + } + } + }; + assert_eq!(exit, Some(4)); + assert_eq!(String::from_utf8(output).unwrap(), "before\nafter\n"); + assert!(matches!( + daemon.request(Request::Terminate { id: process.id }), + Response::Ok + )); + drop(subscription); + drop(successor); + daemon.wait(); + assert_terminal_stopped(&terminal); +} diff --git a/packages/process-daemon/tests/pipe.rs b/packages/process-daemon/tests/pipe.rs new file mode 100644 index 000000000000..1484af004eba --- /dev/null +++ b/packages/process-daemon/tests/pipe.rs @@ -0,0 +1,294 @@ +#![cfg(unix)] + +use std::env; +use std::time::{Duration, Instant}; + +use opencode_pty::protocol::AttachmentRole; +use opencode_pty::service::{ + CreateTerminal, ProcessMode, StreamEvent, TerminalId, TerminalLifecycle, TerminalService, +}; + +fn pipe(script: &str) -> CreateTerminal { + CreateTerminal { + program: "/bin/sh".to_string(), + args: vec!["-c".to_string(), script.to_string()], + cwd: env::current_dir().expect("cwd"), + title: String::new(), + group_id: "pipe".to_string(), + env: std::collections::HashMap::new(), + cols: 0, + rows: 0, + mode: ProcessMode::Pipe, + } +} + +struct Finished { + output: String, + exit_code: Option, + signal: Option, + final_offset: u64, + elapsed: Duration, +} + +/// Collects output from offset 0 until the exit event. +fn run(service: &TerminalService, id: TerminalId) -> Finished { + let started = Instant::now(); + let attachment = service + .attach(id, 0, "test".into(), AttachmentRole::Observer, false) + .unwrap(); + let mut output = attachment.replay.bytes.clone(); + loop { + match attachment + .events + .recv_timeout(Duration::from_secs(10)) + .expect("pipe process did not exit") + { + StreamEvent::Output { bytes, .. } => output.extend(bytes), + StreamEvent::Exited { + exit_code, + signal, + final_offset, + } => { + return Finished { + output: String::from_utf8(output).unwrap(), + exit_code, + signal, + final_offset, + elapsed: started.elapsed(), + }; + } + _ => {} + } + } +} + +fn alive(pid: i32) -> bool { + // SAFETY: signal zero only checks whether the PID exists. + unsafe { libc::kill(pid, 0) == 0 } +} + +fn wait_dead(pid: i32) { + let deadline = Instant::now() + Duration::from_secs(5); + while alive(pid) { + assert!(Instant::now() < deadline, "process {pid} is still running"); + std::thread::sleep(Duration::from_millis(10)); + } +} + +#[test] +fn reports_ordered_output_and_exit_code_without_a_terminal() { + let service = TerminalService::default(); + let info = service + .create(pipe( + "for i in 1 2 3; do echo out$i; echo err$i >&2; done + [ -t 1 ] && echo tty || echo notty + cat; echo stdin-closed + exit 7", + )) + .unwrap(); + assert_eq!(info.mode, ProcessMode::Pipe); + assert_eq!(info.title, "sh"); + let finished = run(&service, info.id); + assert_eq!( + finished.output, + "out1\nerr1\nout2\nerr2\nout3\nerr3\nnotty\nstdin-closed\n" + ); + assert_eq!(finished.exit_code, Some(7)); + assert_eq!(finished.signal, None); + assert_eq!(finished.final_offset, finished.output.len() as u64); + let listed = service.list_mode(ProcessMode::Pipe).unwrap(); + assert_eq!( + listed[0].lifecycle, + TerminalLifecycle::Exited { + exit_code: Some(7), + signal: None + } + ); + service.terminate(info.id).unwrap(); +} + +#[test] +fn sigpipe_is_not_inherited_as_ignored() { + // Rust ignores SIGPIPE in the daemon. A child that inherited that could + // never die from it, so `producer | head` pipelines would misbehave. + let service = TerminalService::default(); + let info = service + .create(pipe("kill -PIPE $$; echo survived")) + .unwrap(); + let finished = run(&service, info.id); + assert_eq!(finished.output, ""); + assert_eq!(finished.signal.as_deref(), Some("SIGPIPE")); + service.terminate(info.id).unwrap(); +} + +#[test] +fn reports_the_signal_that_ended_the_process() { + let service = TerminalService::default(); + let info = service.create(pipe("kill -TERM $$")).unwrap(); + let finished = run(&service, info.id); + assert_eq!(finished.exit_code, None); + assert_eq!(finished.signal.as_deref(), Some("SIGTERM")); + service.terminate(info.id).unwrap(); +} + +#[test] +fn fast_exit_without_output_is_reported() { + let service = TerminalService::default(); + let info = service.create(pipe("exit 3")).unwrap(); + let finished = run(&service, info.id); + assert_eq!(finished.output, ""); + assert_eq!(finished.exit_code, Some(3)); + assert_eq!(finished.final_offset, 0); + service.terminate(info.id).unwrap(); +} + +#[test] +fn a_descendant_holding_the_pipe_delays_the_exit_by_at_most_the_drain() { + let service = TerminalService::default(); + let info = service + .create(pipe("sleep 30 & echo $!; echo started")) + .unwrap(); + let finished = run(&service, info.id); + assert!( + finished.output.ends_with("started\n"), + "{}", + finished.output + ); + assert_eq!(finished.exit_code, Some(0)); + assert!( + finished.elapsed < Duration::from_secs(4), + "{:?}", + finished.elapsed + ); + let sleeper: i32 = finished.output.lines().next().unwrap().parse().unwrap(); + assert!(alive(sleeper)); + // Terminate kills the rest of the process group. + service.terminate(info.id).unwrap(); + wait_dead(sleeper); +} + +#[test] +fn terminate_does_not_wait_for_a_descendant_that_left_the_group() { + let service = TerminalService::default(); + let info = service + .create(pipe( + "perl -e 'use POSIX; POSIX::setsid(); print \"$$\\n\"; $| = 1; sleep 30' & wait", + )) + .unwrap(); + let attachment = service + .attach(info.id, 0, "test".into(), AttachmentRole::Observer, false) + .unwrap(); + let mut output = attachment.replay.bytes.clone(); + while !output.ends_with(b"\n") { + if let StreamEvent::Output { bytes, .. } = attachment + .events + .recv_timeout(Duration::from_secs(5)) + .unwrap() + { + output.extend(bytes); + } + } + let escaped: i32 = String::from_utf8(output).unwrap().trim().parse().unwrap(); + drop(attachment); + let started = Instant::now(); + service.terminate(info.id).unwrap(); + assert!(started.elapsed() < Duration::from_secs(2)); + // It left the group, so it survives; clean it up here. + assert!(alive(escaped)); + // SAFETY: the PID belongs to this test's own descendant. + unsafe { libc::kill(escaped, libc::SIGKILL) }; +} + +#[test] +fn an_exited_process_is_kept_until_terminate() { + let service = TerminalService::default(); + let info = service.create(pipe("exit 0")).unwrap(); + run(&service, info.id); + let pid = info.pid.unwrap() as i32; + // The zombie keeps the PID and process group reserved. + assert!(alive(pid)); + assert_eq!(service.list_mode(ProcessMode::Pipe).unwrap().len(), 1); + service.terminate(info.id).unwrap(); + assert!(!alive(pid)); + assert!(service.list_mode(ProcessMode::Pipe).unwrap().is_empty()); +} + +#[test] +fn terminate_kills_the_process_group() { + let service = TerminalService::default(); + let info = service.create(pipe("sleep 30 & echo $!; wait")).unwrap(); + let attachment = service + .attach(info.id, 0, "test".into(), AttachmentRole::Observer, false) + .unwrap(); + let mut output = attachment.replay.bytes.clone(); + while !output.ends_with(b"\n") { + if let StreamEvent::Output { bytes, .. } = attachment + .events + .recv_timeout(Duration::from_secs(5)) + .unwrap() + { + output.extend(bytes); + } + } + let child: i32 = String::from_utf8(output).unwrap().trim().parse().unwrap(); + drop(attachment); + service.terminate(info.id).unwrap(); + wait_dead(child); + assert!(!alive(info.pid.unwrap() as i32)); +} + +#[test] +fn terminal_requests_are_rejected_and_lists_are_separate() { + let service = TerminalService::default(); + let pty = service + .create(CreateTerminal { + cols: 80, + rows: 24, + mode: ProcessMode::Pty, + ..pipe("exec cat") + }) + .unwrap(); + let pipe = service.create(pipe("exec sleep 30")).unwrap(); + let id = pipe.id; + for result in [ + service.write(id, b"x".to_vec()), + service.resize(id, 80, 24), + service.control(id, "a".into(), 80, 24), + service.input(id, "a".into(), 80, 24, b"x".to_vec()), + service.rename(id, Some("x".into())), + service.snapshot(id).map(|_| ()), + service.read_rows(id, None).map(|_| ()), + ] { + let error = result.unwrap_err().to_string(); + assert!(error.contains("pipe process"), "{error}"); + } + assert!(service.replay(id, 0).is_ok()); + let listed = |mode| { + service + .list_mode(mode) + .unwrap() + .into_iter() + .map(|info| info.id) + .collect::>() + }; + assert_eq!(listed(ProcessMode::Pty), vec![pty.id]); + assert_eq!(listed(ProcessMode::Pipe), vec![pipe.id]); + assert_eq!(service.list().unwrap().len(), 1); + service.terminate(pty.id).unwrap(); + service.terminate(pipe.id).unwrap(); +} + +#[test] +fn pipe_processes_need_no_terminal_size() { + let service = TerminalService::default(); + assert!( + service + .create(CreateTerminal { + mode: ProcessMode::Pty, + ..pipe("true") + }) + .is_err() + ); + let info = service.create(pipe("true")).unwrap(); + service.terminate(info.id).unwrap(); +} diff --git a/packages/process-daemon/tests/runtime.rs b/packages/process-daemon/tests/runtime.rs index 27eab0d70c25..06d975f65502 100644 --- a/packages/process-daemon/tests/runtime.rs +++ b/packages/process-daemon/tests/runtime.rs @@ -297,6 +297,7 @@ fn check_normal_exit(code: u32) { StreamEvent::Exited { exit_code, final_offset, + .. } => { assert_eq!(exit_code, Some(code)); assert_eq!(final_offset, offset); @@ -314,7 +315,8 @@ fn check_normal_exit(code: u32) { assert_eq!( snapshot.info.lifecycle, TerminalLifecycle::Exited { - exit_code: Some(code) + exit_code: Some(code), + signal: None, } ); assert_eq!(snapshot.info.output_tail, offset); diff --git a/packages/process-daemon/tests/service.rs b/packages/process-daemon/tests/service.rs index 7293c6ae6a94..2988f3952b44 100644 --- a/packages/process-daemon/tests/service.rs +++ b/packages/process-daemon/tests/service.rs @@ -69,6 +69,7 @@ fn command(script: &str) -> CreateTerminal { env: std::collections::HashMap::new(), cols: 80, rows: 24, + mode: Default::default(), } } diff --git a/packages/process-daemon/tests/support/resize_fixture.rs b/packages/process-daemon/tests/support/resize_fixture.rs index e09ef8ab36ce..70f4577d06e1 100644 --- a/packages/process-daemon/tests/support/resize_fixture.rs +++ b/packages/process-daemon/tests/support/resize_fixture.rs @@ -43,6 +43,7 @@ impl ResizeFixture { env: [(CHILD.into(), listener.local_addr().unwrap().to_string())].into(), cols: 80, rows: 24, + mode: Default::default(), }) .unwrap(); diff --git a/packages/process-daemon/tests/support/terminal_fixture.rs b/packages/process-daemon/tests/support/terminal_fixture.rs index cdea300d2988..47b2f294a5e7 100644 --- a/packages/process-daemon/tests/support/terminal_fixture.rs +++ b/packages/process-daemon/tests/support/terminal_fixture.rs @@ -62,6 +62,7 @@ impl Fixture { .into(), cols: 80, rows: 24, + mode: Default::default(), } } diff --git a/packages/process-daemon/tests/windows-daemon.rs b/packages/process-daemon/tests/windows-daemon.rs index 86cf44856499..ac3a2e6e0d99 100644 --- a/packages/process-daemon/tests/windows-daemon.rs +++ b/packages/process-daemon/tests/windows-daemon.rs @@ -93,6 +93,7 @@ impl Daemon { env, cols, rows, + mode, } = request; match self.request(Request::Create { program, @@ -103,6 +104,7 @@ impl Daemon { env, cols, rows, + mode, }) { Response::Created { terminal } => terminal, response => panic!("create: {response:?}"), @@ -479,6 +481,7 @@ fn natural_exit_delivers_contiguous_output_and_retains_final_state() { Response::Exited { exit_code, final_offset, + .. } => { assert_eq!(final_offset, offset); break exit_code; @@ -504,7 +507,13 @@ fn natural_exit_delivers_contiguous_output_and_retains_final_state() { assert_eq!(exit_code, Some(actual_exit)); let (info, text) = daemon.snapshot(terminal.id); - assert_eq!(info.lifecycle, TerminalLifecycle::Exited { exit_code }); + assert_eq!( + info.lifecycle, + TerminalLifecycle::Exited { + exit_code, + signal: None + } + ); assert_eq!(info.output_tail, offset); assert!(text.contains("FINAL_DAEMON_MARKER"), "{text:?}"); let Response::Rows { @@ -557,7 +566,7 @@ fn natural_exit_delivers_contiguous_output_and_retains_final_state() { Response::Ok )); assert!( - matches!(daemon.request(Request::List), Response::Terminals { terminals } if terminals.is_empty()) + matches!(daemon.request(Request::List { mode: None }), Response::Terminals { terminals } if terminals.is_empty()) ); assert!(matches!(daemon.request(Request::Shutdown), Response::Ok)); drop(owner); @@ -648,7 +657,7 @@ fn observer_disconnect_and_control_input_preserve_independent_terminals() { assert!(!first_text.contains("SECOND_ONLY_MARKER")); assert!(!second_text.contains("FIRST_ONLY_MARKER")); assert!( - matches!(daemon.request(Request::List), Response::Terminals { terminals } if terminals.len() == 2) + matches!(daemon.request(Request::List { mode: None }), Response::Terminals { terminals } if terminals.len() == 2) ); assert!( matches!(daemon.request(Request::Ping), Response::Pong { instance_id, pid, protocol: 7, .. } @@ -662,7 +671,7 @@ fn observer_disconnect_and_control_input_preserve_independent_terminals() { )); drop(first_control); assert!( - matches!(daemon.request(Request::List), Response::Terminals { terminals } + matches!(daemon.request(Request::List { mode: None }), Response::Terminals { terminals } if terminals.len() == 1 && terminals[0].id == second.id) ); let input = b"after-first-exit"; From 10dd51ac6878ef5bc041a2aa3521eb707905934f Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Oct 2026 16:20:04 -0400 Subject: [PATCH 5/9] fix(process-daemon): reset pipe children's signals and harden pipe startup and draining --- packages/process-daemon/SPEC.md | 10 +- packages/process-daemon/src/client.rs | 9 +- packages/process-daemon/src/service.rs | 23 +- packages/process-daemon/src/service/pipe.rs | 276 ++++++++++++++------ packages/process-daemon/tests/ownership.rs | 60 +++++ packages/process-daemon/tests/pipe.rs | 20 +- 6 files changed, 299 insertions(+), 99 deletions(-) diff --git a/packages/process-daemon/SPEC.md b/packages/process-daemon/SPEC.md index 47b8fef39841..aceaede1b2f7 100644 --- a/packages/process-daemon/SPEC.md +++ b/packages/process-daemon/SPEC.md @@ -116,15 +116,17 @@ a PTY instead, so clients check `features` first. - `title`, `cols` and `rows` are optional for pipe processes; the title defaults to the program's file name and the size is reported as `0x0`. -- The child gets its own process group, stdin on `/dev/null`, and one pipe for +- 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. No `TERM` or `COLORTERM` is added, and nothing parses the output: there is no emulator, no terminal replies, no OSC title and no foreground process. - Output goes through the same raw replay and subscriptions as a PTY, with the same absolute offsets. A pipe has no controller or observer distinction for writing, since it accepts no input. -- `write`, `resize`, `control`, input frames, `snapshot`, `read_rows` and - `rename` fail for pipe processes. `replay`, `subscribe` and `terminate` +- `write`, `resize`, `control`, `input`, `snapshot`, `read_rows` and `rename` + fail for pipe processes. `replay`, `subscribe` and `terminate` work. - Exit is published once both the exit status and the end of output are known. A descendant that keeps the pipe open (`cmd &`) delays that by at @@ -136,7 +138,7 @@ a PTY instead, so clients check `features` first. - An exited pipe process stays listed, as a zombie, until `terminate`. The zombie keeps its PID and process group ID reserved, so `terminate` can kill any descendants left in the group without reaching an unrelated process. - `terminate` kills the whole group with SIGKILL, reaps the child, and never + `terminate` sends SIGKILL to the group and to the child, reaps it, and never waits on a descendant that left the group and still holds the pipe. `List` takes `mode` too. Without it, `list` returns PTYs only, so servers that diff --git a/packages/process-daemon/src/client.rs b/packages/process-daemon/src/client.rs index b2eebb59a730..084363e1dbdb 100644 --- a/packages/process-daemon/src/client.rs +++ b/packages/process-daemon/src/client.rs @@ -525,8 +525,13 @@ fn watch(directory: &Path, id: TerminalId) -> Result<()> { io::stdout().flush()?; } SubscriptionEvent::Response(response) => match *response { - Response::Exited { exit_code, .. } => { - eprintln!("\nterminal exited: {exit_code:?}"); + Response::Exited { + exit_code, signal, .. + } => { + match signal { + Some(signal) => eprintln!("\nterminal killed by {signal}"), + None => eprintln!("\nterminal exited: {exit_code:?}"), + } return Ok(()); } Response::Resized { cols, rows, .. } => { diff --git a/packages/process-daemon/src/service.rs b/packages/process-daemon/src/service.rs index 803b7c41e5ed..e3873779d1ff 100644 --- a/packages/process-daemon/src/service.rs +++ b/packages/process-daemon/src/service.rs @@ -320,8 +320,9 @@ impl TerminalService { cols: u16, rows: u16, ) -> Result<()> { + let terminal = self.pty(id)?; validate_size(cols, rows)?; - self.pty(id)?.request(|reply| ActorMessage::Resize { + terminal.request(|reply| ActorMessage::Resize { attachment_id, cols, rows, @@ -336,8 +337,9 @@ impl TerminalService { cols: u16, rows: u16, ) -> Result<()> { + let terminal = self.pty(id)?; validate_size(cols, rows)?; - self.pty(id)?.request(|reply| ActorMessage::Control { + terminal.request(|reply| ActorMessage::Control { attachment_id, cols, rows, @@ -354,11 +356,12 @@ impl TerminalService { rows: u16, bytes: Vec, ) -> Result<()> { + let terminal = self.pty(id)?; validate_size(cols, rows)?; if bytes.len() > MAX_INPUT_BYTES { bail!("input exceeds {MAX_INPUT_BYTES} byte limit"); } - self.pty(id)?.request(|reply| ActorMessage::Control { + terminal.request(|reply| ActorMessage::Control { attachment_id, cols, rows, @@ -546,9 +549,10 @@ fn start_actor( } }) .context("failed to spawn terminal actor")?; - match init_rx - .recv() - .context("terminal actor stopped during initialization")? + let initialized = init_rx.recv(); + match initialized + .map_err(|_| anyhow!("terminal actor stopped during initialization")) + .and_then(|result| result.map_err(|error| anyhow!(error))) { Ok(()) => Ok(ActorStart { actor_tx, @@ -560,7 +564,7 @@ fn start_actor( Err(error) => { drop(shutdown_tx); let _ = thread.join(); - bail!(error); + Err(error) } } } @@ -811,8 +815,9 @@ fn new_info( .chain(request.args.iter().cloned()) .collect(), cwd: request.cwd.clone(), - cols: request.cols, - rows: request.rows, + // Pipe processes have no terminal size. + cols: if mode.is_pty() { request.cols } else { 0 }, + rows: if mode.is_pty() { request.rows } else { 0 }, lifecycle: TerminalLifecycle::Running, output_head: 0, output_tail: 0, diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs index bfead87b138e..5bdf48f51478 100644 --- a/packages/process-daemon/src/service/pipe.rs +++ b/packages/process-daemon/src/service/pipe.rs @@ -14,11 +14,11 @@ //! - 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. -use std::io::{self, PipeReader, Read}; +use std::io::{self, PipeReader, PipeWriter, Read}; use std::os::fd::AsFd; use std::os::unix::process::CommandExt; use std::process::{Command, Stdio}; -use std::sync::{Arc, Mutex, PoisonError}; +use std::sync::{Arc, Mutex, PoisonError, RwLock}; use std::thread; use std::time::{Duration, Instant}; @@ -36,6 +36,44 @@ use super::{ /// 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, +} + +impl Pipes { + fn new() -> Result { + 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(super) fn spawn_pipe( id: TerminalId, @@ -43,10 +81,15 @@ impl TerminalHandle { request: CreateTerminal, replay_capacity: usize, ) -> Result { - 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 Pipes { + output, + output_writer, + stderr, + cancel, + cancel_writer, + exited, + exited_writer, + } = Pipes::new()?; let mut command = Command::new(&request.program); command .args(&request.args) @@ -56,9 +99,16 @@ impl TerminalHandle { .stdout(output_writer) .stderr(stderr) .process_group(0); - let child = command - .spawn() - .with_context(|| format!("failed to spawn {}", request.program))?; + // 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 { @@ -68,83 +118,76 @@ impl TerminalHandle { // Dropping `Child` neither waits nor kills; `Process` reaps it. drop(child); - match Self::start_pipe(id, order, &request, replay_capacity, &process, output) { - Ok(handle) => Ok(handle), - Err(error) => { - process.kill(); - process.reap(); - Err(error) - } - } - } - - fn start_pipe( - id: TerminalId, - order: u64, - request: &CreateTerminal, - replay_capacity: usize, - process: &Arc, - output: PipeReader, - ) -> Result { - let info = new_info(id, ProcessMode::Pipe, Some(process.pid), request); + let info = new_info(id, ProcessMode::Pipe, Some(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 = start_actor( + let actor = match start_actor( id, ProcessMode::Pipe, ActorMaster { pty: None }, writes, &info, replay_capacity, - (request.cols, request.rows), - )?; - let (cancel, cancel_writer) = io::pipe().context("failed to create cancel pipe")?; - let (exited, exited_writer) = io::pipe().context("failed to create exit pipe")?; + (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 reader = thread::Builder::new() + let started = thread::Builder::new() .name(format!("opencode-pty-reader-{id}")) - .spawn(move || read_output(output, cancel, exited, reader_actor_tx)); - let wait_actor_tx = actor.actor_tx.clone(); - let pid = process.pid; - let waiter = 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); + .spawn(move || read_output(output, 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") }); - let mut joins = vec![actor.thread]; - let started = (|| -> Result<()> { - joins.push(reader.context("failed to spawn terminal reader")?); - joins.push(waiter.context("failed to spawn terminal child waiter")?); - Ok(()) - })(); - if let Err(error) = started { - // Stop what did start: the actor through its shutdown channel, the - // reader through its cancel pipe. The caller kills and reaps. - drop(actor.shutdown_tx); - drop(cancel_writer); - for join in joins { - let _ = join.join(); + 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); } - return Err(error); } - let reap = Arc::clone(process); + 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)))), + 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()))), info, @@ -153,6 +196,32 @@ impl TerminalHandle { } } +/// 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 { + 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()); + } + for signal in [ + libc::SIGCHLD, + libc::SIGHUP, + libc::SIGINT, + libc::SIGQUIT, + libc::SIGTERM, + libc::SIGALRM, + ] { + libc::signal(signal, libc::SIG_DFL); + } + } + Ok(()) +} + /// A pipe child that stays a zombie after it exits, until `reap`. #[derive(Debug)] struct Process { @@ -166,8 +235,12 @@ impl Process { 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 this process group ID reserved. - unsafe { libc::killpg(pid, libc::SIGKILL) }; + // 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); + } } } @@ -242,13 +315,36 @@ fn wait_exit(pid: u32) -> io::Result { } fn signal_name(signal: i32) -> String { - nix::sys::signal::Signal::try_from(signal) - .map(|signal| signal.as_str().to_string()) - .unwrap_or_else(|_| format!("SIG{signal}")) + 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. +/// 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, cancel: PipeReader, @@ -258,19 +354,32 @@ fn read_output( let mut output = output; let mut buffer = [0_u8; 8192]; let mut deadline = None::; + let mut writers_closed = false; loop { let timeout = match deadline { - None => PollTimeout::NONE, - Some(deadline) => { + Some(deadline) if !writers_closed => { let left = deadline.saturating_duration_since(Instant::now()); if left.is_zero() { - let _ = actor.send(ActorMessage::ReaderEof); - return; + // 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 { + 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) } - // 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), @@ -289,7 +398,12 @@ fn read_output( } let ready = |fd: &PollFd| { fd.revents().is_none_or(|events| { - events.intersects(PollFlags::POLLIN | PollFlags::POLLHUP | PollFlags::POLLERR) + events.intersects( + PollFlags::POLLIN + | PollFlags::POLLHUP + | PollFlags::POLLERR + | PollFlags::POLLNVAL, + ) }) }; if ready(&fds[1]) { @@ -298,6 +412,12 @@ fn read_output( 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) => { diff --git a/packages/process-daemon/tests/ownership.rs b/packages/process-daemon/tests/ownership.rs index d14d6997564a..afadcfdfe1ed 100644 --- a/packages/process-daemon/tests/ownership.rs +++ b/packages/process-daemon/tests/ownership.rs @@ -666,3 +666,63 @@ fn pipe_processes_survive_handoff_and_stay_out_of_default_lists() { daemon.wait(); assert_terminal_stopped(&terminal); } + +#[test] +fn pipe_processes_do_not_inherit_the_daemons_blocked_signals() { + use opencode_pty::service::{ProcessMode, TerminalLifecycle}; + + let mut daemon = Daemon::start(); + let (_owner, response) = daemon.own(None); + assert!(matches!(response, Response::Owned)); + for (signal, name) in [(libc::SIGTERM, "SIGTERM"), (libc::SIGINT, "SIGINT")] { + let Response::Created { terminal: process } = daemon.request(Request::Create { + program: "/bin/sh".into(), + args: vec!["-c".into(), "exec sleep 1000".into()], + cwd: daemon.directory.clone(), + title: String::new(), + group_id: "ownership".into(), + env: Default::default(), + cols: 0, + rows: 0, + mode: ProcessMode::Pipe, + }) else { + panic!("create pipe"); + }; + // SAFETY: the PID belongs to this test's pipe process. + assert_eq!( + unsafe { libc::kill(process.pid.unwrap() as i32, signal) }, + 0 + ); + let deadline = Instant::now() + Duration::from_secs(5); + loop { + let Response::Terminals { terminals } = daemon.request(Request::List { + mode: Some(ProcessMode::Pipe), + }) else { + panic!("list failed"); + }; + let lifecycle = terminals + .into_iter() + .find(|value| value.id == process.id) + .unwrap() + .lifecycle; + if lifecycle != TerminalLifecycle::Running { + assert_eq!( + lifecycle, + TerminalLifecycle::Exited { + exit_code: None, + signal: Some(name.into()), + } + ); + break; + } + assert!(Instant::now() < deadline, "pipe process ignored {name}"); + thread::sleep(Duration::from_millis(10)); + } + assert!(matches!( + daemon.request(Request::Terminate { id: process.id }), + Response::Ok + )); + } + assert!(matches!(daemon.request(Request::Shutdown), Response::Ok)); + daemon.wait(); +} diff --git a/packages/process-daemon/tests/pipe.rs b/packages/process-daemon/tests/pipe.rs index 1484af004eba..78fc1e986a43 100644 --- a/packages/process-daemon/tests/pipe.rs +++ b/packages/process-daemon/tests/pipe.rs @@ -167,12 +167,22 @@ fn a_descendant_holding_the_pipe_delays_the_exit_by_at_most_the_drain() { wait_dead(sleeper); } +/// Kills a test descendant even when an assertion fails first. +struct KillOnDrop(i32); + +impl Drop for KillOnDrop { + fn drop(&mut self) { + // SAFETY: the PID belongs to this test's own descendant. + unsafe { libc::kill(self.0, libc::SIGKILL) }; + } +} + #[test] fn terminate_does_not_wait_for_a_descendant_that_left_the_group() { let service = TerminalService::default(); let info = service .create(pipe( - "perl -e 'use POSIX; POSIX::setsid(); print \"$$\\n\"; $| = 1; sleep 30' & wait", + "perl -e 'use POSIX; POSIX::setsid(); $| = 1; print \"$$\\n\"; sleep 30' & wait", )) .unwrap(); let attachment = service @@ -188,15 +198,13 @@ fn terminate_does_not_wait_for_a_descendant_that_left_the_group() { output.extend(bytes); } } - let escaped: i32 = String::from_utf8(output).unwrap().trim().parse().unwrap(); + let escaped = KillOnDrop(String::from_utf8(output).unwrap().trim().parse().unwrap()); drop(attachment); let started = Instant::now(); service.terminate(info.id).unwrap(); assert!(started.elapsed() < Duration::from_secs(2)); - // It left the group, so it survives; clean it up here. - assert!(alive(escaped)); - // SAFETY: the PID belongs to this test's own descendant. - unsafe { libc::kill(escaped, libc::SIGKILL) }; + // It left the group, so it survives until `escaped` drops. + assert!(alive(escaped.0)); } #[test] From d477d4cf6b8fd838afe833eeb5a159ffc704840d Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Oct 2026 16:24:52 -0400 Subject: [PATCH 6/9] fix(process-daemon): reset signal actions before unmasking in pipe children --- packages/process-daemon/src/service/pipe.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs index 5bdf48f51478..4af60ccaf3f4 100644 --- a/packages/process-daemon/src/service/pipe.rs +++ b/packages/process-daemon/src/service/pipe.rs @@ -202,12 +202,7 @@ impl TerminalHandle { fn reset_signals() -> io::Result<()> { // SAFETY: sigemptyset, sigprocmask and signal are async-signal-safe. unsafe { - 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()); - } + // Dispositions first, so no inherited handler can run once unmasked. for signal in [ libc::SIGCHLD, libc::SIGHUP, @@ -218,6 +213,12 @@ fn reset_signals() -> io::Result<()> { ] { 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(()) } From ff51b3e1ff7996fc1bac5cffc2e9558668ccc200 Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Oct 2026 16:27:04 -0400 Subject: [PATCH 7/9] feat(process-daemon): write pipe output to a file --- packages/process-daemon/SPEC.md | 12 +- packages/process-daemon/src/client.rs | 3 + packages/process-daemon/src/daemon/server.rs | 2 + packages/process-daemon/src/protocol.rs | 16 ++- packages/process-daemon/src/service.rs | 11 ++ packages/process-daemon/src/service/pipe.rs | 31 ++++- packages/process-daemon/tests/ownership.rs | 15 ++- packages/process-daemon/tests/pipe.rs | 113 ++++++++++++++++++ packages/process-daemon/tests/service.rs | 1 + .../tests/support/resize_fixture.rs | 1 + .../tests/support/terminal_fixture.rs | 1 + .../process-daemon/tests/windows-daemon.rs | 2 + 12 files changed, 202 insertions(+), 6 deletions(-) diff --git a/packages/process-daemon/SPEC.md b/packages/process-daemon/SPEC.md index aceaede1b2f7..39cc551c486b 100644 --- a/packages/process-daemon/SPEC.md +++ b/packages/process-daemon/SPEC.md @@ -141,6 +141,16 @@ a PTY instead, so clients check `features` first. `terminate` sends SIGKILL to the group and to the child, reaps it, and never waits on a descendant that left the group and still holds the pipe. +`Create` also takes `output_path` for pipe processes (feature `output_path`): +an absolute path the daemon opens before spawning (created with mode `0600` +if missing, otherwise appended to) and writes every output byte to, before +the byte reaches the replay buffer or any subscriber. The file therefore keeps +all output, including what falls out of the 2 MiB replay window or arrives +while no server is attached during a handoff. A path that cannot be opened +fails `create` without starting the process; a later write error stops the +file and is logged, and the process keeps running. `output_path` is rejected +for PTYs. + `List` takes `mode` too. Without it, `list` returns PTYs only, so servers that predate pipe processes never see them. Clients leave `mode` out of the request when it is unset: older daemons reject a `list` with any extra field. @@ -169,7 +179,7 @@ subscriptions, title updates, rename, controller takeover, terminate, and destru shutdown, ownership acquisition, and restart handoff preparation. `Pong` and the registration carry `features`, the daemon's additive -capabilities (currently `["rename", "pipe"]` on Unix and `["rename"]` on Windows). Additive changes, such as a new request +capabilities (currently `["rename", "pipe", "output_path"]` on Unix and `["rename"]` on Windows). Additive changes, such as a new request or field older clients can ignore, extend `features` and keep the protocol version; clients check `features` before sending a new request, since a daemon that cannot parse one drops the connection. Only breaking changes bump diff --git a/packages/process-daemon/src/client.rs b/packages/process-daemon/src/client.rs index 084363e1dbdb..5db77578ed5b 100644 --- a/packages/process-daemon/src/client.rs +++ b/packages/process-daemon/src/client.rs @@ -166,6 +166,7 @@ impl TerminalClient { cols: request.cols, rows: request.rows, mode: request.mode, + output_path: request.output_path, })? { Response::Created { terminal } => Ok(terminal), response => unexpected(response), @@ -595,6 +596,7 @@ fn play(directory: &Path) -> Result<()> { cols: 100, rows: 30, mode: ProcessMode::Pty, + output_path: None, } }; let info = client.create(request)?; @@ -673,6 +675,7 @@ fn play(directory: &Path) -> Result<()> { cols: 100, rows: 30, mode: ProcessMode::Pty, + output_path: None, })?; active = Some(info.id); thread::sleep(Duration::from_millis(200)); diff --git a/packages/process-daemon/src/daemon/server.rs b/packages/process-daemon/src/daemon/server.rs index f1686ab04b48..f92bb90af7c8 100644 --- a/packages/process-daemon/src/daemon/server.rs +++ b/packages/process-daemon/src/daemon/server.rs @@ -559,6 +559,7 @@ fn dispatch( cols, rows, mode, + output_path, } => Response::Created { terminal: service.create(CreateTerminal { program, @@ -570,6 +571,7 @@ fn dispatch( cols, rows, mode, + output_path, })?, }, Request::List { mode } => Response::Terminals { diff --git a/packages/process-daemon/src/protocol.rs b/packages/process-daemon/src/protocol.rs index 64e16990a8ca..ed17a425f05f 100644 --- a/packages/process-daemon/src/protocol.rs +++ b/packages/process-daemon/src/protocol.rs @@ -10,7 +10,7 @@ pub const PROTOCOL_VERSION: u32 = 7; /// Additive capabilities of this daemon. Breaking changes bump `PROTOCOL_VERSION`. /// `pipe` (non-PTY processes) is Unix-only for now. #[cfg(unix)] -pub const FEATURES: &[&str] = &["rename", "pipe"]; +pub const FEATURES: &[&str] = &["rename", "pipe", "output_path"]; #[cfg(not(unix))] pub const FEATURES: &[&str] = &["rename"]; @@ -59,6 +59,10 @@ pub enum Request { rows: u16, #[serde(default, skip_serializing_if = "ProcessMode::is_pty")] mode: ProcessMode, + /// Pipe only: an absolute path the daemon appends every output byte + /// to. Send it only to a daemon that lists the `output_path` feature. + #[serde(default, skip_serializing_if = "Option::is_none")] + output_path: Option, }, /// Without `mode`, lists PTYs only, so older servers never see pipe processes. /// `mode` is left out of the JSON when unset: older daemons reject a `list` @@ -407,6 +411,16 @@ mod tests { .. } )); + assert!(matches!( + serde_json::from_str::( + r#"{"op":"create","program":"sh","args":[],"cwd":"/","group_id":"g","env":{},"mode":"pipe","output_path":"/tmp/out"}"# + ) + .unwrap(), + Request::Create { + output_path: Some(path), + .. + } if path == std::path::Path::new("/tmp/out") + )); // Exits without a signal look exactly as before. assert_eq!( serde_json::to_value(Response::Exited { diff --git a/packages/process-daemon/src/service.rs b/packages/process-daemon/src/service.rs index e3873779d1ff..3abc23809233 100644 --- a/packages/process-daemon/src/service.rs +++ b/packages/process-daemon/src/service.rs @@ -179,6 +179,8 @@ pub struct CreateTerminal { pub cols: u16, pub rows: u16, pub mode: ProcessMode, + /// Pipe only: an absolute path to append every output byte to. + pub output_path: Option, } impl CreateTerminal { @@ -199,6 +201,7 @@ impl CreateTerminal { cols: DEFAULT_COLS, rows: DEFAULT_ROWS, mode: ProcessMode::Pty, + output_path: None, }) } } @@ -225,6 +228,14 @@ impl TerminalService { } pub fn create(&self, request: CreateTerminal) -> Result { + if let Some(path) = &request.output_path { + if request.mode.is_pty() { + bail!("output_path is only supported for pipe processes"); + } + if !path.is_absolute() { + bail!("output_path must be absolute"); + } + } match request.mode { ProcessMode::Pty => validate_size(request.cols, request.rows)?, #[cfg(not(unix))] diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs index 4af60ccaf3f4..22ed8dd9b53b 100644 --- a/packages/process-daemon/src/service/pipe.rs +++ b/packages/process-daemon/src/service/pipe.rs @@ -11,11 +11,16 @@ //! - 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. +//! - With `output_path`, the reader appends every byte to that file before +//! handing it to the actor, so output survives the 2 MiB replay window, +//! arrives before any subscriber, and stays complete across a handoff. //! - 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. -use std::io::{self, PipeReader, PipeWriter, Read}; +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}; @@ -90,6 +95,19 @@ impl TerminalHandle { exited, exited_writer, } = Pipes::new()?; + // Before spawning, so a bad path fails the request with nothing to clean up. + let sink = request + .output_path + .as_deref() + .map(|path| { + OpenOptions::new() + .create(true) + .append(true) + .mode(0o600) + .open(path) + .with_context(|| format!("failed to open output file {}", path.display())) + }) + .transpose()?; let mut command = Command::new(&request.program); command .args(&request.args) @@ -143,7 +161,7 @@ impl TerminalHandle { let reader_actor_tx = actor.actor_tx.clone(); let started = thread::Builder::new() .name(format!("opencode-pty-reader-{id}")) - .spawn(move || read_output(output, cancel, exited, reader_actor_tx)) + .spawn(move || read_output(output, sink, cancel, exited, reader_actor_tx)) .context("failed to spawn terminal reader") .and_then(|reader| { joins.push(reader); @@ -348,6 +366,7 @@ fn signal_name(signal: i32) -> String { /// deadline. fn read_output( output: PipeReader, + mut sink: Option, cancel: PipeReader, exited: PipeReader, actor: Sender, @@ -426,6 +445,14 @@ fn read_output( return; } Ok(length) => { + // The file first, so it always holds at least what + // subscribers have seen, even if the actor is backpressured. + if let Some(file) = &mut sink + && let Err(error) = file.write_all(&buffer[..length]) + { + eprintln!("opencode-pty stopped writing output file: {error}"); + sink = None; + } if actor .send(ActorMessage::Output(buffer[..length].to_vec())) .is_err() diff --git a/packages/process-daemon/tests/ownership.rs b/packages/process-daemon/tests/ownership.rs index afadcfdfe1ed..8ef4b71e6217 100644 --- a/packages/process-daemon/tests/ownership.rs +++ b/packages/process-daemon/tests/ownership.rs @@ -111,6 +111,7 @@ impl Daemon { cols: 80, rows: 24, mode: Default::default(), + output_path: None, }) { Response::Created { terminal } => terminal, response => panic!("create: {response:?}"), @@ -315,10 +316,13 @@ fn rename_is_advertised_and_served() { use opencode_pty::protocol::{SubscriptionEvent, read_subscription_event}; let mut daemon = Daemon::start(); - assert_eq!(daemon.registration.features, ["rename", "pipe"]); + assert_eq!( + daemon.registration.features, + ["rename", "pipe", "output_path"] + ); assert!(matches!( daemon.request(Request::Ping), - Response::Pong { features, .. } if features == ["rename", "pipe"] + Response::Pong { features, .. } if features == ["rename", "pipe", "output_path"] )); let (owner, response) = daemon.own(None); assert!(matches!(response, Response::Owned)); @@ -588,6 +592,7 @@ fn pipe_processes_survive_handoff_and_stay_out_of_default_lists() { let (owner, response) = daemon.own(None); assert!(matches!(response, Response::Owned)); let terminal = daemon.terminal("exec cat"); + let output_file = daemon.root.join("pipe.out"); let Response::Created { terminal: process } = daemon.request(Request::Create { program: "/bin/sh".into(), args: vec![ @@ -601,6 +606,7 @@ fn pipe_processes_survive_handoff_and_stay_out_of_default_lists() { cols: 0, rows: 0, mode: ProcessMode::Pipe, + output_path: Some(output_file.clone()), }) else { panic!("create pipe"); }; @@ -657,6 +663,10 @@ fn pipe_processes_survive_handoff_and_stay_out_of_default_lists() { }; assert_eq!(exit, Some(4)); assert_eq!(String::from_utf8(output).unwrap(), "before\nafter\n"); + assert_eq!( + std::fs::read_to_string(&output_file).unwrap(), + "before\nafter\n" + ); assert!(matches!( daemon.request(Request::Terminate { id: process.id }), Response::Ok @@ -685,6 +695,7 @@ fn pipe_processes_do_not_inherit_the_daemons_blocked_signals() { cols: 0, rows: 0, mode: ProcessMode::Pipe, + output_path: None, }) else { panic!("create pipe"); }; diff --git a/packages/process-daemon/tests/pipe.rs b/packages/process-daemon/tests/pipe.rs index 78fc1e986a43..c41b8ef6a862 100644 --- a/packages/process-daemon/tests/pipe.rs +++ b/packages/process-daemon/tests/pipe.rs @@ -19,6 +19,7 @@ fn pipe(script: &str) -> CreateTerminal { cols: 0, rows: 0, mode: ProcessMode::Pipe, + output_path: None, } } @@ -300,3 +301,115 @@ fn pipe_processes_need_no_terminal_size() { let info = service.create(pipe("true")).unwrap(); service.terminate(info.id).unwrap(); } + +fn temp_path(name: &str) -> std::path::PathBuf { + std::env::temp_dir().join(format!( + "opencode-pty-pipe-{name}-{:032x}", + rand::random::() + )) +} + +fn wait_exited(service: &TerminalService, id: TerminalId) -> TerminalLifecycle { + let deadline = Instant::now() + Duration::from_secs(10); + loop { + let lifecycle = service + .list_mode(ProcessMode::Pipe) + .unwrap() + .into_iter() + .find(|info| info.id == id) + .unwrap() + .lifecycle; + if lifecycle != TerminalLifecycle::Running { + return lifecycle; + } + assert!(Instant::now() < deadline, "pipe process did not exit"); + std::thread::sleep(Duration::from_millis(10)); + } +} + +#[test] +fn output_path_keeps_everything_beyond_the_replay_window_without_a_subscriber() { + use std::os::unix::fs::PermissionsExt; + + // A replay window far smaller than the output. + let service = TerminalService::new(1024); + let path = temp_path("full"); + let info = service + .create(CreateTerminal { + output_path: Some(path.clone()), + ..pipe("seq 1 20000; echo done >&2") + }) + .unwrap(); + assert_eq!( + wait_exited(&service, info.id), + TerminalLifecycle::Exited { + exit_code: Some(0), + signal: None + } + ); + let expected = (1..=20000).map(|n| format!("{n}\n")).collect::() + "done\n"; + assert_eq!(std::fs::read_to_string(&path).unwrap(), expected); + assert_eq!( + std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, + 0o600 + ); + let replay = service.replay(info.id, 0).unwrap(); + assert!(replay.truncated); + assert_eq!(replay.end_offset, expected.len() as u64); + service.terminate(info.id).unwrap(); + std::fs::remove_file(path).unwrap(); +} + +#[test] +fn output_path_appends_to_an_existing_file() { + let service = TerminalService::default(); + let path = temp_path("append"); + std::fs::write(&path, "earlier\n").unwrap(); + let info = service + .create(CreateTerminal { + output_path: Some(path.clone()), + ..pipe("echo later") + }) + .unwrap(); + wait_exited(&service, info.id); + assert_eq!(std::fs::read_to_string(&path).unwrap(), "earlier\nlater\n"); + service.terminate(info.id).unwrap(); + std::fs::remove_file(path).unwrap(); +} + +#[test] +fn output_path_must_be_an_absolute_file_for_a_pipe_process() { + let service = TerminalService::default(); + let missing = temp_path("missing-directory").join("out"); + for (request, error) in [ + ( + CreateTerminal { + output_path: Some("relative.out".into()), + ..pipe("true") + }, + "must be absolute", + ), + ( + CreateTerminal { + output_path: Some(temp_path("pty")), + cols: 80, + rows: 24, + mode: ProcessMode::Pty, + ..pipe("true") + }, + "only supported for pipe processes", + ), + ( + CreateTerminal { + output_path: Some(missing), + ..pipe("touch should-not-run") + }, + "failed to open output file", + ), + ] { + let message = format!("{:#}", service.create(request).unwrap_err()); + assert!(message.contains(error), "{message}"); + } + assert!(service.list_mode(ProcessMode::Pipe).unwrap().is_empty()); + assert!(!std::path::Path::new("should-not-run").exists()); +} diff --git a/packages/process-daemon/tests/service.rs b/packages/process-daemon/tests/service.rs index 2988f3952b44..530aa844ae76 100644 --- a/packages/process-daemon/tests/service.rs +++ b/packages/process-daemon/tests/service.rs @@ -70,6 +70,7 @@ fn command(script: &str) -> CreateTerminal { cols: 80, rows: 24, mode: Default::default(), + output_path: None, } } diff --git a/packages/process-daemon/tests/support/resize_fixture.rs b/packages/process-daemon/tests/support/resize_fixture.rs index 70f4577d06e1..efc8dcda4841 100644 --- a/packages/process-daemon/tests/support/resize_fixture.rs +++ b/packages/process-daemon/tests/support/resize_fixture.rs @@ -44,6 +44,7 @@ impl ResizeFixture { cols: 80, rows: 24, mode: Default::default(), + output_path: None, }) .unwrap(); diff --git a/packages/process-daemon/tests/support/terminal_fixture.rs b/packages/process-daemon/tests/support/terminal_fixture.rs index 47b2f294a5e7..173ca08be9c9 100644 --- a/packages/process-daemon/tests/support/terminal_fixture.rs +++ b/packages/process-daemon/tests/support/terminal_fixture.rs @@ -63,6 +63,7 @@ impl Fixture { cols: 80, rows: 24, mode: Default::default(), + output_path: None, } } diff --git a/packages/process-daemon/tests/windows-daemon.rs b/packages/process-daemon/tests/windows-daemon.rs index ac3a2e6e0d99..00efd2db3fd1 100644 --- a/packages/process-daemon/tests/windows-daemon.rs +++ b/packages/process-daemon/tests/windows-daemon.rs @@ -94,6 +94,7 @@ impl Daemon { cols, rows, mode, + output_path, } = request; match self.request(Request::Create { program, @@ -105,6 +106,7 @@ impl Daemon { cols, rows, mode, + output_path, }) { Response::Created { terminal } => terminal, response => panic!("create: {response:?}"), From 77d16d5f97bd8f6ab31edb7a23080dadb1145684 Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Oct 2026 16:32:17 -0400 Subject: [PATCH 8/9] fix(process-daemon): report output file write errors and refuse symlinks --- packages/process-daemon/SPEC.md | 4 +- packages/process-daemon/src/service.rs | 15 ++++ packages/process-daemon/src/service/pipe.rs | 17 ++++- packages/process-daemon/tests/pipe.rs | 85 ++++++++++++++++----- 4 files changed, 102 insertions(+), 19 deletions(-) diff --git a/packages/process-daemon/SPEC.md b/packages/process-daemon/SPEC.md index 39cc551c486b..c10755168385 100644 --- a/packages/process-daemon/SPEC.md +++ b/packages/process-daemon/SPEC.md @@ -148,7 +148,9 @@ the byte reaches the replay buffer or any subscriber. The file therefore keeps all output, including what falls out of the 2 MiB replay window or arrives while no server is attached during a handoff. A path that cannot be opened fails `create` without starting the process; a later write error stops the -file and is logged, and the process keeps running. `output_path` is rejected +file, sets `TerminalInfo.output_error` (omitted otherwise), and the process +keeps running. The file is closed before the exit is published, and a symlink +as the final path component is refused. `output_path` is rejected for PTYs. `List` takes `mode` too. Without it, `list` returns PTYs only, so servers that diff --git a/packages/process-daemon/src/service.rs b/packages/process-daemon/src/service.rs index 3abc23809233..e4468e18a96b 100644 --- a/packages/process-daemon/src/service.rs +++ b/packages/process-daemon/src/service.rs @@ -87,6 +87,10 @@ pub struct TerminalInfo { pub lifecycle: TerminalLifecycle, pub output_head: u64, pub output_tail: u64, + /// Set when writing a pipe process's `output_path` failed; the file then + /// lacks everything from that point on. Omitted when there is no error. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub output_error: Option, } #[derive(Clone, Debug)] @@ -832,6 +836,7 @@ fn new_info( lifecycle: TerminalLifecycle::Running, output_head: 0, output_tail: 0, + output_error: None, })) } @@ -859,6 +864,9 @@ enum ActorMessage { /// 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 { reply: Reply>, @@ -1352,6 +1360,12 @@ fn run_actor(config: ActorConfig) -> Result<()> { } } #[cfg(unix)] + Ok(ActorMessage::OutputFileFailed(message)) => { + if let Ok(mut value) = info.write() { + value.output_error = Some(message); + } + } + #[cfg(unix)] Ok(ActorMessage::ChildKilled(signal)) => { child_exit = Some(Ok((None, Some(signal)))); } @@ -1945,6 +1959,7 @@ mod tests { lifecycle: TerminalLifecycle::Running, output_head: 0, output_tail: 0, + output_error: None, } } diff --git a/packages/process-daemon/src/service/pipe.rs b/packages/process-daemon/src/service/pipe.rs index 22ed8dd9b53b..1f4bd299f949 100644 --- a/packages/process-daemon/src/service/pipe.rs +++ b/packages/process-daemon/src/service/pipe.rs @@ -104,6 +104,8 @@ impl TerminalHandle { .create(true) .append(true) .mode(0o600) + // Never write through a symlink planted at the final component. + .custom_flags(libc::O_NOFOLLOW) .open(path) .with_context(|| format!("failed to open output file {}", path.display())) }) @@ -388,6 +390,8 @@ fn read_output( .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; } @@ -441,6 +445,8 @@ fn read_output( 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; } @@ -450,8 +456,17 @@ fn read_output( if let Some(file) = &mut sink && let Err(error) = file.write_all(&buffer[..length]) { - eprintln!("opencode-pty stopped writing output file: {error}"); + // 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(buffer[..length].to_vec())) diff --git a/packages/process-daemon/tests/pipe.rs b/packages/process-daemon/tests/pipe.rs index c41b8ef6a862..2064c53129b7 100644 --- a/packages/process-daemon/tests/pipe.rs +++ b/packages/process-daemon/tests/pipe.rs @@ -302,11 +302,22 @@ fn pipe_processes_need_no_terminal_size() { service.terminate(info.id).unwrap(); } -fn temp_path(name: &str) -> std::path::PathBuf { - std::env::temp_dir().join(format!( - "opencode-pty-pipe-{name}-{:032x}", - rand::random::() - )) +/// A unique temporary path, removed when dropped even if a test fails. +struct TempPath(std::path::PathBuf); + +impl TempPath { + fn new(name: &str) -> Self { + Self(std::env::temp_dir().join(format!( + "opencode-pty-pipe-{name}-{:032x}", + rand::random::() + ))) + } +} + +impl Drop for TempPath { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } } fn wait_exited(service: &TerminalService, id: TerminalId) -> TerminalLifecycle { @@ -333,7 +344,8 @@ fn output_path_keeps_everything_beyond_the_replay_window_without_a_subscriber() // A replay window far smaller than the output. let service = TerminalService::new(1024); - let path = temp_path("full"); + let file = TempPath::new("full"); + let path = &file.0; let info = service .create(CreateTerminal { output_path: Some(path.clone()), @@ -348,23 +360,27 @@ fn output_path_keeps_everything_beyond_the_replay_window_without_a_subscriber() } ); let expected = (1..=20000).map(|n| format!("{n}\n")).collect::() + "done\n"; - assert_eq!(std::fs::read_to_string(&path).unwrap(), expected); + assert_eq!(std::fs::read_to_string(path).unwrap(), expected); assert_eq!( - std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, + std::fs::metadata(path).unwrap().permissions().mode() & 0o777, 0o600 ); let replay = service.replay(info.id, 0).unwrap(); assert!(replay.truncated); assert_eq!(replay.end_offset, expected.len() as u64); + assert_eq!( + service.list_mode(ProcessMode::Pipe).unwrap()[0].output_error, + None + ); service.terminate(info.id).unwrap(); - std::fs::remove_file(path).unwrap(); } #[test] fn output_path_appends_to_an_existing_file() { let service = TerminalService::default(); - let path = temp_path("append"); - std::fs::write(&path, "earlier\n").unwrap(); + let file = TempPath::new("append"); + let path = &file.0; + std::fs::write(path, "earlier\n").unwrap(); let info = service .create(CreateTerminal { output_path: Some(path.clone()), @@ -372,15 +388,20 @@ fn output_path_appends_to_an_existing_file() { }) .unwrap(); wait_exited(&service, info.id); - assert_eq!(std::fs::read_to_string(&path).unwrap(), "earlier\nlater\n"); + assert_eq!(std::fs::read_to_string(path).unwrap(), "earlier\nlater\n"); service.terminate(info.id).unwrap(); - std::fs::remove_file(path).unwrap(); } #[test] fn output_path_must_be_an_absolute_file_for_a_pipe_process() { let service = TerminalService::default(); - let missing = temp_path("missing-directory").join("out"); + let missing = TempPath::new("missing-directory").0.join("out"); + let marker = TempPath::new("should-not-run"); + let target = TempPath::new("target"); + std::fs::write(&target.0, "").unwrap(); + let link = TempPath::new("link"); + std::os::unix::fs::symlink(&target.0, &link.0).unwrap(); + let touch = format!("touch '{}'", marker.0.display()); for (request, error) in [ ( CreateTerminal { @@ -391,7 +412,7 @@ fn output_path_must_be_an_absolute_file_for_a_pipe_process() { ), ( CreateTerminal { - output_path: Some(temp_path("pty")), + output_path: Some(TempPath::new("pty").0.clone()), cols: 80, rows: 24, mode: ProcessMode::Pty, @@ -402,7 +423,14 @@ fn output_path_must_be_an_absolute_file_for_a_pipe_process() { ( CreateTerminal { output_path: Some(missing), - ..pipe("touch should-not-run") + ..pipe(&touch) + }, + "failed to open output file", + ), + ( + CreateTerminal { + output_path: Some(link.0.clone()), + ..pipe(&touch) }, "failed to open output file", ), @@ -411,5 +439,28 @@ fn output_path_must_be_an_absolute_file_for_a_pipe_process() { assert!(message.contains(error), "{message}"); } assert!(service.list_mode(ProcessMode::Pipe).unwrap().is_empty()); - assert!(!std::path::Path::new("should-not-run").exists()); + // Give a wrongly spawned child time to run before checking. + std::thread::sleep(Duration::from_millis(100)); + assert!(!marker.0.exists()); +} + +#[cfg(target_os = "linux")] +#[test] +fn a_failing_output_file_is_reported_and_the_process_keeps_running() { + let service = TerminalService::default(); + let info = service + .create(CreateTerminal { + output_path: Some("/dev/full".into()), + ..pipe("echo lost; echo kept; exit 5") + }) + .unwrap(); + let finished = run(&service, info.id); + assert_eq!(finished.output, "lost\nkept\n"); + assert_eq!(finished.exit_code, Some(5)); + let error = service.list_mode(ProcessMode::Pipe).unwrap()[0] + .output_error + .clone() + .unwrap(); + assert!(error.contains("failed to write output file"), "{error}"); + service.terminate(info.id).unwrap(); } From 3513f8146f18a6d5373f576f0408cbc9e77e1326 Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Oct 2026 16:35:10 -0400 Subject: [PATCH 9/9] fix(process-daemon): report output file errors on the exit event --- packages/process-daemon/src/daemon/server.rs | 2 ++ packages/process-daemon/src/protocol.rs | 5 +++++ packages/process-daemon/src/service.rs | 7 ++++++- packages/process-daemon/src/transport/unix.rs | 2 ++ packages/process-daemon/src/transport/windows.rs | 1 + packages/process-daemon/tests/pipe.rs | 8 +++++++- 6 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/process-daemon/src/daemon/server.rs b/packages/process-daemon/src/daemon/server.rs index f92bb90af7c8..5bb13d37171f 100644 --- a/packages/process-daemon/src/daemon/server.rs +++ b/packages/process-daemon/src/daemon/server.rs @@ -505,10 +505,12 @@ fn stream_subscription( exit_code, signal, final_offset, + output_error, } => Response::Exited { exit_code, signal, final_offset, + output_error, }, StreamEvent::ControllerChanged { attachment_id, diff --git a/packages/process-daemon/src/protocol.rs b/packages/process-daemon/src/protocol.rs index ed17a425f05f..82f5bebc78a5 100644 --- a/packages/process-daemon/src/protocol.rs +++ b/packages/process-daemon/src/protocol.rs @@ -187,6 +187,10 @@ pub enum Response { #[serde(default, skip_serializing_if = "Option::is_none")] signal: Option, final_offset: u64, + /// Same as `TerminalInfo.output_error`, so a live subscriber learns the + /// output file is incomplete without another request. + #[serde(default, skip_serializing_if = "Option::is_none")] + output_error: Option, }, ControllerChanged { attachment_id: Option, @@ -427,6 +431,7 @@ mod tests { exit_code: Some(1), signal: None, final_offset: 2, + output_error: None, }) .unwrap(), serde_json::json!({"type": "exited", "exit_code": 1, "final_offset": 2}) diff --git a/packages/process-daemon/src/service.rs b/packages/process-daemon/src/service.rs index e4468e18a96b..dd04d9305a0e 100644 --- a/packages/process-daemon/src/service.rs +++ b/packages/process-daemon/src/service.rs @@ -136,6 +136,7 @@ pub enum StreamEvent { exit_code: Option, signal: Option, final_offset: u64, + output_error: Option, }, ControllerChanged { attachment_id: Option, @@ -1399,16 +1400,19 @@ fn run_actor(config: ActorConfig) -> Result<()> { && let Some(result) = child_exit.take() { let (exit_code, signal) = result.clone().unwrap_or_default(); + let mut output_error = None; if let Ok(mut value) = info.write() { value.lifecycle = match result { Ok((exit_code, signal)) => TerminalLifecycle::Exited { exit_code, signal }, Err(message) => TerminalLifecycle::Failed { message }, }; + output_error = value.output_error.clone(); } let event = StreamEvent::Exited { exit_code, signal, final_offset: replay.tail, + output_error, }; broadcast( &shutdown, @@ -2144,7 +2148,8 @@ mod tests { [StreamEvent::Exited { exit_code: Some(23), signal: None, - final_offset: 5 + final_offset: 5, + output_error: None, }] )); } diff --git a/packages/process-daemon/src/transport/unix.rs b/packages/process-daemon/src/transport/unix.rs index 5be4764267a6..d4ee215bda2d 100644 --- a/packages/process-daemon/src/transport/unix.rs +++ b/packages/process-daemon/src/transport/unix.rs @@ -393,6 +393,7 @@ mod tests { exit_code: Some(0), signal: None, final_offset: 123, + output_error: None, }, ) .unwrap(); @@ -404,6 +405,7 @@ mod tests { exit_code: Some(0), signal: None, final_offset: 123, + output_error: None, } )); assert_eq!(client.read(&mut [0]).unwrap(), 0); diff --git a/packages/process-daemon/src/transport/windows.rs b/packages/process-daemon/src/transport/windows.rs index cc9d04405d23..9fc7eea8c9ba 100644 --- a/packages/process-daemon/src/transport/windows.rs +++ b/packages/process-daemon/src/transport/windows.rs @@ -705,6 +705,7 @@ mod tests { exit_code: Some(0), signal: None, final_offset: 123, + output_error: None, }, ) .unwrap(); diff --git a/packages/process-daemon/tests/pipe.rs b/packages/process-daemon/tests/pipe.rs index 2064c53129b7..92b8b712502d 100644 --- a/packages/process-daemon/tests/pipe.rs +++ b/packages/process-daemon/tests/pipe.rs @@ -25,6 +25,7 @@ fn pipe(script: &str) -> CreateTerminal { struct Finished { output: String, + output_error: Option, exit_code: Option, signal: Option, final_offset: u64, @@ -49,9 +50,11 @@ fn run(service: &TerminalService, id: TerminalId) -> Finished { exit_code, signal, final_offset, + output_error, } => { return Finished { output: String::from_utf8(output).unwrap(), + output_error, exit_code, signal, final_offset, @@ -96,6 +99,7 @@ fn reports_ordered_output_and_exit_code_without_a_terminal() { ); assert_eq!(finished.exit_code, Some(7)); assert_eq!(finished.signal, None); + assert_eq!(finished.output_error, None); assert_eq!(finished.final_offset, finished.output.len() as u64); let listed = service.list_mode(ProcessMode::Pipe).unwrap(); assert_eq!( @@ -396,6 +400,7 @@ fn output_path_appends_to_an_existing_file() { fn output_path_must_be_an_absolute_file_for_a_pipe_process() { let service = TerminalService::default(); let missing = TempPath::new("missing-directory").0.join("out"); + let pty = TempPath::new("pty"); let marker = TempPath::new("should-not-run"); let target = TempPath::new("target"); std::fs::write(&target.0, "").unwrap(); @@ -412,7 +417,7 @@ fn output_path_must_be_an_absolute_file_for_a_pipe_process() { ), ( CreateTerminal { - output_path: Some(TempPath::new("pty").0.clone()), + output_path: Some(pty.0.clone()), cols: 80, rows: 24, mode: ProcessMode::Pty, @@ -457,6 +462,7 @@ fn a_failing_output_file_is_reported_and_the_process_keeps_running() { let finished = run(&service, info.id); assert_eq!(finished.output, "lost\nkept\n"); assert_eq!(finished.exit_code, Some(5)); + assert!(finished.output_error.is_some()); let error = service.list_mode(ProcessMode::Pipe).unwrap()[0] .output_error .clone()