mirror of
https://github.com/openai/codex.git
synced 2026-11-28 23:34:06 +00:00
Terminate Windows process trees with job objects (#34624)
## Why Terminating a Windows execution session must also stop child processes, while a normal root-process exit must continue to allow background descendants to run. ## What changed - Assign Windows pipe, ConPTY, and sandbox processes to job objects and terminate the job when a session is cancelled, times out, or is explicitly stopped. - Preserve descendants when the root process exits normally. - Attach ConPTY and sandbox processes to their jobs atomically at creation; keep root-process termination as a fallback where job setup is unavailable. ## Testing Added Windows coverage for descendant termination and preservation across pipe, ConPTY, capture, cancellation, and legacy sandbox execution paths. GitOrigin-RevId: 8f831f2fc4caaa7b79ce842a3ed7192bd02dd3b4
This commit is contained in:
1 parent
ded4eacdbd
commit
9b33613db6
16 files changed
+946
-241
No files matched your search
@@ -19,12 +19,8 @@
|
||||
// SOFTWARE.
|
||||
|
||||
// Local modifications:
|
||||
// - Fix Codex bug #13945 in the Windows PTY kill path. The vendored code treated
|
||||
// `TerminateProcess`'s nonzero success return as failure and `0` as success,
|
||||
// which inverts kill outcomes for both `WinChild::do_kill` and
|
||||
// `WinChildKiller::kill`.
|
||||
// - This bug still exists in the original WezTerm source as of 2026-03-08, so
|
||||
// this is an intentional divergence from upstream.
|
||||
// - Place spawned processes in a Job Object so kill operations terminate the
|
||||
// full process tree, while normal root exit preserves background descendants.
|
||||
|
||||
use anyhow::Context as _;
|
||||
use filedescriptor::OwnedHandle;
|
||||
@@ -35,6 +31,7 @@ use std::io::Error as IoError;
|
||||
use std::io::Result as IoResult;
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::task::Context;
|
||||
use std::task::Poll;
|
||||
@@ -45,19 +42,29 @@ use winapi::um::synchapi::WaitForSingleObject;
|
||||
use winapi::um::winbase::INFINITE;
|
||||
|
||||
pub(crate) mod conpty;
|
||||
mod job;
|
||||
mod procthreadattr;
|
||||
mod psuedocon;
|
||||
|
||||
pub use conpty::ConPtySystem;
|
||||
pub use job::JobObject;
|
||||
pub use psuedocon::PsuedoCon;
|
||||
pub use psuedocon::conpty_supported;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WinChild {
|
||||
proc: Mutex<OwnedHandle>,
|
||||
job: Arc<JobObject>,
|
||||
}
|
||||
|
||||
impl WinChild {
|
||||
pub(crate) fn new(proc: OwnedHandle, job: Arc<JobObject>) -> Self {
|
||||
Self {
|
||||
proc: Mutex::new(proc),
|
||||
job,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_complete(&mut self) -> IoResult<Option<ExitStatus>> {
|
||||
let mut status: DWORD = 0;
|
||||
let proc = self.proc.lock().unwrap().try_clone().unwrap();
|
||||
@@ -66,6 +73,7 @@ impl WinChild {
|
||||
if status == STILL_ACTIVE {
|
||||
Ok(None)
|
||||
} else {
|
||||
self.preserve_descendants();
|
||||
Ok(Some(ExitStatus::with_exit_code(status)))
|
||||
}
|
||||
} else {
|
||||
@@ -74,48 +82,45 @@ impl WinChild {
|
||||
}
|
||||
|
||||
fn do_kill(&mut self) -> IoResult<()> {
|
||||
let proc = self.proc.lock().unwrap().try_clone().unwrap();
|
||||
let res = unsafe { TerminateProcess(proc.as_raw_handle() as _, 1) };
|
||||
// Codex bug #13945: Win32 returns nonzero on success, so only `0` is an error.
|
||||
if res == 0 {
|
||||
Err(IoError::last_os_error())
|
||||
} else {
|
||||
Ok(())
|
||||
self.job.terminate()
|
||||
}
|
||||
|
||||
fn preserve_descendants(&self) {
|
||||
if let Err(err) = self.job.preserve_descendants() {
|
||||
log::warn!("ConPTY failed to preserve descendants after root exit: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ChildKiller for WinChild {
|
||||
fn kill(&mut self) -> IoResult<()> {
|
||||
self.do_kill().ok();
|
||||
if let Err(err) = self.do_kill() {
|
||||
log::warn!("ConPTY failed to terminate process tree: {err}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn clone_killer(&self) -> Box<dyn ChildKiller + Send + Sync> {
|
||||
let proc = self.proc.lock().unwrap().try_clone().unwrap();
|
||||
Box::new(WinChildKiller { proc })
|
||||
Box::new(WinChildKiller {
|
||||
job: Arc::clone(&self.job),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WinChildKiller {
|
||||
proc: OwnedHandle,
|
||||
job: Arc<JobObject>,
|
||||
}
|
||||
|
||||
impl ChildKiller for WinChildKiller {
|
||||
fn kill(&mut self) -> IoResult<()> {
|
||||
let res = unsafe { TerminateProcess(self.proc.as_raw_handle() as _, 1) };
|
||||
// Codex bug #13945: Win32 returns nonzero on success, so only `0` is an error.
|
||||
if res == 0 {
|
||||
Err(IoError::last_os_error())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
self.job.terminate()
|
||||
}
|
||||
|
||||
fn clone_killer(&self) -> Box<dyn ChildKiller + Send + Sync> {
|
||||
let proc = self.proc.try_clone().unwrap();
|
||||
Box::new(WinChildKiller { proc })
|
||||
Box::new(WinChildKiller {
|
||||
job: Arc::clone(&self.job),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,6 +140,7 @@ impl Child for WinChild {
|
||||
let mut status: DWORD = 0;
|
||||
let res = unsafe { GetExitCodeProcess(proc.as_raw_handle() as _, &mut status) };
|
||||
if res != 0 {
|
||||
self.preserve_descendants();
|
||||
Ok(ExitStatus::with_exit_code(status))
|
||||
} else {
|
||||
Err(IoError::last_os_error())
|
||||
|
||||
Reference in new issue
Block a user