Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Treat zombie processes as inactive in the Unix PID backend (#43504)

Eric Traut6750f5bd1356
diff --git a/codex-rs/app-server-daemon/src/backend/pid.rs b/codex-rs/app-server-daemon/src/backend/pid.rs
index 0b2b32cdff..e34e170880 100644
--- a/codex-rs/app-server-daemon/src/backend/pid.rs
+++ b/codex-rs/app-server-daemon/src/backend/pid.rs
@@ -503,8 +503,21 @@ async fn process_matches_record(record: &PidRecord) -> Result<bool> {
return Ok(false);
}
- match read_process_start_time(record.pid).await {
- Ok(start_time) => Ok(start_time == record.process_start_time),
+ match read_process_details(record.pid).await {
+ Ok((state, start_time)) => {
+ // An unreaped zombie still passes kill(pid, 0) and retains its start
+ // time, but it can no longer run the app-server or updater.
+ if state.starts_with('Z') {
+ if start_time == record.process_start_time
+ && let Ok(raw_pid) = libc::pid_t::try_from(record.pid)
+ {
+ // Re-exec can lose the Child handle without changing parenthood.
+ unsafe { libc::waitpid(raw_pid, std::ptr::null_mut(), libc::WNOHANG) };
+ }
+ return Ok(false);
+ }
+ Ok(start_time == record.process_start_time)
+ }
Err(_err) if !process_exists(record.pid) => Ok(false),
Err(err) => Err(err),
}
@@ -623,8 +636,13 @@ async fn inspect_empty_pid_reservation(
#[cfg(unix)]
async fn read_process_start_time(pid: u32) -> Result<String> {
+ Ok(read_process_details(pid).await?.1)
+}
+
+#[cfg(unix)]
+async fn read_process_details(pid: u32) -> Result<(String, String)> {
let output = Command::new("ps")
- .args(["-p", &pid.to_string(), "-o", "lstart="])
+ .args(["-p", &pid.to_string(), "-o", "stat=", "-o", "lstart="])
.output()
.await
.context("failed to invoke ps for pid-managed app server")?;
@@ -632,13 +650,16 @@ async fn read_process_start_time(pid: u32) -> Result<String> {
bail!("failed to read start time for pid-managed app server {pid}");
}
- let start_time = String::from_utf8(output.stdout)
- .context("pid-managed app server start time was not utf-8")?;
+ let details = String::from_utf8(output.stdout)
+ .context("pid-managed app server process details were not utf-8")?;
+ let Some((state, start_time)) = details.trim().split_once(char::is_whitespace) else {
+ bail!("pid-managed app server {pid} has no recorded start time");
+ };
let start_time = start_time.trim();
if start_time.is_empty() {
bail!("pid-managed app server {pid} has no recorded start time");
}
- Ok(start_time.to_string())
+ Ok((state.to_string(), start_time.to_string()))
}
#[cfg(all(test, any(unix, windows)))]
diff --git a/codex-rs/app-server-daemon/src/backend/pid_tests.rs b/codex-rs/app-server-daemon/src/backend/pid_tests.rs
index 0fbf843e7e..ac417232cb 100644
--- a/codex-rs/app-server-daemon/src/backend/pid_tests.rs
+++ b/codex-rs/app-server-daemon/src/backend/pid_tests.rs
@@ -226,6 +226,41 @@ async fn stop_reaps_untracked_app_server_child() {
assert!(!pid_file.exists());
}
+#[cfg(unix)]
+#[tokio::test]
+async fn exited_unreaped_updater_is_reaped() {
+ let temp = TempDir::new().expect("temp dir");
+ let mut child = std::process::Command::new("sleep")
+ .arg("60")
+ .spawn()
+ .expect("spawn updater shim");
+ let record = PidRecord {
+ pid: child.id(),
+ process_start_time: read_process_start_time(child.id())
+ .await
+ .expect("start time"),
+ };
+ let backend =
+ PidBackend::new_update_loop(temp.path().join("codex"), temp.path().join("updater.pid"));
+ child.kill().expect("terminate updater shim");
+ let deadline = tokio::time::Instant::now() + Duration::from_secs(5);
+ let result = loop {
+ let result = backend.record_is_active(&record).await;
+ if matches!(&result, Ok(false)) || tokio::time::Instant::now() >= deadline {
+ break result;
+ }
+ tokio::time::sleep(Duration::from_millis(50)).await;
+ };
+ assert!(!result.expect("check zombie updater"));
+ assert_eq!(
+ child
+ .wait()
+ .expect_err("updater shim was already reaped")
+ .raw_os_error(),
+ Some(libc::ECHILD)
+ );
+}
+
#[test]
fn update_loop_uses_hidden_app_server_subcommand() {
let backend = PidBackend {