Prevent TUI startup hangs when a launcher restores blocking input (#63089)

## Why

A launcher that shares stdin with Codex can clear `O_NONBLOCK` on the
terminal probe's duplicated descriptor. After reading a partial reply,
the probe can then block waiting for more input past its deadline.

## What changed

Poll for readable input with a zero timeout before every probe read.
When no bytes are ready, return to the probe's deadline-aware wait.

## Testing

Add a pseudoterminal regression test that clears `O_NONBLOCK`, answers
only the cursor position query, and checks that the startup screen
appears without another keystroke.

GitOrigin-RevId: 7b6e11db6ac3634f77147ab1251f5321be4355cb
This commit is contained in:
Eric Traut
2026-11-22 07:02:39 +00:00
committed by copyberry
parent e1ecb5c8fc
commit 1476a08f1b
2 changed files with 39 additions and 5 deletions
+9 -5
View File
@@ -183,6 +183,11 @@ mod imp {
if buffer.len() >= max_bytes || Instant::now() >= deadline {
return Ok(());
}
// A launcher sharing stdin can clear O_NONBLOCK on our duplicate.
// Check readiness before every read, including after a partial reply.
if !self.poll_readable(Duration::ZERO)? {
return Ok(());
}
let bytes_to_read = chunk.len().min(max_bytes.saturating_sub(buffer.len()));
let count = unsafe {
@@ -218,12 +223,8 @@ mod imp {
};
let deadline = Instant::now() + timeout;
loop {
let now = Instant::now();
if now >= deadline {
return Ok(false);
}
let timeout_ms = deadline
.saturating_duration_since(now)
.saturating_duration_since(Instant::now())
.as_millis()
.min(libc::c_int::MAX as u128) as libc::c_int;
let result = unsafe {
@@ -239,6 +240,9 @@ mod imp {
if err.kind() != io::ErrorKind::Interrupted {
return Err(err);
}
if Instant::now() >= deadline {
return Ok(false);
}
}
}
}
+30
View File
@@ -29,6 +29,36 @@ mod gov_cloud_guidance;
#[path = "tui_mode_picker_tests.rs"]
mod tui_mode_picker;
#[test]
fn startup_finishes_when_launcher_restores_blocking_input() -> Result<()> {
let repo_root = codex_utils_cargo_bin::repo_root()?;
let codex_home = tempfile::tempdir()?;
write_test_config(codex_home.path(), &repo_root)?;
let mut terminal = PtyCodex::start(&repo_root, codex_home, &[])?;
let deadline = Instant::now() + STARTUP_TIMEOUT;
while !contains_bytes(&terminal.output, b"\x1b[6n") {
terminal.ensure_running()?;
ensure!(Instant::now() < deadline, "missing startup cursor query");
terminal.read_output(Duration::from_millis(/*millis*/ 50))?;
}
// The launcher shares stdin's file status flags. Answer only the cursor query
// so the probe must time out after reading the reply without another keystroke.
let fd = terminal.slave.as_raw_fd();
let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
if flags == -1 || unsafe { libc::fcntl(fd, libc::F_SETFL, flags & !libc::O_NONBLOCK) } == -1 {
return Err(std::io::Error::last_os_error().into());
}
terminal.write_input(b"\x1b[1;1R")?;
while !terminal.screen_contains("OpenAI Codex") {
terminal.ensure_running()?;
ensure!(Instant::now() < deadline, "startup hung on blocking input");
terminal.read_output(Duration::from_millis(/*millis*/ 50))?;
}
Ok(())
}
#[test]
fn focus_gained_with_unanswered_palette_queries_preserves_immediate_input() -> Result<()> {
let repo_root = codex_utils_cargo_bin::repo_root()?;