Skip to content

armgit / herdr

Public Imported from GitHub
0

the runtime your coding agents live on

fix: keep polling when a resize samples a zero-sized grid (#5310)

JJ Liebig143bf5fd4781
diff --git a/src/client/terminal_geometry.rs b/src/client/terminal_geometry.rs
index 02d6d05aa..4126fc11b 100644
--- a/src/client/terminal_geometry.rs
+++ b/src/client/terminal_geometry.rs
@@ -141,39 +141,73 @@ pub(super) fn resize_poll_loop(
should_quit: &Arc<AtomicBool>,
) {
crate::platform::watch_terminal_resize_signal();
- let mut last_size = (
- initial_cols,
- initial_rows,
- initial_cell_width,
- initial_cell_height,
- initial_pixel_geometry_exact,
+ resize_poll_loop_with(
+ resize_tx,
+ (
+ initial_cols,
+ initial_rows,
+ initial_cell_width,
+ initial_cell_height,
+ initial_pixel_geometry_exact,
+ ),
+ |last_cell_size| {
+ current_terminal_geometry(
+ pixel_geometry_enabled,
+ pixel_geometry_fallback,
+ reported_cell_size,
+ Some(last_cell_size),
+ )
+ },
+ crate::platform::take_terminal_resize_signal,
+ should_quit,
);
+}
+
+/// The resize watcher loop, with its geometry and resize-signal sources
+/// injected so the transient/recovery behavior is testable without a terminal.
+pub(super) fn resize_poll_loop_with(
+ resize_tx: tokio::sync::mpsc::Sender<ClientLoopEvent>,
+ initial_size: (u16, u16, u32, u32, bool),
+ mut read_geometry: impl FnMut((u32, u32)) -> io::Result<TerminalGeometry>,
+ mut take_signal: impl FnMut() -> bool,
+ should_quit: &Arc<AtomicBool>,
+) {
+ let mut last_size = initial_size;
+ // Retained across a transient sample so a resize that lands back on the
+ // same size still re-reports (host modes may have been reset).
+ let mut signalled = false;
while !should_quit.load(Ordering::Acquire) {
std::thread::sleep(Duration::from_millis(100));
- let signalled = crate::platform::take_terminal_resize_signal();
- let new_size = match current_terminal_geometry(
- pixel_geometry_enabled,
- pixel_geometry_fallback,
- reported_cell_size,
- Some((last_size.2, last_size.3)),
- ) {
- Ok(size) => size,
- Err(err) => {
- let _ = resize_tx.blocking_send(ClientLoopEvent::TerminalUnavailable(err));
- break;
- }
- };
+ signalled |= take_signal();
+ let (cols, rows, cell_width_px, cell_height_px, exact) =
+ match read_geometry((last_size.2, last_size.3)) {
+ Ok(size) => size,
+ // A window drag can momentarily report a zero-sized grid. Keep
+ // the last known geometry and the pending signal, then resample
+ // instead of treating the terminal as gone.
+ Err(err) if crate::platform::is_transient_terminal_grid_error(&err) => continue,
+ Err(err) => {
+ let _ = resize_tx.blocking_send(ClientLoopEvent::TerminalUnavailable(err));
+ break;
+ }
+ };
+ let new_size = (cols, rows, cell_width_px, cell_height_px, exact);
if resize_report_required(signalled, new_size, last_size) {
last_size = new_size;
if resize_tx
.blocking_send(ClientLoopEvent::Resize(
- new_size.0, new_size.1, new_size.2, new_size.3, new_size.4,
+ cols,
+ rows,
+ cell_width_px,
+ cell_height_px,
+ exact,
))
.is_err()
{
break;
}
}
+ signalled = false;
}
}
diff --git a/src/client/tests/mod.rs b/src/client/tests/mod.rs
index 323af2f87..8e2a37ec4 100644
--- a/src/client/tests/mod.rs
+++ b/src/client/tests/mod.rs
@@ -31,6 +31,89 @@ fn unavailable_terminal_grid_is_not_fabricated() {
assert_eq!(err.kind(), io::ErrorKind::NotConnected);
}
+#[test]
+fn resize_poll_loop_keeps_polling_through_a_transient_zero_grid() {
+ let (resize_tx, mut resize_rx) = tokio::sync::mpsc::channel(8);
+ let should_quit = Arc::new(AtomicBool::new(false));
+ let quit = Arc::clone(&should_quit);
+ let mut samples = vec![
+ Err(crate::platform::zero_sized_terminal_grid_error()),
+ Ok((80u16, 24u16, 8u32, 16u32, true)),
+ ]
+ .into_iter();
+ let mut signals = vec![true, false].into_iter();
+
+ super::terminal_geometry::resize_poll_loop_with(
+ resize_tx,
+ (80, 24, 8, 16, true),
+ move |_| match samples.next() {
+ Some(sample) => sample,
+ None => {
+ quit.store(true, Ordering::Release);
+ Ok((80, 24, 8, 16, true))
+ }
+ },
+ move || signals.next().unwrap_or(false),
+ &should_quit,
+ );
+
+ // The transient zero-sized sample must keep the loop polling and retain the
+ // pending resize signal, so the recovered size re-reports exactly once even
+ // though it matches the last known size, and no failure is reported.
+ let mut events = Vec::new();
+ while let Ok(event) = resize_rx.try_recv() {
+ events.push(event);
+ }
+ assert!(
+ matches!(
+ events.as_slice(),
+ [super::ClientLoopEvent::Resize(80, 24, 8, 16, true)]
+ ),
+ "expected exactly one recovered resize report"
+ );
+}
+
+#[test]
+fn resize_poll_loop_detaches_once_on_a_real_terminal_failure() {
+ let (resize_tx, mut resize_rx) = tokio::sync::mpsc::channel(8);
+ let should_quit = Arc::new(AtomicBool::new(false));
+ let mut samples = vec![
+ Err(crate::platform::zero_sized_terminal_grid_error()),
+ Err(io::Error::new(
+ io::ErrorKind::NotConnected,
+ "terminal is gone",
+ )),
+ ]
+ .into_iter();
+
+ super::terminal_geometry::resize_poll_loop_with(
+ resize_tx,
+ (80, 24, 8, 16, true),
+ move |_| {
+ samples
+ .next()
+ .expect("polling must stop after a real terminal failure")
+ },
+ || false,
+ &should_quit,
+ );
+
+ // The transient sample must not stop the loop, but the following real
+ // failure must report exactly one terminal-unavailable event and terminate.
+ let mut events = Vec::new();
+ while let Ok(event) = resize_rx.try_recv() {
+ events.push(event);
+ }
+ assert!(
+ matches!(
+ events.as_slice(),
+ [super::ClientLoopEvent::TerminalUnavailable(err)]
+ if err.kind() == io::ErrorKind::NotConnected
+ ),
+ "expected exactly one terminal-unavailable event"
+ );
+}
+
#[test]
fn missing_pixel_geometry_keeps_a_valid_terminal_grid() {
let reported_cell_size = AtomicU64::new(0);
diff --git a/src/platform/mod.rs b/src/platform/mod.rs
index 8af0205f6..2f4bf16df 100644
--- a/src/platform/mod.rs
+++ b/src/platform/mod.rs
@@ -316,6 +316,33 @@ pub(crate) fn terminal_erase_byte() -> Option<u8> {
None
}
+/// Error kind marker for a terminal that momentarily has no usable grid.
+///
+/// A window drag can sample a zero-sized grid before the terminal settles, so
+/// this is distinct from a real terminal failure. Startup still rejects it; the
+/// resize watcher treats it as a transient sample instead of a dead terminal.
+#[derive(Debug)]
+pub(crate) struct ZeroSizedGridError;
+
+impl std::fmt::Display for ZeroSizedGridError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str("terminal reported a zero-sized grid")
+ }
+}
+
+impl std::error::Error for ZeroSizedGridError {}
+
+pub(crate) fn zero_sized_terminal_grid_error() -> std::io::Error {
+ std::io::Error::new(std::io::ErrorKind::InvalidData, ZeroSizedGridError)
+}
+
+/// Whether an error only means the terminal has no usable size yet, so a
+/// resizing terminal should keep its last known geometry instead of detaching.
+pub(crate) fn is_transient_terminal_grid_error(err: &std::io::Error) -> bool {
+ err.get_ref()
+ .is_some_and(|inner| inner.is::<ZeroSizedGridError>())
+}
+
pub(crate) fn terminal_grid_size() -> std::io::Result<(u16, u16)> {
#[cfg(unix)]
let (cols, rows) = unix_common::read_terminal_grid_size()?;
@@ -325,10 +352,7 @@ pub(crate) fn terminal_grid_size() -> std::io::Result<(u16, u16)> {
let (cols, rows) = fallback::read_terminal_grid_size()?;
if cols == 0 || rows == 0 {
- return Err(std::io::Error::new(
- std::io::ErrorKind::InvalidData,
- "terminal reported a zero-sized grid",
- ));
+ return Err(zero_sized_terminal_grid_error());
}
Ok((cols, rows))
}