Prevent console windows for piped Windows child processes (#48483)

## Why

Piped child processes launched from a detached Windows process should not allocate a console window.

## What changed

Set `CREATE_NO_WINDOW` by default for `codex-rs/utils/pty` child commands. Preserve it alongside `CREATE_SUSPENDED` when preparing a child for Job Object containment, since Tokio's `creation_flags` replaces existing flags.

## Testing

Add a Windows regression test that runs from a detached process and verifies that children have no console window and retain working piped stdin and stdout, both with and without Job Object containment.

GitOrigin-RevId: 990386032d74fa783a66bb20faff769539ea41c1
(cherry picked from commit 1a89aec960)
This commit is contained in:
Felipe Coury
2026-09-26 15:49:13 -07:00
committed by Ian MacLeod
parent 5a7bb4dac9
commit bbb398d186
2 changed files with 77 additions and 0 deletions
+10
View File
@@ -4,6 +4,7 @@
//! change settings that the native backend cannot inspect. Children receive only
//! explicitly supplied environment variables and default to kill-on-drop. Stdio,
//! descriptor inheritance, and compatibility fallbacks are configured independently.
//! Windows children communicate over pipes and never allocate a console window.
//! Original Unix inputs retain their NUL validation even when std replaces them.
use std::ffi::OsStr;
@@ -12,6 +13,10 @@ use std::ffi::OsString;
use std::io;
use std::path::Path;
use std::process::Stdio as TokioStdio;
#[cfg(windows)]
use winapi::um::winbase::CREATE_NO_WINDOW;
#[cfg(windows)]
use winapi::um::winbase::CREATE_SUSPENDED;
use crate::child::Child;
use crate::child::ChildKind;
@@ -98,6 +103,8 @@ impl Command {
.stdin(TokioStdio::piped())
.stdout(TokioStdio::piped())
.stderr(TokioStdio::piped());
#[cfg(windows)]
inner.creation_flags(CREATE_NO_WINDOW);
Self {
inner,
#[cfg(unix)]
@@ -243,6 +250,9 @@ impl Command {
#[cfg(windows)]
pub fn prepare_suspended_spawn(&mut self, job: &crate::JobObject) {
job.prepare_suspended_spawn(&mut self.inner);
// Tokio's creation_flags replaces, rather than adds to, the flags.
self.inner
.creation_flags(CREATE_NO_WINDOW | CREATE_SUSPENDED);
}
/// Reject original inputs that std replaced with a NUL-free placeholder.
+67
View File
@@ -5,14 +5,17 @@ use super::wait_for_output_contains;
use crate::TerminalSize;
use crate::spawn_pipe_process_no_stdin;
use crate::spawn_pty_process;
use pretty_assertions::assert_eq;
use std::collections::HashMap;
use std::os::windows::io::AsRawHandle;
use std::os::windows::io::FromRawHandle;
use std::os::windows::io::OwnedHandle;
use std::os::windows::process::CommandExt;
use std::path::Path;
use std::process::Stdio;
use std::time::Duration;
use tokio::io::AsyncBufReadExt;
use tokio::io::AsyncWriteExt;
use tokio::io::BufReader;
use tokio::process::Command;
use winapi::um::jobapi::IsProcessInJob;
@@ -22,6 +25,70 @@ use winapi::um::winnt::PROCESS_QUERY_LIMITED_INFORMATION;
const READY_MARKER: &str = "__CODEX_CHILD_READY__";
const VALUE_MARKER: &str = "__CODEX_CHILD_VALUE__";
#[tokio::test]
async fn piped_child_has_no_console_with_or_without_job_containment() -> anyhow::Result<()> {
// Nextest itself can run without a visible console. Re-execute this test
// detached, like app-server-daemon, so children cannot inherit its console.
if std::env::var_os("CODEX_PTY_DETACHED_CONSOLE_PROBE").is_none() {
let output = std::process::Command::new(std::env::current_exe()?)
.args([
"--exact",
"tests::windows_tests::piped_child_has_no_console_with_or_without_job_containment",
"--nocapture",
])
.env("CODEX_PTY_DETACHED_CONSOLE_PROBE", "1")
.creation_flags(winapi::um::winbase::DETACHED_PROCESS)
.output()?;
anyhow::ensure!(
output.status.success(),
"detached probe failed:\n{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
return Ok(());
}
let Some(python) = find_python() else {
eprintln!("python not found; skipping Windows pipe console test");
return Ok(());
};
let job = crate::JobObject::create_without_breakaway()?;
for containment in [None, Some(&job)] {
let mut command = crate::Command::new(&python);
command.envs(std::env::vars()).args([
"-u",
"-c",
"import ctypes,sys; print(ctypes.windll.kernel32.GetConsoleWindow()); print(sys.stdin.readline().strip())",
]);
if let Some(job) = containment {
command.prepare_suspended_spawn(job);
}
let mut child = command.spawn()?;
if let Some(job) = containment {
let pid = child
.id()
.ok_or_else(|| anyhow::anyhow!("missing child pid"))?;
anyhow::ensure!(job.assign_and_resume_process(pid)?, "job assignment failed");
}
child
.stdin
.as_mut()
.unwrap()
.write_all(b"stdio works\n")
.await?;
let output =
tokio::time::timeout(Duration::from_secs(10), child.wait_with_output()).await??;
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(
String::from_utf8(output.stdout)?.replace("\r\n", "\n"),
"0\nstdio works\n"
);
}
Ok(())
}
struct WindowsShell {
name: &'static str,
program: String,