Skip to content

Commit c6ce82a

Browse files
bddapetraut-openai
andauthored
allow codex to be run from pid 1 (openai#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 openai#4198 This pr implements the solution mentioned in that issue. Co-authored-by: Eric Traut <[email protected]>
1 parent f604507 commit c6ce82a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

codex-rs/core/src/spawn.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ pub(crate) async fn spawn_child_async(
6767
// This relies on prctl(2), so it only works on Linux.
6868
#[cfg(target_os = "linux")]
6969
unsafe {
70-
cmd.pre_exec(|| {
70+
let parent_pid = libc::getpid();
71+
cmd.pre_exec(move || {
7172
// This prctl call effectively requests, "deliver SIGTERM when my
7273
// current parent dies."
7374
if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM) == -1 {
@@ -76,9 +77,10 @@ pub(crate) async fn spawn_child_async(
7677

7778
// Though if there was a race condition and this pre_exec() block is
7879
// run _after_ the parent (i.e., the Codex process) has already
79-
// exited, then the parent is the _init_ process (which will never
80-
// die), so we should just terminate the child process now.
81-
if libc::getppid() == 1 {
80+
// exited, then parent will be the closest configured "subreaper"
81+
// ancestor process, or PID 1 (init). If the Codex process has exited
82+
// already, so should the child process.
83+
if libc::getppid() != parent_pid {
8284
libc::raise(libc::SIGTERM);
8385
}
8486
Ok(())

0 commit comments

Comments
 (0)