From 95af4179233e2b6f446c5d6ad20bdc5a2f876c98 Mon Sep 17 00:00:00 2001 From: Andrew Dirksen Date: Tue, 4 Nov 2025 17:54:46 -0800 Subject: [PATCH] allow codex to be run from pid 1 (#4200) Previously it was not possible for codex to run commands as the init process (pid 1) in linux. Commands run in containers tend to see their own pid as 1. See https://github.com/openai/codex/issues/4198 This pr implements the solution mentioned in that issue. Co-authored-by: Eric Traut --- codex-rs/core/src/spawn.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/codex-rs/core/src/spawn.rs b/codex-rs/core/src/spawn.rs index 1c82df31..c3a91266 100644 --- a/codex-rs/core/src/spawn.rs +++ b/codex-rs/core/src/spawn.rs @@ -67,7 +67,8 @@ pub(crate) async fn spawn_child_async( // This relies on prctl(2), so it only works on Linux. #[cfg(target_os = "linux")] unsafe { - cmd.pre_exec(|| { + let parent_pid = libc::getpid(); + cmd.pre_exec(move || { // This prctl call effectively requests, "deliver SIGTERM when my // current parent dies." if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM) == -1 { @@ -76,9 +77,10 @@ pub(crate) async fn spawn_child_async( // Though if there was a race condition and this pre_exec() block is // run _after_ the parent (i.e., the Codex process) has already - // exited, then the parent is the _init_ process (which will never - // die), so we should just terminate the child process now. - if libc::getppid() == 1 { + // exited, then parent will be the closest configured "subreaper" + // ancestor process, or PID 1 (init). If the Codex process has exited + // already, so should the child process. + if libc::getppid() != parent_pid { libc::raise(libc::SIGTERM); } Ok(())