From fbb8258acea55acc9b2a78fd691bbcca16613a5f Mon Sep 17 00:00:00 2001 From: afterimaginary <126125402+afterimaginary@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:04:04 -0800 Subject: [PATCH] fix(cli): stop the header color query from hanging on macOS macOS poll() does not support /dev/tty: it returns POLLNVAL at once instead of waiting, so the query's 200 ms deadline never applied and each read blocked until the terminal answered. In a terminal that never answers the DA1 sentinel, every command that prints the header hung before it ran. Wait with select() on macOS, which works with /dev/tty. Co-Authored-By: Claude Opus 5.5 --- .../header_color_query_no_reply/package.json | 1 + .../snapshots.toml | 10 ++++ .../snapshots/header_color_query_no_reply.md | 13 +++++ crates/vp_shared/src/header.rs | 53 ++++++++++++++----- 4 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/header_color_query_no_reply/package.json create mode 100644 crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/header_color_query_no_reply/snapshots.toml create mode 100644 crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/header_color_query_no_reply/snapshots/header_color_query_no_reply.md diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/header_color_query_no_reply/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/header_color_query_no_reply/package.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/header_color_query_no_reply/package.json @@ -0,0 +1 @@ +{} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/header_color_query_no_reply/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/header_color_query_no_reply/snapshots.toml new file mode 100644 index 0000000000..b4141f2958 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/header_color_query_no_reply/snapshots.toml @@ -0,0 +1,10 @@ +# The runner's terminal never answers the header's color queries or the DA1 +# sentinel. `COLORTERM=truecolor` makes `vp` send them, so the query must give +# up at its deadline instead of blocking the command. +[[case]] +name = "header_color_query_no_reply" +vp = "global" +env = { COLORTERM = "truecolor" } +steps = [ + { argv = ["vp", "--definitely-invalid"], comment = "The header falls back to default colors and the command finishes, although the terminal never answers", continue-on-failure = true }, +] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/header_color_query_no_reply/snapshots/header_color_query_no_reply.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/header_color_query_no_reply/snapshots/header_color_query_no_reply.md new file mode 100644 index 0000000000..d6e912e896 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/header_color_query_no_reply/snapshots/header_color_query_no_reply.md @@ -0,0 +1,13 @@ +# header_color_query_no_reply + +## `vp --definitely-invalid` + +The header falls back to default colors and the command finishes, although the terminal never answers + +**Exit code:** 2 + +``` +VITE+ - The Unified Toolchain for the Web + +error: Unexpected argument '--definitely-invalid' +``` diff --git a/crates/vp_shared/src/header.rs b/crates/vp_shared/src/header.rs index 277cd2e863..3f1cfb993c 100644 --- a/crates/vp_shared/src/header.rs +++ b/crates/vp_shared/src/header.rs @@ -287,6 +287,45 @@ fn read_until_either( } } +/// Waits up to `timeout` for `fd` to become readable. Returns `false` on timeout. +/// +/// macOS `poll()` does not support `/dev/tty`: it returns `POLLNVAL` at once +/// instead of waiting, so a following `read()` blocks until the terminal sends +/// something. A terminal that never answers the DA1 query would then hang `vp`. +/// `select()` works with `/dev/tty` on macOS, as terminal-colorsaurus also does. +#[cfg(target_os = "macos")] +fn wait_readable(fd: std::os::fd::BorrowedFd<'_>, timeout: Duration) -> std::io::Result { + use std::os::fd::AsRawFd; + + use nix::sys::{ + select::{FD_SETSIZE, FdSet, select}, + time::TimeVal, + }; + + // `FdSet::insert` panics for descriptors that don't fit in an fd_set. + if usize::try_from(fd.as_raw_fd()).map_or(true, |raw| raw >= FD_SETSIZE) { + return Ok(false); + } + let mut readfds = FdSet::new(); + readfds.insert(fd); + let micros = i64::try_from(timeout.as_micros()).unwrap_or(i64::MAX); + let mut timeval = TimeVal::new(micros / 1_000_000, (micros % 1_000_000) as _); + let ready = select(None, &mut readfds, None, None, &mut timeval)?; + Ok(ready > 0 && readfds.contains(fd)) +} + +/// Waits up to `timeout` for `fd` to become readable. Returns `false` on timeout. +#[cfg(all(unix, not(target_os = "macos")))] +fn wait_readable(fd: std::os::fd::BorrowedFd<'_>, timeout: Duration) -> std::io::Result { + use nix::poll::{PollFd, PollFlags, PollTimeout, poll}; + + let Ok(timeout) = PollTimeout::try_from(timeout) else { + return Ok(false); + }; + let mut fds = [PollFd::new(fd, PollFlags::POLLIN)]; + Ok(poll(&mut fds, timeout)? > 0) +} + /// Queries terminal colors using the DA1 sandwich technique with /// stream-based response parsing (modelled after `terminal-colorsaurus`). /// @@ -301,10 +340,7 @@ fn query_terminal_colors(palette_indices: &[u8]) -> (Option, Vec<(u8, Rgb)> os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd}, }; - use nix::{ - poll::{PollFd, PollFlags, PollTimeout, poll}, - sys::termios::{SetArg, Termios, cfmakeraw, tcgetattr, tcsetattr}, - }; + use nix::sys::termios::{SetArg, Termios, cfmakeraw, tcgetattr, tcsetattr}; if is_osc_query_unsupported() { return (None, vec![]); @@ -380,14 +416,7 @@ fn query_terminal_colors(palette_indices: &[u8]) -> (Option, Vec<(u8, Rgb)> impl io::Read for TtyReader<'_> { fn read(&mut self, buf: &mut [u8]) -> io::Result { let remaining = self.deadline.saturating_duration_since(Instant::now()); - if remaining.is_zero() { - return Err(io::Error::new(io::ErrorKind::TimedOut, "tty read timed out")); - } - let mut fds = [PollFd::new(self.tty.as_fd(), PollFlags::POLLIN)]; - let timeout = PollTimeout::try_from(remaining) - .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "tty read timed out"))?; - let ready = poll(&mut fds, timeout).map_err(io::Error::from)?; - if ready == 0 { + if remaining.is_zero() || !wait_readable(self.tty.as_fd(), remaining)? { return Err(io::Error::new(io::ErrorKind::TimedOut, "tty read timed out")); } io::Read::read(&mut &*self.tty, buf)