Prevent Unix PTY I/O from blocking runtime shutdown (#40460)

## Why

Blocking PTY reads and output-channel sends can keep Tokio runtime shutdown
waiting when a detached child retains the terminal or output backpressure fills
the channel.

## What changed

- Drive Unix PTY reads and writes through nonblocking `AsyncFd` readiness so
  their tasks can be cancelled during shutdown.
- Keep draining child output after its receiver closes, and preserve queued
  input plus EOF delivery when portable PTY stdin closes.
- Return a descriptive error when PTY spawning uses a Tokio runtime without an
  I/O driver.

## Testing

Add Unix coverage for detached children, full and dropped output channels,
large input with EOF, inherited file descriptors, and runtimes without I/O.

GitOrigin-RevId: f7f1f58abebf8bf1d39285cd61b8d69946d54155
This commit is contained in:
viyatb-oai authored and copyberry committed 2026-08-24 19:25:32 +00:00
1 parent 4aa52f2cb2
commit bf3eb2ec91
5 files changed
+477 -65

No files matched your search

+308
View File
@@ -873,6 +873,314 @@ async fn pipe_drop_reaps_child() -> anyhow::Result<()> {
Ok(())
}
#[cfg(unix)]
enum PtyShutdown {
Terminate,
Drop,
}
#[cfg(unix)]
fn assert_pty_shutdown_with_detached_child(
inherited_fds: &[i32],
shutdown: PtyShutdown,
) -> anyhow::Result<()> {
let Some(python) = find_python() else {
eprintln!("python not found; skipping detached PTY shutdown test");
return Ok(());
};
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
let (session, child_pid) = runtime.block_on(async {
let marker = "__codex_detached_pid:";
// The detached child retains the slave descriptors but outlives the
// original process group. Its alarm bounds a regressed runtime drop.
let script = format!(
r"import os, select, signal, time, tty
signal.alarm(10)
if os.fork() == 0:
os.setsid()
signal.signal(signal.SIGHUP, signal.SIG_IGN)
signal.alarm(10)
tty.setraw(0)
print('{marker}' + str(os.getpid()), flush=True)
select.select([0], [], [])
print('__codex_input_ready__', flush=True)
time.sleep(10)
os._exit(0)
time.sleep(10)
"
);
let env_map: HashMap<String, String> = std::env::vars().collect();
let spawned = spawn_pty_process(
&python,
&["-c".to_string(), script],
Path::new("."),
&env_map,
&None,
TerminalSize::default(),
inherited_fds,
)
.await?;
let (session, mut output_rx, exit_rx) = combine_spawned_output(spawned);
let child_pid = wait_for_marker_pid(&mut output_rx, marker, /*timeout_ms*/ 2_000).await?;
let writer = session.writer_sender();
writer.send(vec![b'x'; 1024 * 1024]).await?;
writer.send(b"pending".to_vec()).await?;
// The slave reports readable input without consuming it; the first write
// exceeds terminal capacity, so the second chunk must remain queued.
wait_for_output_contains(
&mut output_rx,
"__codex_input_ready__",
/*timeout_ms*/ 2_000,
)
.await?;
assert_eq!(writer.capacity(), writer.max_capacity() - 1);
drop(writer);
let session = match shutdown {
PtyShutdown::Terminate => {
session.terminate();
Some(session)
}
PtyShutdown::Drop => {
drop(session);
None
}
};
tokio::time::timeout(std::time::Duration::from_secs(2), exit_rx).await??;
Ok::<_, anyhow::Error>((session, child_pid))
})?;
let started = std::time::Instant::now();
drop(runtime);
let elapsed = started.elapsed();
let detached_child_alive = process_exists(child_pid)?;
let _ = unsafe { libc::kill(child_pid, libc::SIGKILL) };
drop(session);
assert!(
elapsed < std::time::Duration::from_secs(2),
"runtime shutdown waited for detached PTY child: {elapsed:?}"
);
assert!(
detached_child_alive,
"detached child exited before runtime shutdown"
);
Ok(())
}
#[cfg(unix)]
#[test]
fn pty_spawn_without_io_driver_returns_error() -> anyhow::Result<()> {
use std::os::fd::AsRawFd;
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()?;
let preserved_fd = std::fs::File::open("/dev/null")?;
let env_map: HashMap<String, String> = std::env::vars().collect();
let (program, args) = shell_command("true");
for inherited_fds in [&[][..], &[preserved_fd.as_raw_fd()][..]] {
let Err(error) = runtime.block_on(spawn_pty_process(
&program,
&args,
Path::new("."),
&env_map,
&None,
TerminalSize::default(),
inherited_fds,
)) else {
anyhow::bail!("PTY spawn succeeded without a Tokio I/O driver");
};
assert_eq!(
error.to_string(),
"PTY I/O requires a Tokio runtime with I/O enabled"
);
}
Ok(())
}
#[cfg(unix)]
#[test]
fn pty_terminate_allows_runtime_shutdown_with_detached_child() -> anyhow::Result<()> {
assert_pty_shutdown_with_detached_child(&[], PtyShutdown::Terminate)
}
#[cfg(unix)]
#[test]
fn pty_drop_allows_runtime_shutdown_with_detached_child() -> anyhow::Result<()> {
assert_pty_shutdown_with_detached_child(&[], PtyShutdown::Drop)
}
#[cfg(unix)]
#[test]
fn pty_preserving_fds_terminate_allows_runtime_shutdown_with_detached_child() -> anyhow::Result<()>
{
use std::os::fd::AsRawFd;
let preserved_fd = std::fs::File::open("/dev/null")?;
assert_pty_shutdown_with_detached_child(&[preserved_fd.as_raw_fd()], PtyShutdown::Terminate)
}
#[cfg(unix)]
#[test]
fn pty_preserving_fds_drop_allows_runtime_shutdown_with_detached_child() -> anyhow::Result<()> {
use std::os::fd::AsRawFd;
let preserved_fd = std::fs::File::open("/dev/null")?;
assert_pty_shutdown_with_detached_child(&[preserved_fd.as_raw_fd()], PtyShutdown::Drop)
}
#[cfg(unix)]
#[test]
fn pty_terminate_allows_runtime_shutdown_with_full_output_channel() -> anyhow::Result<()> {
use std::os::fd::AsRawFd;
let Some(python) = find_python() else {
eprintln!("python not found; skipping PTY output backpressure test");
return Ok(());
};
let preserved_fd = std::fs::File::open("/dev/null")?;
for inherited_fds in [&[][..], &[preserved_fd.as_raw_fd()][..]] {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
let (session, stdout_rx) = runtime.block_on(async {
let env_map: HashMap<String, String> = std::env::vars().collect();
let script =
"import os, signal\nsignal.alarm(10)\nwhile True:\n os.write(1, b'x' * 8192)\n";
let SpawnedProcess {
session,
stdout_rx,
exit_rx,
..
} = spawn_pty_process(
&python,
&["-c".to_string(), script.to_string()],
Path::new("."),
&env_map,
&None,
TerminalSize::default(),
inherited_fds,
)
.await?;
tokio::time::timeout(std::time::Duration::from_secs(2), async {
while stdout_rx.len() < stdout_rx.max_capacity() {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
})
.await?;
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
session.terminate();
tokio::time::timeout(std::time::Duration::from_secs(2), exit_rx).await??;
Ok::<_, anyhow::Error>((session, stdout_rx))
})?;
// Keep the receiver alive through runtime shutdown. An OS-thread
// watchdog releases a regressed blocking_send without a Tokio timer.
let (shutdown_tx, shutdown_rx) = std::sync::mpsc::channel();
let receiver_guard = std::thread::spawn(move || {
let _ = shutdown_rx.recv_timeout(std::time::Duration::from_secs(3));
drop(stdout_rx);
});
let started = std::time::Instant::now();
drop(runtime);
let elapsed = started.elapsed();
let _ = shutdown_tx.send(());
receiver_guard
.join()
.map_err(|_| anyhow::anyhow!("output receiver watchdog panicked"))?;
drop(session);
assert!(
elapsed < std::time::Duration::from_secs(2),
"runtime shutdown waited on a full PTY output channel: {elapsed:?}"
);
}
Ok(())
}
#[cfg(unix)]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn pty_dropped_output_receiver_keeps_draining_child() -> anyhow::Result<()> {
use std::os::fd::AsRawFd;
let Some(python) = find_python() else {
eprintln!("python not found; skipping PTY dropped output receiver test");
return Ok(());
};
let preserved_fd = std::fs::File::open("/dev/null")?;
let env_map: HashMap<String, String> = std::env::vars().collect();
let script =
"import signal, sys\nsignal.alarm(10)\nsys.stdout.buffer.write(b'x' * (4 * 1024 * 1024))";
for inherited_fds in [&[][..], &[preserved_fd.as_raw_fd()][..]] {
let SpawnedProcess {
session: _session,
stdout_rx,
exit_rx,
..
} = spawn_pty_process(
&python,
&["-c".to_string(), script.to_string()],
Path::new("."),
&env_map,
&None,
TerminalSize::default(),
inherited_fds,
)
.await?;
drop(stdout_rx);
let code = tokio::time::timeout(std::time::Duration::from_secs(2), exit_rx).await??;
assert_eq!(code, 0, "child should finish even when output is discarded");
}
Ok(())
}
#[cfg(unix)]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn pty_close_stdin_preserves_large_input_and_delivers_eof() -> anyhow::Result<()> {
let Some(python) = find_python() else {
eprintln!("python not found; skipping PTY input backpressure and EOF test");
return Ok(());
};
let script = r"import signal, sys, termios, time
signal.alarm(10)
attrs = termios.tcgetattr(0)
attrs[3] &= ~termios.ECHO
termios.tcsetattr(0, termios.TCSANOW, attrs)
print('__ready__', flush=True)
time.sleep(0.2)
data = sys.stdin.buffer.read()
# Closing the portable writer appends a newline before VEOF.
assert data == b'0123456789abcdefghijklmnopqrstuvwxyz\n' * 32768 + b'\n', len(data)
print('__complete__', flush=True)
";
let env_map: HashMap<String, String> = std::env::vars().collect();
let spawned = spawn_pty_process(
&python,
&["-c".to_string(), script.to_string()],
Path::new("."),
&env_map,
&None,
TerminalSize::default(),
&[],
)
.await?;
let (session, mut output_rx, exit_rx) = combine_spawned_output(spawned);
wait_for_output_contains(&mut output_rx, "__ready__", /*timeout_ms*/ 2_000).await?;
let writer = session.writer_sender();
writer
.send(b"0123456789abcdefghijklmnopqrstuvwxyz\n".repeat(32_768))
.await?;
drop(writer);
session.close_stdin();
let (output, code) = collect_output_until_exit(output_rx, exit_rx, /*timeout_ms*/ 5_000).await;
assert_eq!(
(code, String::from_utf8_lossy(&output).trim()),
(0, "__complete__")
);
Ok(())
}
#[cfg(unix)]
#[test]
fn pty_terminate_reaps_child_when_waiter_is_queued() -> anyhow::Result<()> {