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
@@ -10,6 +10,7 @@ use codex_protocol::config_types::EnvironmentVariablePattern;
|
||||
use codex_protocol::config_types::ShellEnvironmentPolicy;
|
||||
use codex_protocol::shell_environment;
|
||||
use codex_utils_pty::ExecCommandSession;
|
||||
use codex_utils_pty::ProcessSignal as PtyProcessSignal;
|
||||
use codex_utils_pty::TerminalSize;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::sync::Notify;
|
||||
@@ -33,8 +34,11 @@ use crate::protocol::ExecOutputStream;
|
||||
use crate::protocol::ExecParams;
|
||||
use crate::protocol::ExecResponse;
|
||||
use crate::protocol::ProcessOutputChunk;
|
||||
use crate::protocol::ProcessSignal;
|
||||
use crate::protocol::ReadParams;
|
||||
use crate::protocol::ReadResponse;
|
||||
use crate::protocol::SignalParams;
|
||||
use crate::protocol::SignalResponse;
|
||||
use crate::protocol::TerminateParams;
|
||||
use crate::protocol::TerminateResponse;
|
||||
use crate::protocol::WriteParams;
|
||||
@@ -272,7 +276,6 @@ impl LocalProcess {
|
||||
&self,
|
||||
params: ReadParams,
|
||||
) -> Result<ReadResponse, JSONRPCErrorError> {
|
||||
let _process_id = params.process_id.clone();
|
||||
let after_seq = params.after_seq.unwrap_or(0);
|
||||
let max_bytes = params.max_bytes.unwrap_or(usize::MAX);
|
||||
let wait = Duration::from_millis(params.wait_ms.unwrap_or(0));
|
||||
@@ -351,7 +354,6 @@ impl LocalProcess {
|
||||
&self,
|
||||
params: WriteParams,
|
||||
) -> Result<WriteResponse, JSONRPCErrorError> {
|
||||
let _process_id = params.process_id.clone();
|
||||
let _input_bytes = params.chunk.0.len();
|
||||
let writer_tx = {
|
||||
let process_map = self.inner.processes.lock().await;
|
||||
@@ -383,11 +385,33 @@ impl LocalProcess {
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn signal_process(
|
||||
&self,
|
||||
params: SignalParams,
|
||||
) -> Result<SignalResponse, JSONRPCErrorError> {
|
||||
{
|
||||
let process_map = self.inner.processes.lock().await;
|
||||
match process_map.get(¶ms.process_id) {
|
||||
Some(ProcessEntry::Running(process)) => {
|
||||
if process.exit_code.is_some() {
|
||||
return Ok(SignalResponse {});
|
||||
}
|
||||
process
|
||||
.session
|
||||
.signal(pty_process_signal(params.signal))
|
||||
.map_err(|err| internal_error(format!("failed to signal process: {err}")))?
|
||||
}
|
||||
Some(ProcessEntry::Starting) | None => {}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(SignalResponse {})
|
||||
}
|
||||
|
||||
pub(crate) async fn terminate_process(
|
||||
&self,
|
||||
params: TerminateParams,
|
||||
) -> Result<TerminateResponse, JSONRPCErrorError> {
|
||||
let _process_id = params.process_id.clone();
|
||||
let running = {
|
||||
let process_map = self.inner.processes.lock().await;
|
||||
match process_map.get(¶ms.process_id) {
|
||||
@@ -483,6 +507,10 @@ impl ExecProcess for LocalExecProcess {
|
||||
self.backend.write(&self.process_id, chunk).await
|
||||
}
|
||||
|
||||
async fn signal(&self, signal: ProcessSignal) -> Result<(), ExecServerError> {
|
||||
self.backend.signal(&self.process_id, signal).await
|
||||
}
|
||||
|
||||
async fn terminate(&self) -> Result<(), ExecServerError> {
|
||||
self.backend.terminate(&self.process_id).await
|
||||
}
|
||||
@@ -519,6 +547,20 @@ impl LocalProcess {
|
||||
.map_err(map_handler_error)
|
||||
}
|
||||
|
||||
async fn signal(
|
||||
&self,
|
||||
process_id: &ProcessId,
|
||||
signal: ProcessSignal,
|
||||
) -> Result<(), ExecServerError> {
|
||||
self.signal_process(SignalParams {
|
||||
process_id: process_id.clone(),
|
||||
signal,
|
||||
})
|
||||
.await
|
||||
.map_err(map_handler_error)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn terminate(&self, process_id: &ProcessId) -> Result<(), ExecServerError> {
|
||||
self.terminate_process(TerminateParams {
|
||||
process_id: process_id.clone(),
|
||||
@@ -529,6 +571,12 @@ impl LocalProcess {
|
||||
}
|
||||
}
|
||||
|
||||
fn pty_process_signal(signal: ProcessSignal) -> PtyProcessSignal {
|
||||
match signal {
|
||||
ProcessSignal::Interrupt => PtyProcessSignal::Interrupt,
|
||||
}
|
||||
}
|
||||
|
||||
fn map_handler_error(error: JSONRPCErrorError) -> ExecServerError {
|
||||
ExecServerError::Server {
|
||||
code: error.code,
|
||||
|
||||
Reference in New Issue
Block a user