mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
f2969f36e8
## Why A long-running unified exec process started with `tty: false` could not be interrupted via `write_stdin`: ordinary non-TTY stdin writes are rejected once stdin is closed, but an exact U+0003 payload should still map to a process interrupt. The interrupt should flow through the same process lifecycle path as a real signal so Codex preserves process-reported output and exit metadata instead of fabricating a Ctrl-C exit code or tearing down the session early. ## What Changed - Add `process/signal` to exec-server with `ProcessSignal::Interrupt` and an empty response. - Add a non-consuming `ProcessHandle::signal` path for spawned processes; on Unix it sends SIGINT to the process group and leaves terminate/hard-kill unchanged. - Route non-TTY U+0003 `write_stdin` through `process.signal(...)` instead of `terminate`, then let the normal post-write collection path drain output and observe exit. - Add exec-server coverage where a shell `trap INT` handler prints the signal and exits with its own code. - Add unified exec coverage where a `tty: false` process traps SIGINT, emits output, and exits with its own code. ## Validation - `just test -p codex-exec-server exec_process_signal_interrupts_process` - `just test -p codex-exec-server` - `just test -p codex-core write_stdin_ctrl_c_interrupts_non_tty_session`
190 lines
5.4 KiB
Rust
190 lines
5.4 KiB
Rust
//! Process-group helpers shared by pipe/pty and shell command execution.
|
|
//!
|
|
//! This module centralizes the OS-specific pieces that ensure a spawned
|
|
//! command can be cleaned up reliably:
|
|
//! - `set_process_group` is called in `pre_exec` so the child starts its own
|
|
//! process group.
|
|
//! - `detach_from_tty` starts a new session so non-interactive children do not
|
|
//! inherit the controlling TTY.
|
|
//! - `kill_process_group_by_pid` targets the whole group (children/grandchildren)
|
|
//! - `kill_process_group` targets a known process group ID directly
|
|
//! instead of a single PID.
|
|
//! - `set_parent_death_signal` (Linux only) arranges for the child to receive a
|
|
//! `SIGTERM` when the parent exits, and re-checks the parent PID to avoid
|
|
//! races during fork/exec.
|
|
//!
|
|
//! On non-Unix platforms these helpers are no-ops.
|
|
|
|
use std::io;
|
|
|
|
use tokio::process::Child;
|
|
|
|
#[cfg(target_os = "linux")]
|
|
/// Ensure the child receives SIGTERM when the original parent dies.
|
|
///
|
|
/// This should run in `pre_exec` and uses `parent_pid` captured before spawn to
|
|
/// avoid a race where the parent exits between fork and exec.
|
|
pub fn set_parent_death_signal(parent_pid: libc::pid_t) -> io::Result<()> {
|
|
if unsafe { libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM) } == -1 {
|
|
return Err(io::Error::last_os_error());
|
|
}
|
|
|
|
if unsafe { libc::getppid() } != parent_pid {
|
|
unsafe {
|
|
libc::raise(libc::SIGTERM);
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(not(target_os = "linux"))]
|
|
/// No-op on non-Linux platforms.
|
|
pub fn set_parent_death_signal(_parent_pid: i32) -> io::Result<()> {
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
/// Detach from the controlling TTY by starting a new session.
|
|
pub fn detach_from_tty() -> io::Result<()> {
|
|
let result = unsafe { libc::setsid() };
|
|
if result == -1 {
|
|
let err = io::Error::last_os_error();
|
|
if err.raw_os_error() == Some(libc::EPERM) {
|
|
return set_process_group();
|
|
}
|
|
return Err(err);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
/// No-op on non-Unix platforms.
|
|
pub fn detach_from_tty() -> io::Result<()> {
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
/// Put the calling process into its own process group.
|
|
///
|
|
/// Intended for use in `pre_exec` so the child becomes the group leader.
|
|
pub fn set_process_group() -> io::Result<()> {
|
|
let result = unsafe { libc::setpgid(0, 0) };
|
|
if result == -1 {
|
|
Err(io::Error::last_os_error())
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
/// No-op on non-Unix platforms.
|
|
pub fn set_process_group() -> io::Result<()> {
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
/// Kill the process group for the given PID (best-effort).
|
|
///
|
|
/// This resolves the PGID for `pid` and sends SIGKILL to the whole group.
|
|
pub fn kill_process_group_by_pid(pid: u32) -> io::Result<()> {
|
|
use std::io::ErrorKind;
|
|
|
|
let pid = pid as libc::pid_t;
|
|
let pgid = unsafe { libc::getpgid(pid) };
|
|
if pgid == -1 {
|
|
let err = io::Error::last_os_error();
|
|
if err.kind() != ErrorKind::NotFound && err.raw_os_error() != Some(libc::ESRCH) {
|
|
return Err(err);
|
|
}
|
|
return Ok(());
|
|
}
|
|
|
|
let result = unsafe { libc::killpg(pgid, libc::SIGKILL) };
|
|
if result == -1 {
|
|
let err = io::Error::last_os_error();
|
|
if err.kind() != ErrorKind::NotFound && err.raw_os_error() != Some(libc::ESRCH) {
|
|
return Err(err);
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
/// No-op on non-Unix platforms.
|
|
pub fn kill_process_group_by_pid(_pid: u32) -> io::Result<()> {
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
fn signal_process_group_id(pgid: libc::pid_t, signal: libc::c_int) -> io::Result<bool> {
|
|
use std::io::ErrorKind;
|
|
|
|
let result = unsafe { libc::killpg(pgid, signal) };
|
|
if result == -1 {
|
|
let err = io::Error::last_os_error();
|
|
if err.kind() == ErrorKind::NotFound || err.raw_os_error() == Some(libc::ESRCH) {
|
|
return Ok(false);
|
|
}
|
|
return Err(err);
|
|
}
|
|
|
|
Ok(true)
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
/// Send SIGTERM to a specific process group ID (best-effort).
|
|
///
|
|
/// Returns `Ok(true)` when SIGTERM was delivered to an existing group and
|
|
/// `Ok(false)` when the group no longer exists.
|
|
pub fn terminate_process_group(process_group_id: u32) -> io::Result<bool> {
|
|
signal_process_group_id(process_group_id as libc::pid_t, libc::SIGTERM)
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
/// No-op on non-Unix platforms.
|
|
pub fn terminate_process_group(_process_group_id: u32) -> io::Result<bool> {
|
|
Ok(false)
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
/// Send SIGINT to a specific process group ID (best-effort).
|
|
pub fn interrupt_process_group(process_group_id: u32) -> io::Result<()> {
|
|
signal_process_group_id(process_group_id as libc::pid_t, libc::SIGINT).map(|_| ())
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
/// No-op on non-Unix platforms.
|
|
pub fn interrupt_process_group(_process_group_id: u32) -> io::Result<()> {
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
/// Kill a specific process group ID (best-effort).
|
|
pub fn kill_process_group(process_group_id: u32) -> io::Result<()> {
|
|
signal_process_group_id(process_group_id as libc::pid_t, libc::SIGKILL).map(|_| ())
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
/// No-op on non-Unix platforms.
|
|
pub fn kill_process_group(_process_group_id: u32) -> io::Result<()> {
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
/// Kill the process group for a tokio child (best-effort).
|
|
pub fn kill_child_process_group(child: &mut Child) -> io::Result<()> {
|
|
if let Some(pid) = child.id() {
|
|
return kill_process_group_by_pid(pid);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
/// No-op on non-Unix platforms.
|
|
pub fn kill_child_process_group(_child: &mut Child) -> io::Result<()> {
|
|
Ok(())
|
|
}
|