mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
[codex] Handle Ctrl-C for non-TTY unified exec (#26734)
## 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`
This commit is contained in:
committed by
GitHub
Unverified
parent
f574946960
commit
f2969f36e8
@@ -17,6 +17,8 @@ pub use pipe::spawn_process_no_stdin as spawn_pipe_process_no_stdin;
|
||||
pub use process::ProcessDriver;
|
||||
/// Handle for interacting with a spawned process (PTY or pipe).
|
||||
pub use process::ProcessHandle;
|
||||
/// Process signal supported by spawned-process handles.
|
||||
pub use process::ProcessSignal;
|
||||
/// Bundle of process handles plus split output and exit receivers returned by spawn helpers.
|
||||
pub use process::SpawnedProcess;
|
||||
/// Terminal size in character cells used for PTY spawn and resize operations.
|
||||
|
||||
@@ -19,7 +19,9 @@ use tokio::task::JoinHandle;
|
||||
|
||||
use crate::process::ChildTerminator;
|
||||
use crate::process::ProcessHandle;
|
||||
use crate::process::ProcessSignal;
|
||||
use crate::process::SpawnedProcess;
|
||||
use crate::process::exit_code_from_status;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
use libc;
|
||||
@@ -32,6 +34,22 @@ struct PipeChildTerminator {
|
||||
}
|
||||
|
||||
impl ChildTerminator for PipeChildTerminator {
|
||||
fn signal(&mut self, signal: ProcessSignal) -> io::Result<()> {
|
||||
match signal {
|
||||
ProcessSignal::Interrupt => {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
crate::process_group::interrupt_process_group(self.process_group_id)
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
Err(crate::process::unsupported_signal(signal))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn kill(&mut self) -> io::Result<()> {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
@@ -209,7 +227,7 @@ async fn spawn_process_with_stdin_mode(
|
||||
let wait_exit_code = Arc::clone(&exit_code);
|
||||
let wait_handle: JoinHandle<()> = tokio::spawn(async move {
|
||||
let code = match child.wait().await {
|
||||
Ok(status) => status.code().unwrap_or(-1),
|
||||
Ok(status) => exit_code_from_status(status),
|
||||
Err(_) => -1,
|
||||
};
|
||||
wait_exit_status.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
|
||||
@@ -2,6 +2,7 @@ use core::fmt;
|
||||
use std::io;
|
||||
#[cfg(unix)]
|
||||
use std::os::fd::RawFd;
|
||||
use std::process::ExitStatus;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex as StdMutex;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
@@ -17,7 +18,39 @@ use tokio::sync::watch;
|
||||
use tokio::task::AbortHandle;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ProcessSignal {
|
||||
Interrupt,
|
||||
}
|
||||
|
||||
pub(crate) fn unsupported_signal(signal: ProcessSignal) -> io::Error {
|
||||
match signal {
|
||||
ProcessSignal::Interrupt => io::Error::new(
|
||||
io::ErrorKind::Unsupported,
|
||||
"process interrupt is not supported by this process backend",
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn exit_code_from_status(status: ExitStatus) -> i32 {
|
||||
if let Some(code) = status.code() {
|
||||
return code;
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::process::ExitStatusExt;
|
||||
if let Some(signal) = status.signal() {
|
||||
return 128 + signal;
|
||||
}
|
||||
}
|
||||
|
||||
-1
|
||||
}
|
||||
|
||||
pub(crate) trait ChildTerminator: Send + Sync {
|
||||
fn signal(&mut self, signal: ProcessSignal) -> io::Result<()>;
|
||||
|
||||
fn kill(&mut self) -> io::Result<()>;
|
||||
}
|
||||
|
||||
@@ -193,6 +226,17 @@ impl ProcessHandle {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn signal(&self, signal: ProcessSignal) -> io::Result<()> {
|
||||
let Ok(mut killer_opt) = self.killer.lock() else {
|
||||
return Ok(());
|
||||
};
|
||||
let Some(killer) = killer_opt.as_mut() else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
killer.signal(signal)
|
||||
}
|
||||
|
||||
/// Attempts to kill the child and abort helper tasks.
|
||||
pub fn terminate(&self) {
|
||||
self.request_terminate();
|
||||
@@ -232,6 +276,10 @@ struct ClosureTerminator {
|
||||
}
|
||||
|
||||
impl ChildTerminator for ClosureTerminator {
|
||||
fn signal(&mut self, signal: ProcessSignal) -> io::Result<()> {
|
||||
Err(unsupported_signal(signal))
|
||||
}
|
||||
|
||||
fn kill(&mut self) -> io::Result<()> {
|
||||
if let Some(inner) = self.inner.as_mut() {
|
||||
(inner)();
|
||||
|
||||
@@ -118,15 +118,10 @@ pub fn kill_process_group_by_pid(_pid: u32) -> io::Result<()> {
|
||||
}
|
||||
|
||||
#[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> {
|
||||
fn signal_process_group_id(pgid: libc::pid_t, signal: libc::c_int) -> io::Result<bool> {
|
||||
use std::io::ErrorKind;
|
||||
|
||||
let pgid = process_group_id as libc::pid_t;
|
||||
let result = unsafe { libc::killpg(pgid, libc::SIGTERM) };
|
||||
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) {
|
||||
@@ -138,27 +133,37 @@ pub fn terminate_process_group(process_group_id: u32) -> io::Result<bool> {
|
||||
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<()> {
|
||||
use std::io::ErrorKind;
|
||||
|
||||
let pgid = process_group_id as libc::pid_t;
|
||||
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(())
|
||||
signal_process_group_id(process_group_id as libc::pid_t, libc::SIGKILL).map(|_| ())
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
|
||||
@@ -30,10 +30,13 @@ use tokio::task::JoinHandle;
|
||||
|
||||
use crate::process::ChildTerminator;
|
||||
use crate::process::ProcessHandle;
|
||||
use crate::process::ProcessSignal;
|
||||
use crate::process::PtyHandles;
|
||||
use crate::process::PtyMasterHandle;
|
||||
use crate::process::SpawnedProcess;
|
||||
use crate::process::TerminalSize;
|
||||
#[cfg(unix)]
|
||||
use crate::process::exit_code_from_status;
|
||||
|
||||
/// Returns true when ConPTY support is available (Windows only).
|
||||
#[cfg(windows)]
|
||||
@@ -54,6 +57,19 @@ struct PtyChildTerminator {
|
||||
}
|
||||
|
||||
impl ChildTerminator for PtyChildTerminator {
|
||||
fn signal(&mut self, signal: ProcessSignal) -> std::io::Result<()> {
|
||||
match signal {
|
||||
ProcessSignal::Interrupt => {
|
||||
#[cfg(unix)]
|
||||
if let Some(process_group_id) = self.process_group_id {
|
||||
return crate::process_group::interrupt_process_group(process_group_id);
|
||||
}
|
||||
|
||||
Err(crate::process::unsupported_signal(signal))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn kill(&mut self) -> std::io::Result<()> {
|
||||
#[cfg(unix)]
|
||||
if let Some(process_group_id) = self.process_group_id {
|
||||
@@ -81,6 +97,14 @@ struct RawPidTerminator {
|
||||
|
||||
#[cfg(unix)]
|
||||
impl ChildTerminator for RawPidTerminator {
|
||||
fn signal(&mut self, signal: ProcessSignal) -> std::io::Result<()> {
|
||||
match signal {
|
||||
ProcessSignal::Interrupt => {
|
||||
crate::process_group::interrupt_process_group(self.process_group_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn kill(&mut self) -> std::io::Result<()> {
|
||||
crate::process_group::kill_process_group(self.process_group_id)
|
||||
}
|
||||
@@ -368,7 +392,7 @@ async fn spawn_process_preserving_fds(
|
||||
let wait_exit_code = Arc::clone(&exit_code);
|
||||
let wait_handle: JoinHandle<()> = tokio::task::spawn_blocking(move || {
|
||||
let code = match child.wait() {
|
||||
Ok(status) => status.code().unwrap_or(-1),
|
||||
Ok(status) => exit_code_from_status(status),
|
||||
Err(_) => -1,
|
||||
};
|
||||
wait_exit_status.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
|
||||
Reference in New Issue
Block a user