mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:59:25 +00:00
Terminate timed-out Git process trees (#36793)
## Why Timing out a Git metadata command must not leave helper processes running after the command wrapper exits. ## What changed - Run Git metadata commands in a dedicated process group on Unix and a Job Object on Windows so timeout cleanup terminates their full process trees. - Start Windows commands suspended, assign them to the Job Object, and then resume them so immediate descendants cannot escape containment. - Preserve descendants when a Git command completes normally, and retain the existing direct-spawn fallback if Windows Job Object setup fails. ## Testing Added cross-platform regression tests for cleanup both while the command wrapper is running and after it exits, plus Windows coverage for immediate-child Job Object containment. GitOrigin-RevId: 351851708e23ff06b89fe1894bd09a3558f67293
This commit is contained in:
1 parent
e4e0c7070e
commit
3149fa4b99
9 files changed
+337
-18
No files matched your search
@@ -6,8 +6,18 @@ use crate::TerminalSize;
|
||||
use crate::spawn_pipe_process_no_stdin;
|
||||
use crate::spawn_pty_process;
|
||||
use std::collections::HashMap;
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
use std::os::windows::io::FromRawHandle;
|
||||
use std::os::windows::io::OwnedHandle;
|
||||
use std::path::Path;
|
||||
use std::process::Stdio;
|
||||
use std::time::Duration;
|
||||
use tokio::io::AsyncBufReadExt;
|
||||
use tokio::io::BufReader;
|
||||
use tokio::process::Command;
|
||||
use winapi::um::jobapi::IsProcessInJob;
|
||||
use winapi::um::processthreadsapi::OpenProcess;
|
||||
use winapi::um::winnt::PROCESS_QUERY_LIMITED_INFORMATION;
|
||||
|
||||
const READY_MARKER: &str = "__CODEX_CHILD_READY__";
|
||||
const VALUE_MARKER: &str = "__CODEX_CHILD_VALUE__";
|
||||
@@ -183,6 +193,54 @@ async fn normal_exit_preserves_descendants_for_pipe_and_conpty() -> anyhow::Resu
|
||||
assert_normal_exit_preserves_descendant("ConPTY", &python, &env).await
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn contained_spawn_owns_immediate_descendant() -> anyhow::Result<()> {
|
||||
let Some(python) = find_python() else {
|
||||
eprintln!("python not found; skipping Windows contained-spawn test");
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let mut command = Command::new(&python);
|
||||
command
|
||||
.args([
|
||||
"-u",
|
||||
"-c",
|
||||
"import subprocess,sys; child=subprocess.Popen([sys.executable,'-c','import time; time.sleep(60)']); print(child.pid,flush=True); child.wait()",
|
||||
])
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null());
|
||||
|
||||
let job = crate::JobObject::create()?;
|
||||
let mut root = job.spawn_contained(&mut command)?;
|
||||
let stdout = root
|
||||
.stdout
|
||||
.take()
|
||||
.ok_or_else(|| anyhow::anyhow!("missing contained process stdout"))?;
|
||||
let mut stdout = BufReader::new(stdout);
|
||||
let mut child_pid = String::new();
|
||||
tokio::time::timeout(Duration::from_secs(10), stdout.read_line(&mut child_pid)).await??;
|
||||
let child_pid: u32 = child_pid.trim().parse()?;
|
||||
|
||||
let process = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, child_pid) };
|
||||
anyhow::ensure!(!process.is_null(), "failed to open immediate child process");
|
||||
let process = unsafe { OwnedHandle::from_raw_handle(process.cast()) };
|
||||
let mut in_job = 0;
|
||||
let checked = unsafe {
|
||||
IsProcessInJob(
|
||||
process.as_raw_handle().cast(),
|
||||
job.as_raw_handle().cast(),
|
||||
&mut in_job,
|
||||
)
|
||||
};
|
||||
anyhow::ensure!(checked != 0, "failed to inspect child Job Object");
|
||||
anyhow::ensure!(in_job != 0, "immediate child escaped its Job Object");
|
||||
|
||||
job.terminate()?;
|
||||
tokio::time::timeout(Duration::from_secs(10), root.wait()).await??;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn conpty_delivers_input_to_foreground_children() -> anyhow::Result<()> {
|
||||
let Some(python) = find_python() else {
|
||||
|
||||
Reference in new issue
Block a user