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
@@ -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)();
|
||||
|
||||
Reference in New Issue
Block a user