mirror of
https://github.com/openai/codex.git
synced 2026-11-28 23:34:06 +00:00
Allow ConPTY output to close after the last console client exits (#45504)
## Why Retaining the pseudoconsole's creation pipe handles prevents output readers from seeing EOF while the session remains alive. ## What changed Drop the creation handles after a successful process spawn and call `ReleasePseudoConsole` when available on Windows 11 24H2 or newer. This lets output close after the last attached client exits while preserving I/O for surviving console descendants. Older Windows versions retain the `ClosePseudoConsole` cleanup path on drop. ## Testing Add Windows lifecycle tests for output closure after normal exit and termination while retaining the session, plus continued input and output for a surviving console child. These tests skip when `ReleasePseudoConsole` is unavailable. GitOrigin-RevId: fb1094fb2a570e6fec0cc80d6356f5d7eb1edcbf
This commit is contained in:
1 parent
973ec2942c
commit
6ce16aadce
4 files changed
+168
-9
No files matched your search
@@ -79,6 +79,10 @@ shared_library!(ConPtyFuncs,
|
||||
pub fn ClosePseudoConsole(hpc: HPCON),
|
||||
);
|
||||
|
||||
shared_library!(ConPtyReleaseFuncs,
|
||||
pub fn ReleasePseudoConsole(hpc: HPCON) -> HRESULT,
|
||||
);
|
||||
|
||||
shared_library!(Ntdll,
|
||||
pub fn RtlGetVersion(
|
||||
version_info: *mut OSVERSIONINFOW
|
||||
@@ -89,6 +93,8 @@ lazy_static! {
|
||||
static ref CONPTY: ConPtyFuncs = ConPtyFuncs::open(Path::new("kernel32.dll")).expect(
|
||||
"this system does not support conpty. Windows 10 October 2018 or newer is required",
|
||||
);
|
||||
static ref CONPTY_RELEASE: Option<ConPtyReleaseFuncs> =
|
||||
ConPtyReleaseFuncs::open(Path::new("kernel32.dll")).ok();
|
||||
}
|
||||
|
||||
pub fn conpty_supported() -> bool {
|
||||
@@ -109,10 +115,9 @@ fn windows_build_number() -> Option<u32> {
|
||||
|
||||
pub struct PsuedoCon {
|
||||
con: HPCON,
|
||||
// CreatePseudoConsole borrows these pipe handles for the lifetime of the
|
||||
// pseudoconsole, so we must keep owning them until ClosePseudoConsole.
|
||||
_input: FileDescriptor,
|
||||
_output: FileDescriptor,
|
||||
// Retain the creation handles until a client has attached. Keeping the
|
||||
// output write handle afterward would prevent the reader from seeing EOF.
|
||||
creation_handles: Option<(FileDescriptor, FileDescriptor)>,
|
||||
}
|
||||
|
||||
unsafe impl Send for PsuedoCon {}
|
||||
@@ -146,8 +151,7 @@ impl PsuedoCon {
|
||||
);
|
||||
Ok(Self {
|
||||
con,
|
||||
_input: input,
|
||||
_output: output,
|
||||
creation_handles: Some((input, output)),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -163,7 +167,7 @@ impl PsuedoCon {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn spawn_command(&self, cmd: CommandBuilder) -> anyhow::Result<WinChild> {
|
||||
pub fn spawn_command(&mut self, cmd: CommandBuilder) -> anyhow::Result<WinChild> {
|
||||
let job = Arc::new(JobObject::create()?);
|
||||
let mut si: STARTUPINFOEXW = unsafe { mem::zeroed() };
|
||||
si.StartupInfo.cb = mem::size_of::<STARTUPINFOEXW>() as u32;
|
||||
@@ -214,10 +218,25 @@ impl PsuedoCon {
|
||||
let _main_thread = unsafe { OwnedHandle::from_raw_handle(pi.hThread as _) };
|
||||
let proc = unsafe { OwnedHandle::from_raw_handle(pi.hProcess as _) };
|
||||
|
||||
self.creation_handles.take();
|
||||
// Windows 11 24H2+ lets the console close its output after the last
|
||||
// attached client exits, without terminating surviving descendants.
|
||||
// Older Windows retains its existing ClosePseudoConsole-on-drop path.
|
||||
if let Some(release) = CONPTY_RELEASE.as_ref() {
|
||||
let result = unsafe { (release.ReleasePseudoConsole)(self.con) };
|
||||
if result != S_OK {
|
||||
log::warn!("failed to release pseudoconsole ownership: HRESULT {result}");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(WinChild::new(proc, job))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "psuedocon_tests.rs"]
|
||||
mod lifecycle_tests;
|
||||
|
||||
fn resolve_current_directory(cmd: &CommandBuilder) -> Option<Vec<u16>> {
|
||||
let home = cmd
|
||||
.get_env("USERPROFILE")
|
||||
|
||||
Reference in new issue
Block a user