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
@@ -1995,6 +1995,249 @@ async fn write_stdin_returns_exit_metadata_and_clears_session() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn write_stdin_ctrl_c_interrupts_non_tty_session() -> Result<()> {
|
||||
assert_write_stdin_ctrl_c_interrupts_non_tty_session(
|
||||
"trap",
|
||||
"trap 'echo INT-TRAP; exit 42' INT; echo READY; while true; do sleep 30; done",
|
||||
/*expected_exit_code*/ 42,
|
||||
Some("INT-TRAP"),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn write_stdin_ctrl_c_default_interrupt_reports_130_for_non_tty_session() -> Result<()> {
|
||||
assert_write_stdin_ctrl_c_interrupts_non_tty_session(
|
||||
"default",
|
||||
"echo READY; exec sleep 30",
|
||||
/*expected_exit_code*/ 130,
|
||||
/*expected_interrupt_output*/ None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn assert_write_stdin_ctrl_c_interrupts_non_tty_session(
|
||||
test_name: &str,
|
||||
command: &str,
|
||||
expected_exit_code: i32,
|
||||
expected_interrupt_output: Option<&str>,
|
||||
) -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
skip_if_sandbox!(Ok(()));
|
||||
skip_if_windows!(Ok(()));
|
||||
|
||||
let server = start_mock_server().await;
|
||||
|
||||
let mut builder = test_codex().with_config(|config| {
|
||||
if let Err(err) = config.features.enable(Feature::UnifiedExec) {
|
||||
panic!("test config should allow feature update: {err}");
|
||||
}
|
||||
});
|
||||
let test = builder.build_with_remote_env(&server).await?;
|
||||
|
||||
let start_call_id = format!("uexec-non-tty-interrupt-{test_name}-start");
|
||||
let interrupt_call_id = format!("uexec-non-tty-interrupt-{test_name}");
|
||||
|
||||
let start_args = serde_json::json!({
|
||||
"cmd": command,
|
||||
"yield_time_ms": 250,
|
||||
"tty": false,
|
||||
});
|
||||
let interrupt_args = serde_json::json!({
|
||||
"chars": "\u{3}",
|
||||
"session_id": 1000,
|
||||
"yield_time_ms": 1000,
|
||||
});
|
||||
|
||||
let responses = vec![
|
||||
sse(vec![
|
||||
ev_response_created("resp-1"),
|
||||
ev_function_call(
|
||||
&start_call_id,
|
||||
"exec_command",
|
||||
&serde_json::to_string(&start_args)?,
|
||||
),
|
||||
ev_completed("resp-1"),
|
||||
]),
|
||||
sse(vec![
|
||||
ev_response_created("resp-2"),
|
||||
ev_function_call(
|
||||
&interrupt_call_id,
|
||||
"write_stdin",
|
||||
&serde_json::to_string(&interrupt_args)?,
|
||||
),
|
||||
ev_completed("resp-2"),
|
||||
]),
|
||||
sse(vec![
|
||||
ev_assistant_message("msg-1", "done"),
|
||||
ev_completed("resp-3"),
|
||||
]),
|
||||
];
|
||||
let request_log = mount_sse_sequence(&server, responses).await;
|
||||
|
||||
submit_unified_exec_turn(
|
||||
&test,
|
||||
"interrupt non-tty unified exec",
|
||||
PermissionProfile::Disabled,
|
||||
)
|
||||
.await?;
|
||||
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::TurnComplete(_))
|
||||
})
|
||||
.await;
|
||||
|
||||
let requests = request_log.requests();
|
||||
assert!(!requests.is_empty(), "expected at least one POST request");
|
||||
let bodies = requests
|
||||
.into_iter()
|
||||
.map(|request| request.body_json())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let outputs = collect_tool_outputs(&bodies)?;
|
||||
|
||||
let start_output = outputs
|
||||
.get(&start_call_id)
|
||||
.with_context(|| format!("missing start output for exec_command {start_call_id}"))?;
|
||||
assert_eq!(
|
||||
start_output.process_id.as_deref(),
|
||||
Some("1000"),
|
||||
"exec_command should leave a running non-TTY session"
|
||||
);
|
||||
assert!(
|
||||
start_output.exit_code.is_none(),
|
||||
"initial exec_command should not include exit_code while session is running"
|
||||
);
|
||||
assert!(
|
||||
start_output.output.contains("READY"),
|
||||
"start output should include command readiness marker, got {:?}",
|
||||
start_output.output
|
||||
);
|
||||
|
||||
let interrupt_output = outputs
|
||||
.get(&interrupt_call_id)
|
||||
.with_context(|| format!("missing interrupt output for write_stdin {interrupt_call_id}"))?;
|
||||
assert!(
|
||||
interrupt_output.process_id.is_none(),
|
||||
"interrupted process should be cleared from the session map"
|
||||
);
|
||||
assert_eq!(
|
||||
interrupt_output.exit_code,
|
||||
Some(expected_exit_code),
|
||||
"interrupt should preserve the process-reported exit code"
|
||||
);
|
||||
if let Some(expected_interrupt_output) = expected_interrupt_output {
|
||||
assert!(
|
||||
interrupt_output.output.contains(expected_interrupt_output),
|
||||
"interrupt should drain output from the signal handler, got {:?}",
|
||||
interrupt_output.output
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
#[cfg_attr(not(windows), ignore = "Windows-only unified exec interrupt test")]
|
||||
async fn write_stdin_ctrl_c_reports_unsupported_interrupt_to_model_on_windows() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
skip_if_sandbox!(Ok(()));
|
||||
|
||||
let server = start_mock_server().await;
|
||||
|
||||
let mut builder = test_codex().with_config(|config| {
|
||||
config
|
||||
.features
|
||||
.enable(Feature::UnifiedExec)
|
||||
.expect("test config should allow feature update");
|
||||
});
|
||||
let test = builder.build_with_remote_env(&server).await?;
|
||||
|
||||
let start_call_id = "uexec-windows-interrupt-start";
|
||||
let interrupt_call_id = "uexec-windows-interrupt";
|
||||
|
||||
let start_args = serde_json::json!({
|
||||
"shell": "cmd",
|
||||
"cmd": "echo READY && ping -n 30 127.0.0.1 >NUL",
|
||||
"yield_time_ms": 250,
|
||||
"tty": false,
|
||||
});
|
||||
let interrupt_args = serde_json::json!({
|
||||
"chars": "\u{3}",
|
||||
"session_id": 1000,
|
||||
"yield_time_ms": 1000,
|
||||
});
|
||||
|
||||
let responses = vec![
|
||||
sse(vec![
|
||||
ev_response_created("resp-1"),
|
||||
ev_function_call(
|
||||
start_call_id,
|
||||
"exec_command",
|
||||
&serde_json::to_string(&start_args)?,
|
||||
),
|
||||
ev_completed("resp-1"),
|
||||
]),
|
||||
sse(vec![
|
||||
ev_response_created("resp-2"),
|
||||
ev_function_call(
|
||||
interrupt_call_id,
|
||||
"write_stdin",
|
||||
&serde_json::to_string(&interrupt_args)?,
|
||||
),
|
||||
ev_completed("resp-2"),
|
||||
]),
|
||||
sse(vec![
|
||||
ev_response_created("resp-3"),
|
||||
ev_assistant_message("msg-1", "done"),
|
||||
ev_completed("resp-3"),
|
||||
]),
|
||||
];
|
||||
let request_log = mount_sse_sequence(&server, responses).await;
|
||||
|
||||
submit_unified_exec_turn(
|
||||
&test,
|
||||
"interrupt non-tty unified exec on Windows",
|
||||
PermissionProfile::Disabled,
|
||||
)
|
||||
.await?;
|
||||
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::TurnComplete(_))
|
||||
})
|
||||
.await;
|
||||
|
||||
let start_output = request_log
|
||||
.function_call_output_text(start_call_id)
|
||||
.expect("missing start output for exec_command");
|
||||
let start_output = parse_unified_exec_output(&start_output)?;
|
||||
assert_eq!(
|
||||
start_output.process_id.as_deref(),
|
||||
Some("1000"),
|
||||
"exec_command should leave a running non-TTY session"
|
||||
);
|
||||
assert!(
|
||||
start_output.output.contains("READY"),
|
||||
"start output should include command readiness marker, got {:?}",
|
||||
start_output.output
|
||||
);
|
||||
|
||||
let interrupt_output = request_log
|
||||
.function_call_output_text(interrupt_call_id)
|
||||
.expect("missing interrupt output for write_stdin");
|
||||
assert!(
|
||||
interrupt_output.contains("write_stdin failed"),
|
||||
"model-visible write_stdin output should report failure, got {interrupt_output:?}"
|
||||
);
|
||||
assert!(
|
||||
interrupt_output.contains("process interrupt is not supported by this process backend"),
|
||||
"model-visible write_stdin output should explain unsupported interrupt, got {interrupt_output:?}"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn unified_exec_emits_end_event_when_session_dies_via_stdin() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
Reference in New Issue
Block a user