Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/check-process-daemon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }})
Expand Down
52 changes: 51 additions & 1 deletion packages/process-daemon/SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,56 @@ 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, 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`, `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` 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, 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
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.
Expand All @@ -131,7 +181,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", "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
Expand Down
19 changes: 15 additions & 4 deletions packages/process-daemon/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -165,14 +165,16 @@ impl TerminalClient {
env: request.env,
cols: request.cols,
rows: request.rows,
mode: request.mode,
output_path: request.output_path,
})? {
Response::Created { terminal } => Ok(terminal),
response => unexpected(response),
}
}

pub fn list(&self) -> Result<Vec<TerminalInfo>> {
match self.request(Request::List)? {
match self.request(Request::List { mode: None })? {
Response::Terminals { terminals } => Ok(terminals),
response => unexpected(response),
}
Expand Down Expand Up @@ -524,8 +526,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, .. } => {
Expand Down Expand Up @@ -588,6 +595,8 @@ fn play(directory: &Path) -> Result<()> {
cwd: env::current_dir()?,
cols: 100,
rows: 30,
mode: ProcessMode::Pty,
output_path: None,
}
};
let info = client.create(request)?;
Expand Down Expand Up @@ -665,6 +674,8 @@ fn play(directory: &Path) -> Result<()> {
cwd: env::current_dir()?,
cols: 100,
rows: 30,
mode: ProcessMode::Pty,
output_path: None,
})?;
active = Some(info.id);
thread::sleep(Duration::from_millis(200));
Expand Down
110 changes: 108 additions & 2 deletions packages/process-daemon/src/daemon/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,112 @@ 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)]
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.
///
/// 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)]
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);

/// 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<Blocked> {
// 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);
}
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");
}
// 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<Control>) -> 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 {
/// Longest the loop sleeps without a wake or deadline.
safety_net: Duration,
Expand Down Expand Up @@ -405,10 +503,14 @@ fn stream_subscription(
},
StreamEvent::Exited {
exit_code,
signal,
final_offset,
output_error,
} => Response::Exited {
exit_code,
signal,
final_offset,
output_error,
},
StreamEvent::ControllerChanged {
attachment_id,
Expand Down Expand Up @@ -458,6 +560,8 @@ fn dispatch(
env,
cols,
rows,
mode,
output_path,
} => Response::Created {
terminal: service.create(CreateTerminal {
program,
Expand All @@ -468,10 +572,12 @@ fn dispatch(
env,
cols,
rows,
mode,
output_path,
})?,
},
Request::List => Response::Terminals {
terminals: service.list()?,
Request::List { mode } => Response::Terminals {
terminals: service.list_mode(mode.unwrap_or_default())?,
},
Request::Write {
id,
Expand Down
Loading
Loading