mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
Bound local exec-server output draining after process exit (#52652)
## Why A local command can exit while a descendant keeps its inherited output pipes open, delaying `exec_command` completion and turn completion. ## What changed Start the bounded output drain when a local exec-server process exits, whether exit is reported by an event or recovered through `process/read`. Remote execution continues waiting for output closure so buffered or delayed output is retained. ## Testing Add regression coverage for local shell snapshot execution completing before a descendant releases stdout, preserving the foreground output and exit code. Add remote event-stream cases that retain output arriving after both reported and recovered process exit. GitOrigin-RevId: d38bcd394be42cf48d2af02733e883b02d1dd62f
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use super::process::OutputDrainPolicy;
|
||||
use super::*;
|
||||
use crate::codex_thread::BackgroundTerminalInfo;
|
||||
use crate::exec::ExecCapturePolicy;
|
||||
@@ -291,15 +292,18 @@ async fn blocking_terminate_unified_process(
|
||||
) -> anyhow::Result<Arc<UnifiedExecProcess>> {
|
||||
let (wake_tx, _wake_rx) = watch::channel(0);
|
||||
Ok(Arc::new(
|
||||
UnifiedExecProcess::from_exec_server_started(StartedExecProcess {
|
||||
process: Arc::new(BlockingTerminateExecProcess {
|
||||
process_id: process_id.to_string().into(),
|
||||
terminate_started,
|
||||
allow_terminate,
|
||||
wake_tx,
|
||||
}),
|
||||
sandbox_type: Some(codex_sandboxing::SandboxType::None),
|
||||
})
|
||||
UnifiedExecProcess::from_exec_server_started(
|
||||
StartedExecProcess {
|
||||
process: Arc::new(BlockingTerminateExecProcess {
|
||||
process_id: process_id.to_string().into(),
|
||||
terminate_started,
|
||||
allow_terminate,
|
||||
wake_tx,
|
||||
}),
|
||||
sandbox_type: Some(codex_sandboxing::SandboxType::None),
|
||||
},
|
||||
OutputDrainPolicy::WaitForOutputClosure,
|
||||
)
|
||||
.await?,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -74,6 +74,15 @@ impl Drop for OutputTaskGuard {
|
||||
}
|
||||
}
|
||||
|
||||
/// Controls whether process exit can start the collectors' bounded output drain.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub(super) enum OutputDrainPolicy {
|
||||
/// Match direct local execution when descendants retain inherited output pipes.
|
||||
BoundedAfterExit,
|
||||
/// Remote foreground output may still be buffered or in transit after exit.
|
||||
WaitForOutputClosure,
|
||||
}
|
||||
|
||||
/// Transport-specific process handle used by unified exec.
|
||||
enum ProcessHandle {
|
||||
Local(Box<ExecCommandSession>),
|
||||
@@ -385,6 +394,7 @@ impl UnifiedExecProcess {
|
||||
|
||||
pub(super) async fn from_exec_server_started(
|
||||
started: StartedExecProcess,
|
||||
output_drain_policy: OutputDrainPolicy,
|
||||
) -> Result<Self, UnifiedExecError> {
|
||||
let process_handle = ProcessHandle::ExecServer(Arc::clone(&started.process));
|
||||
// Older peers do not report this field. In that case, skip local
|
||||
@@ -394,6 +404,7 @@ impl UnifiedExecProcess {
|
||||
let output_handles = managed.output_handles().clone();
|
||||
managed.output_task = Some(Self::spawn_exec_server_output_task(
|
||||
started,
|
||||
output_drain_policy,
|
||||
output_handles,
|
||||
managed.output_tx.clone(),
|
||||
managed.state_tx.clone(),
|
||||
@@ -422,6 +433,7 @@ impl UnifiedExecProcess {
|
||||
|
||||
fn spawn_exec_server_output_task(
|
||||
started: StartedExecProcess,
|
||||
output_drain_policy: OutputDrainPolicy,
|
||||
output_handles: OutputHandles,
|
||||
output_tx: broadcast::Sender<Vec<u8>>,
|
||||
state_tx: watch::Sender<ProcessState>,
|
||||
@@ -527,6 +539,9 @@ impl UnifiedExecProcess {
|
||||
state
|
||||
});
|
||||
}
|
||||
if exited && output_drain_policy == OutputDrainPolicy::BoundedAfterExit {
|
||||
cancellation_token.cancel();
|
||||
}
|
||||
if closed {
|
||||
output_closed.store(true, Ordering::Release);
|
||||
output_closed_notify.notify_waiters();
|
||||
@@ -564,6 +579,9 @@ impl UnifiedExecProcess {
|
||||
let mut state = state_tx.borrow().clone();
|
||||
state.sandbox_denied |= sandbox_denied.unwrap_or(false);
|
||||
let _ = state_tx.send_replace(state.exited(Some(exit_code)));
|
||||
if output_drain_policy == OutputDrainPolicy::BoundedAfterExit {
|
||||
cancellation_token.cancel();
|
||||
}
|
||||
}
|
||||
ExecProcessEvent::Closed { seq } => {
|
||||
if seq <= last_seq {
|
||||
|
||||
@@ -13,6 +13,7 @@ use tokio_util::sync::CancellationToken;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::oneshot::Completion;
|
||||
use super::process::OutputDrainPolicy;
|
||||
|
||||
use crate::codex_thread::BackgroundTerminalInfo;
|
||||
use crate::exec_env::CODEX_PERMISSION_PROFILE_ENV_VAR;
|
||||
@@ -1343,7 +1344,13 @@ impl UnifiedExecProcessManager {
|
||||
None => backend.start(params).await,
|
||||
}
|
||||
.map_err(|err| UnifiedExecError::create_process(err.to_string()))?;
|
||||
return UnifiedExecProcess::from_exec_server_started(started).await;
|
||||
let output_drain_policy = if environment.is_remote() {
|
||||
OutputDrainPolicy::WaitForOutputClosure
|
||||
} else {
|
||||
OutputDrainPolicy::BoundedAfterExit
|
||||
};
|
||||
return UnifiedExecProcess::from_exec_server_started(started, output_drain_policy)
|
||||
.await;
|
||||
}
|
||||
|
||||
// TODO(anp): Keep PathUri through the local PTY/process launch boundary.
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use super::process::OutputDrainPolicy;
|
||||
use super::process::UnifiedExecProcess;
|
||||
use crate::unified_exec::UnifiedExecError;
|
||||
use codex_exec_server::ExecProcess;
|
||||
@@ -104,7 +105,7 @@ pub(super) async fn remote_process(
|
||||
sandbox_type: sandbox_type.into(),
|
||||
};
|
||||
|
||||
UnifiedExecProcess::from_exec_server_started(started)
|
||||
UnifiedExecProcess::from_exec_server_started(started, OutputDrainPolicy::WaitForOutputClosure)
|
||||
.await
|
||||
.expect("remote process should start")
|
||||
}
|
||||
|
||||
@@ -381,6 +381,64 @@ async fn run_no_shell_turn(harness: &TestCodexHarness) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn shell_snapshot_v2_returns_before_inherited_output_closes() -> Result<()> {
|
||||
use std::io::Write;
|
||||
|
||||
skip_if_remote!(Ok(()), "tests the local executor's inherited output pipes");
|
||||
let profile_home = tempfile::tempdir()?;
|
||||
let gate_path = profile_home.path().join("output-gate");
|
||||
let status = std::process::Command::new("mkfifo")
|
||||
.arg(&gate_path)
|
||||
.status()?;
|
||||
anyhow::ensure!(status.success(), "failed to create output gate");
|
||||
let mut gate = std::fs::OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open(&gate_path)?;
|
||||
let harness = TestCodexHarness::with_auto_env_builder(shell_snapshot_v2_prewarm_builder(
|
||||
profile_home.path(),
|
||||
))
|
||||
.await?;
|
||||
|
||||
// The foreground shell exits, but its descendant retains stdout until we release the FIFO.
|
||||
// Wait for the public command-end and turn-complete notifications, not polled process state.
|
||||
let result = tokio::time::timeout(
|
||||
Duration::from_secs(10),
|
||||
run_tool_turn_on_harness(
|
||||
&harness,
|
||||
"run a command whose descendant retains stdout",
|
||||
"inherited-output",
|
||||
"exec_command",
|
||||
json!({
|
||||
"cmd": "(read -r release < \"$HOME/output-gate\") & printf 'foreground output'; exit 7",
|
||||
"yield_time_ms": 30_000,
|
||||
"tty": false,
|
||||
}),
|
||||
),
|
||||
)
|
||||
.await;
|
||||
|
||||
// Release the descendant even when completion timed out on the broken path.
|
||||
gate.write_all(b"release\n")?;
|
||||
harness.test().codex.shutdown_and_wait().await?;
|
||||
let end =
|
||||
result.expect("tool response should complete before the inherited pipe is released")?;
|
||||
assert_eq!(
|
||||
(end.exit_code, end.aggregated_output),
|
||||
(7, "foreground output".to_string()),
|
||||
);
|
||||
let output = harness.function_call_stdout("inherited-output").await;
|
||||
assert!(output.contains("Process exited with code 7"), "{output}");
|
||||
assert!(
|
||||
!output.contains("Process running with session ID"),
|
||||
"{output}"
|
||||
);
|
||||
assert!(output.ends_with("foreground output"), "{output}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn shell_snapshot_v2_warms_after_hooks_without_blocking_the_model() -> Result<()> {
|
||||
|
||||
@@ -72,6 +72,8 @@ const REPLAY_RETAINED_OUTPUT_SEQ: u64 = 800;
|
||||
#[cfg_attr(windows, allow(dead_code))]
|
||||
enum PushedExecScenario {
|
||||
Complete,
|
||||
OutputAfterExit,
|
||||
OutputAfterRecoveredExit,
|
||||
CodexPath,
|
||||
CodexPathDefaultFeature,
|
||||
CodexPathDisabledFeature,
|
||||
@@ -420,7 +422,79 @@ async fn serve_exec_with_pushed_events(
|
||||
)
|
||||
.await;
|
||||
|
||||
let mut process_read_requests = 0;
|
||||
match scenario {
|
||||
PushedExecScenario::OutputAfterExit | PushedExecScenario::OutputAfterRecoveredExit => {
|
||||
send_exec_server_json(
|
||||
&mut websocket,
|
||||
json!({
|
||||
"method": "process/output",
|
||||
"params": {
|
||||
"processId": &process_id,
|
||||
"seq": 1,
|
||||
"stream": "stdout",
|
||||
"chunk": BASE64_STANDARD.encode("pushed "),
|
||||
}
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
let mut exit = json!({
|
||||
"method": "process/exited",
|
||||
"params": {
|
||||
"processId": &process_id,
|
||||
"seq": 2,
|
||||
"exitCode": 0,
|
||||
}
|
||||
});
|
||||
if matches!(scenario, PushedExecScenario::OutputAfterExit) {
|
||||
exit["params"]["sandboxDenied"] = json!(false);
|
||||
}
|
||||
send_exec_server_json(&mut websocket, exit).await;
|
||||
if matches!(scenario, PushedExecScenario::OutputAfterRecoveredExit) {
|
||||
let read = read_exec_server_json(&mut websocket, STARTUP_TIMEOUT).await;
|
||||
assert_eq!(read["method"], "process/read");
|
||||
process_read_requests += 1;
|
||||
send_exec_server_json(
|
||||
&mut websocket,
|
||||
json!({
|
||||
"id": read["id"],
|
||||
"result": {
|
||||
"chunks": [],
|
||||
"nextSeq": 3,
|
||||
"exited": true,
|
||||
"exitCode": 0,
|
||||
"closed": false,
|
||||
"failure": null,
|
||||
"sandboxDenied": false,
|
||||
}
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
// Model foreground output delayed in transit beyond the local post-exit grace.
|
||||
tokio::time::sleep(Duration::from_millis(250)).await;
|
||||
send_exec_server_json(
|
||||
&mut websocket,
|
||||
json!({
|
||||
"method": "process/output",
|
||||
"params": {
|
||||
"processId": &process_id,
|
||||
"seq": 3,
|
||||
"stream": "stdout",
|
||||
"chunk": BASE64_STANDARD.encode("remote output\n"),
|
||||
}
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
send_exec_server_json(
|
||||
&mut websocket,
|
||||
json!({
|
||||
"method": "process/closed",
|
||||
"params": { "processId": &process_id, "seq": 4 }
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
PushedExecScenario::Complete
|
||||
| PushedExecScenario::DenyWin
|
||||
| PushedExecScenario::DenyWinUnchanged
|
||||
@@ -506,13 +580,16 @@ async fn serve_exec_with_pushed_events(
|
||||
PushedExecScenario::ReplayGap => {}
|
||||
}
|
||||
|
||||
let mut process_read_requests = 0;
|
||||
loop {
|
||||
let request = read_exec_server_json(&mut websocket, Duration::from_secs(/*secs*/ 5)).await;
|
||||
match request["method"].as_str() {
|
||||
Some("process/read") => {
|
||||
process_read_requests += 1;
|
||||
let result = match scenario {
|
||||
PushedExecScenario::OutputAfterExit
|
||||
| PushedExecScenario::OutputAfterRecoveredExit => {
|
||||
panic!("unexpected read after the delayed-output sequence")
|
||||
}
|
||||
PushedExecScenario::Complete
|
||||
| PushedExecScenario::CodexPath
|
||||
| PushedExecScenario::CodexPathDefaultFeature
|
||||
@@ -633,6 +710,8 @@ async fn serve_exec_with_pushed_events(
|
||||
}
|
||||
|
||||
#[test_case(PushedExecScenario::Complete, ManagedNetworkScenario::None, false ; "complete_event_stream")]
|
||||
#[test_case(PushedExecScenario::OutputAfterExit, ManagedNetworkScenario::None, false ; "output_after_exit")]
|
||||
#[test_case(PushedExecScenario::OutputAfterRecoveredExit, ManagedNetworkScenario::None, false ; "output_after_recovered_exit")]
|
||||
#[test_case(PushedExecScenario::CodexPath, ManagedNetworkScenario::None, false ; "codex_path_is_exported_before_nested_command")]
|
||||
#[test_case(PushedExecScenario::CodexPathDefaultFeature, ManagedNetworkScenario::None, false ; "codex_path_is_not_exported_by_default")]
|
||||
#[test_case(PushedExecScenario::CodexPathDisabledFeature, ManagedNetworkScenario::None, false ; "disabled_codex_path_preserves_original_login_command")]
|
||||
@@ -1368,6 +1447,22 @@ timeout = 900
|
||||
let output = output.context("exec_command output should contain text")?;
|
||||
let process_read_requests = exec_server_result.process_read_requests;
|
||||
match scenario {
|
||||
PushedExecScenario::OutputAfterExit | PushedExecScenario::OutputAfterRecoveredExit => {
|
||||
assert_ne!(success, Some(false));
|
||||
assert!(saw_exec_command_begin);
|
||||
assert!(output.contains("Process exited with code 0"));
|
||||
assert!(
|
||||
output.contains(COMPLETE_OUTPUT),
|
||||
"missing remote output tail: {output}"
|
||||
);
|
||||
assert_eq!(
|
||||
process_read_requests,
|
||||
usize::from(matches!(
|
||||
scenario,
|
||||
PushedExecScenario::OutputAfterRecoveredExit
|
||||
))
|
||||
);
|
||||
}
|
||||
PushedExecScenario::Complete
|
||||
| PushedExecScenario::DenyWin
|
||||
| PushedExecScenario::DenyWinUnchanged
|
||||
|
||||
Reference in New Issue
Block a user