mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
Publish process exit status and exit code together (#63896)
## Why Separate reads and writes of the exit flag and exit code could let unified exec observe a process as exited before seeing its exit code, or return a live session ID alongside an exit code. ## What changed Store the exit code in a shared `OnceLock` for pipe, PTY, and driver-backed processes. Derive `has_exited()` from the presence of that code. Unified exec reads the code when it retires the session, after any pending output it needs to retain has been drained. ## Testing Extend the pipe, PTY, and driver-backed tests to check that the process handle exposes the exit code reported by the exit notification. The driver-backed test also verifies that output arriving after the notification is still collected. GitOrigin-RevId: 6192e5ceb9c71c6321e76f750c531c0ea6554dca
This commit is contained in:
@@ -713,11 +713,7 @@ impl UnifiedExecProcessManager {
|
||||
.refresh_process_state(process_id, &process, retention)
|
||||
.await
|
||||
{
|
||||
ProcessStatus::Alive {
|
||||
exit_code,
|
||||
process_id,
|
||||
..
|
||||
} => (Some(process_id), exit_code),
|
||||
ProcessStatus::Alive { process_id, .. } => (Some(process_id), None),
|
||||
ProcessStatus::Exited { exit_code, entry } => {
|
||||
entry.process.cancellation_token().cancel();
|
||||
if let Err(message) = finish_deferred_network_approval_after_process_exit(
|
||||
@@ -1054,10 +1050,9 @@ impl UnifiedExecProcessManager {
|
||||
.await;
|
||||
let (process_id, exit_code, event_call_id) = match status {
|
||||
ProcessStatus::Alive {
|
||||
exit_code,
|
||||
call_id,
|
||||
process_id,
|
||||
} => (Some(process_id), exit_code, call_id),
|
||||
} => (Some(process_id), None, call_id),
|
||||
ProcessStatus::Exited { exit_code, entry } => {
|
||||
entry.process.cancellation_token().cancel();
|
||||
let call_id = entry.call_id.clone();
|
||||
@@ -1126,7 +1121,6 @@ impl UnifiedExecProcessManager {
|
||||
return ProcessStatus::Unknown;
|
||||
};
|
||||
|
||||
let exit_code = entry.process.exit_code();
|
||||
let process_id = entry.process_id;
|
||||
|
||||
if entry.process.has_exited() {
|
||||
@@ -1143,7 +1137,6 @@ impl UnifiedExecProcessManager {
|
||||
.map_or(true, |buffer| buffer.pending.total_bytes() > 0))
|
||||
{
|
||||
return ProcessStatus::Alive {
|
||||
exit_code: None,
|
||||
call_id: entry.call_id.clone(),
|
||||
process_id,
|
||||
};
|
||||
@@ -1152,12 +1145,11 @@ impl UnifiedExecProcessManager {
|
||||
return ProcessStatus::Unknown;
|
||||
};
|
||||
ProcessStatus::Exited {
|
||||
exit_code,
|
||||
exit_code: entry.process.exit_code(),
|
||||
entry: Box::new(entry),
|
||||
}
|
||||
} else {
|
||||
ProcessStatus::Alive {
|
||||
exit_code,
|
||||
call_id: entry.call_id.clone(),
|
||||
process_id,
|
||||
}
|
||||
@@ -1818,7 +1810,6 @@ enum OutputRetention {
|
||||
|
||||
enum ProcessStatus {
|
||||
Alive {
|
||||
exit_code: Option<i32>,
|
||||
call_id: String,
|
||||
process_id: i32,
|
||||
},
|
||||
|
||||
@@ -6,8 +6,7 @@ use std::io;
|
||||
use std::io::ErrorKind;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex as StdMutex;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use crate::ChildStdin;
|
||||
use crate::Command;
|
||||
@@ -244,9 +243,7 @@ async fn spawn_process_with_stdin_mode(
|
||||
});
|
||||
|
||||
let (exit_tx, exit_rx) = oneshot::channel::<i32>();
|
||||
let exit_status = Arc::new(AtomicBool::new(false));
|
||||
let wait_exit_status = Arc::clone(&exit_status);
|
||||
let exit_code = Arc::new(StdMutex::new(None));
|
||||
let exit_code = Arc::new(OnceLock::new());
|
||||
let wait_exit_code = Arc::clone(&exit_code);
|
||||
#[cfg(windows)]
|
||||
let wait_job = match &windows_terminator {
|
||||
@@ -268,10 +265,7 @@ async fn spawn_process_with_stdin_mode(
|
||||
}
|
||||
Err(_) => -1,
|
||||
};
|
||||
wait_exit_status.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
if let Ok(mut guard) = wait_exit_code.lock() {
|
||||
*guard = Some(code);
|
||||
}
|
||||
let _ = wait_exit_code.set(code);
|
||||
let _ = exit_tx.send(code);
|
||||
});
|
||||
|
||||
@@ -287,7 +281,6 @@ async fn spawn_process_with_stdin_mode(
|
||||
stderr_handle,
|
||||
writer_handle,
|
||||
wait_handle,
|
||||
exit_status,
|
||||
exit_code,
|
||||
/*pty_handles*/ None,
|
||||
/*resizer*/ None,
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::os::fd::RawFd;
|
||||
use std::process::ExitStatus;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex as StdMutex;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use portable_pty::MasterPty;
|
||||
@@ -115,8 +115,7 @@ pub struct ProcessHandle {
|
||||
stderr_reader: Option<JoinHandle<()>>,
|
||||
writer_handle: StdMutex<Option<JoinHandle<()>>>,
|
||||
wait_handle: StdMutex<Option<JoinHandle<()>>>,
|
||||
exit_status: Arc<AtomicBool>,
|
||||
exit_code: Arc<StdMutex<Option<i32>>>,
|
||||
exit_code: Arc<OnceLock<i32>>,
|
||||
// PtyHandles must be preserved because the process will receive Control+C if the
|
||||
// slave is closed
|
||||
_pty_handles: StdMutex<Option<PtyHandles>>,
|
||||
@@ -140,8 +139,7 @@ impl ProcessHandle {
|
||||
stderr_reader: Option<JoinHandle<()>>,
|
||||
writer_handle: JoinHandle<()>,
|
||||
wait_handle: JoinHandle<()>,
|
||||
exit_status: Arc<AtomicBool>,
|
||||
exit_code: Arc<StdMutex<Option<i32>>>,
|
||||
exit_code: Arc<OnceLock<i32>>,
|
||||
pty_handles: Option<PtyHandles>,
|
||||
resizer: Option<ResizeFn>,
|
||||
) -> Self {
|
||||
@@ -152,7 +150,6 @@ impl ProcessHandle {
|
||||
stderr_reader,
|
||||
writer_handle: StdMutex::new(Some(writer_handle)),
|
||||
wait_handle: StdMutex::new(Some(wait_handle)),
|
||||
exit_status,
|
||||
exit_code,
|
||||
_pty_handles: StdMutex::new(pty_handles),
|
||||
resizer: StdMutex::new(resizer),
|
||||
@@ -180,14 +177,14 @@ impl ProcessHandle {
|
||||
writer_tx
|
||||
}
|
||||
|
||||
/// True if the child process has exited.
|
||||
/// Once this returns true, the exit code is available.
|
||||
pub fn has_exited(&self) -> bool {
|
||||
self.exit_status.load(std::sync::atomic::Ordering::SeqCst)
|
||||
self.exit_code.get().is_some()
|
||||
}
|
||||
|
||||
/// Returns the exit code if known.
|
||||
pub fn exit_code(&self) -> Option<i32> {
|
||||
self.exit_code.lock().ok().and_then(|guard| *guard)
|
||||
self.exit_code.get().copied()
|
||||
}
|
||||
|
||||
/// Resize the PTY in character cells.
|
||||
@@ -442,16 +439,11 @@ pub fn spawn_from_driver(driver: ProcessDriver) -> SpawnedProcess {
|
||||
let writer_handle = writer_handle.unwrap_or_else(|| tokio::spawn(async {}));
|
||||
|
||||
let (exit_tx, exit_rx_out) = oneshot::channel::<i32>();
|
||||
let exit_status = Arc::new(AtomicBool::new(false));
|
||||
let wait_exit_status = Arc::clone(&exit_status);
|
||||
let exit_code = Arc::new(StdMutex::new(None));
|
||||
let exit_code = Arc::new(OnceLock::new());
|
||||
let wait_exit_code = Arc::clone(&exit_code);
|
||||
let wait_handle = tokio::spawn(async move {
|
||||
let code = exit_rx.await.unwrap_or(-1);
|
||||
wait_exit_status.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
if let Ok(mut guard) = wait_exit_code.lock() {
|
||||
*guard = Some(code);
|
||||
}
|
||||
let _ = wait_exit_code.set(code);
|
||||
let _ = exit_seen_tx.send(true);
|
||||
let _ = exit_tx.send(code);
|
||||
});
|
||||
@@ -467,7 +459,6 @@ pub fn spawn_from_driver(driver: ProcessDriver) -> SpawnedProcess {
|
||||
stderr_reader_handle,
|
||||
writer_handle,
|
||||
wait_handle,
|
||||
exit_status,
|
||||
exit_code,
|
||||
/*pty_handles*/ None,
|
||||
resizer,
|
||||
|
||||
@@ -19,8 +19,7 @@ use std::process::Command as StdCommand;
|
||||
#[cfg(all(unix, not(target_os = "linux")))]
|
||||
use std::process::Stdio;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex as StdMutex;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::OnceLock;
|
||||
#[cfg(not(unix))]
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -278,19 +277,14 @@ async fn spawn_process_portable(
|
||||
};
|
||||
|
||||
let (exit_tx, exit_rx) = oneshot::channel::<i32>();
|
||||
let exit_status = Arc::new(AtomicBool::new(false));
|
||||
let wait_exit_status = Arc::clone(&exit_status);
|
||||
let exit_code = Arc::new(StdMutex::new(None));
|
||||
let exit_code = Arc::new(OnceLock::new());
|
||||
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.exit_code() as i32,
|
||||
Err(_) => -1,
|
||||
};
|
||||
wait_exit_status.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
if let Ok(mut guard) = wait_exit_code.lock() {
|
||||
*guard = Some(code);
|
||||
}
|
||||
let _ = wait_exit_code.set(code);
|
||||
let _ = exit_tx.send(code);
|
||||
});
|
||||
|
||||
@@ -314,7 +308,6 @@ async fn spawn_process_portable(
|
||||
/*stderr_reader*/ None,
|
||||
writer_handle,
|
||||
wait_handle,
|
||||
exit_status,
|
||||
exit_code,
|
||||
Some(handles),
|
||||
/*resizer*/ None,
|
||||
@@ -485,9 +478,7 @@ async fn spawn_process_preserving_fds(
|
||||
let (reader_handle, writer_handle) = io.spawn(stdout_tx, writer_rx, stdin_close_behavior);
|
||||
|
||||
let (exit_tx, exit_rx) = oneshot::channel::<i32>();
|
||||
let exit_status = Arc::new(AtomicBool::new(false));
|
||||
let wait_exit_status = Arc::clone(&exit_status);
|
||||
let exit_code = Arc::new(StdMutex::new(None));
|
||||
let exit_code = Arc::new(OnceLock::new());
|
||||
let wait_exit_code = Arc::clone(&exit_code);
|
||||
let on_exit = move |status: std::io::Result<std::process::ExitStatus>| {
|
||||
let code = match status {
|
||||
@@ -497,10 +488,7 @@ async fn spawn_process_preserving_fds(
|
||||
Ok(status) => exit_code_from_status(status),
|
||||
Err(_) => -1,
|
||||
};
|
||||
wait_exit_status.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
if let Ok(mut guard) = wait_exit_code.lock() {
|
||||
*guard = Some(code);
|
||||
}
|
||||
let _ = wait_exit_code.set(code);
|
||||
let _ = exit_tx.send(code);
|
||||
};
|
||||
#[cfg(target_os = "linux")]
|
||||
@@ -523,7 +511,6 @@ async fn spawn_process_preserving_fds(
|
||||
/*stderr_reader*/ None,
|
||||
writer_handle,
|
||||
wait_handle,
|
||||
exit_status,
|
||||
exit_code,
|
||||
Some(handles),
|
||||
/*resizer*/ None,
|
||||
|
||||
@@ -511,8 +511,8 @@ async fn pipe_and_pty_share_interface() -> anyhow::Result<()> {
|
||||
crate::ChildFds::Inherited(&[]),
|
||||
)
|
||||
.await?;
|
||||
let (_pipe_session, pipe_output_rx, pipe_exit_rx) = combine_spawned_output(pipe);
|
||||
let (_pty_session, pty_output_rx, pty_exit_rx) = combine_spawned_output(pty);
|
||||
let (pipe_session, pipe_output_rx, pipe_exit_rx) = combine_spawned_output(pipe);
|
||||
let (pty_session, pty_output_rx, pty_exit_rx) = combine_spawned_output(pty);
|
||||
|
||||
let timeout_ms = if cfg!(windows) { 10_000 } else { 3_000 };
|
||||
let (pipe_out, pipe_code) =
|
||||
@@ -520,8 +520,11 @@ async fn pipe_and_pty_share_interface() -> anyhow::Result<()> {
|
||||
let (pty_out, pty_code) =
|
||||
collect_output_until_exit(pty_output_rx, pty_exit_rx, timeout_ms).await;
|
||||
|
||||
assert_eq!(pipe_code, 0);
|
||||
assert_eq!(pty_code, 0);
|
||||
for (session, code) in [(&pipe_session, pipe_code), (&pty_session, pty_code)] {
|
||||
assert_eq!(code, 0);
|
||||
assert!(session.has_exited());
|
||||
assert_eq!(session.exit_code(), Some(code));
|
||||
}
|
||||
assert!(
|
||||
String::from_utf8_lossy(&pipe_out).contains("pipe_ok"),
|
||||
"pipe output mismatch: {pipe_out:?}"
|
||||
@@ -764,29 +767,32 @@ async fn driver_backed_process_drains_output_that_arrives_after_exit_signal() ->
|
||||
});
|
||||
|
||||
let SpawnedProcess {
|
||||
session: _session,
|
||||
session,
|
||||
stdout_rx,
|
||||
stderr_rx: _stderr_rx,
|
||||
exit_rx,
|
||||
} = spawned;
|
||||
let stdout_task = tokio::spawn(async move { collect_split_output(stdout_rx).await });
|
||||
|
||||
exit_tx.send(0).expect("send exit code");
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
|
||||
stdout_tx.send(b"tail".to_vec())?;
|
||||
drop(stdout_tx);
|
||||
|
||||
assert_eq!((session.has_exited(), session.exit_code()), (false, None));
|
||||
exit_tx.send(17).expect("send exit code");
|
||||
let timeout = tokio::time::Duration::from_secs(2);
|
||||
let code = tokio::time::timeout(timeout, exit_rx)
|
||||
.await
|
||||
.map_err(|_| anyhow::anyhow!("timed out waiting for driver exit"))?
|
||||
.unwrap_or(-1);
|
||||
.map_err(|_| anyhow::anyhow!("timed out waiting for driver exit"))??;
|
||||
assert_eq!(code, 17);
|
||||
assert_eq!(
|
||||
(session.has_exited(), session.exit_code()),
|
||||
(true, Some(code))
|
||||
);
|
||||
|
||||
stdout_tx.send(b"tail".to_vec())?;
|
||||
drop(stdout_tx);
|
||||
let stdout = tokio::time::timeout(timeout, stdout_task)
|
||||
.await
|
||||
.map_err(|_| anyhow::anyhow!("timed out waiting to drain driver stdout"))??;
|
||||
|
||||
assert_eq!(stdout, b"tail".to_vec());
|
||||
assert_eq!(code, 0);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user