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:
Colin Young
2026-08-03 20:00:49 +00:00
committed by copyberry
parent e4e0c7070e
commit 3149fa4b99
9 changed files with 337 additions and 18 deletions
+7 -18
View File
@@ -3,7 +3,6 @@ use std::collections::HashSet;
use std::ffi::OsStr;
use std::path::Path;
use std::path::PathBuf;
use std::process::Stdio;
use codex_file_system::ExecutorFileSystem;
use codex_file_system::FindUpErrorPolicy;
@@ -16,10 +15,10 @@ use serde::Deserialize;
use serde::Serialize;
use tokio::process::Command;
use tokio::time::Duration as TokioDuration;
use tokio::time::timeout;
use ts_rs::TS;
use crate::GitSha;
use crate::git_process::run_git_command_with_timeout_output;
/// Return `true` if the project folder specified by the `Config` is inside a
/// Git repository.
@@ -401,13 +400,9 @@ impl crate::FsmonitorProbeRunner for LocalFsmonitorProbeRunner<'_> {
// Both probes are fast, bounded metadata queries that do not inspect the
// worktree or index, so do not reduce the requested command's timeout.
let mut command = Command::new(self.git);
command
.args(args)
.current_dir(self.cwd)
.stdin(Stdio::null())
.kill_on_drop(true);
match timeout(GIT_COMMAND_TIMEOUT, command.output()).await {
Ok(Ok(output)) if output.status.success() => Some(output.stdout),
command.args(args).current_dir(self.cwd);
match run_git_command_with_timeout_output(&mut command, GIT_COMMAND_TIMEOUT).await {
Some(output) if output.status.success() => Some(output.stdout),
_ => None,
}
}
@@ -432,15 +427,8 @@ async fn run_git_command_with_timeout_from(
.args(["-c", &format!("core.hooksPath={DISABLED_HOOKS_PATH}")])
.args(["-c", fsmonitor.git_config_arg()])
.args(args)
.current_dir(cwd)
.stdin(Stdio::null())
.kill_on_drop(true);
let result = timeout(GIT_COMMAND_TIMEOUT, command.output()).await;
match result {
Ok(Ok(output)) => Some(output),
_ => None, // Timeout or error
}
.current_dir(cwd);
run_git_command_with_timeout_output(&mut command, GIT_COMMAND_TIMEOUT).await
}
async fn get_git_remotes(cwd: &Path) -> Option<Vec<String>> {
@@ -905,6 +893,7 @@ mod tests {
use pretty_assertions::assert_eq;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::process::Stdio;
#[tokio::test]
async fn git_metadata_commands_do_not_inherit_stdin() {