Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

fix: keep a keystroke that lands in the same poll as a resize (vendored crossterm) (#3704)

Sebastian Müller8d58cc8fa205
diff --git a/vendor/crossterm/PRIME_AGENT_PATCH.md b/vendor/crossterm/PRIME_AGENT_PATCH.md
index 8febc1fd7a..e972b8e882 100644
--- a/vendor/crossterm/PRIME_AGENT_PATCH.md
+++ b/vendor/crossterm/PRIME_AGENT_PATCH.md
@@ -61,3 +61,17 @@ the enhancement for the RTT>50ms class (the product's primary remote-SSH
deployment shape) while buying only the silent class's raced-transition
stall, which no user rides. The timed contract is locked by
`crates/pa-cli/tests/kitty_verdict_time_e2e.rs`.
+
+# Every readiness token of a poll is handled (2026-10-21)
+
+`src/event/source/unix/mio.rs` `try_read` returned on the first token that produced an event and
+dropped the rest of that poll's tokens. mio registers edge-triggered, so when a resize and a
+keystroke landed in the same poll, one of them was lost (the
+`scripts/check_private_default_frames.py` flake). Now tty input and the resize queue in token order,
+a wake still returns `Interrupted` with the input kept queued, and the blocking tty read is
+unchanged. Upstream crossterm has the same code.
+
+The test is an inline `#[cfg(test)]` module because only it can reach the private
+`UnixInternalEventSource::try_read`; cargo does not test a non-member crate in place, so run it from
+a copy of `vendor/crossterm` without the `[[example]]` entries (the examples are not vendored):
+`TERM=xterm-256color cargo test --features event-stream --lib -- a_keystroke_in_the_same_poll`.
diff --git a/vendor/crossterm/src/event/source/unix/mio.rs b/vendor/crossterm/src/event/source/unix/mio.rs
index 1c6f9437b3..704fead5e8 100644
--- a/vendor/crossterm/src/event/source/unix/mio.rs
+++ b/vendor/crossterm/src/event/source/unix/mio.rs
@@ -89,6 +89,9 @@ impl EventSource for UnixInternalEventSource {
return Ok(None);
}
+ // mio registers edge-triggered: every token of this poll is handled before returning.
+ #[cfg(feature = "event-stream")]
+ let mut woken = false;
for token in self.events.iter().map(|x| x.token()) {
match token {
TTY_TOKEN => {
@@ -114,8 +117,8 @@ impl EventSource for UnixInternalEventSource {
}
};
- if let Some(event) = self.parser.next() {
- return Ok(Some(event));
+ if !self.parser.internal_events.is_empty() {
+ break;
}
}
}
@@ -129,22 +132,30 @@ impl EventSource for UnixInternalEventSource {
// it's a really long time from the mio, async-std/tokio executor, ...
// point of view.
let new_size = crate::terminal::size()?;
- return Ok(Some(InternalEvent::Event(Event::Resize(
- new_size.0, new_size.1,
- ))));
+ self.parser
+ .internal_events
+ .push_back(InternalEvent::Event(Event::Resize(
+ new_size.0, new_size.1,
+ )));
}
}
#[cfg(feature = "event-stream")]
- WAKE_TOKEN => {
- return Err(std::io::Error::new(
- std::io::ErrorKind::Interrupted,
- "Poll operation was woken up by `Waker::wake`",
- ));
- }
+ WAKE_TOKEN => woken = true,
_ => unreachable!("Synchronize Evented handle registration & token handling"),
}
}
+ #[cfg(feature = "event-stream")]
+ if woken {
+ return Err(std::io::Error::new(
+ std::io::ErrorKind::Interrupted,
+ "Poll operation was woken up by `Waker::wake`",
+ ));
+ }
+ if let Some(event) = self.parser.next() {
+ return Ok(Some(event));
+ }
+
// Processing above can take some time, check if timeout expired
if timeout.elapsed() {
return Ok(None);
@@ -227,3 +238,40 @@ impl Iterator for Parser {
self.internal_events.pop_front()
}
}
+
+#[cfg(test)]
+mod tests {
+ use std::io::Write;
+ use std::os::unix::net::UnixStream;
+ use std::time::Duration;
+
+ use super::UnixInternalEventSource;
+ use crate::event::{source::EventSource, Event, InternalEvent, KeyCode};
+ use crate::terminal::sys::file_descriptor::FileDesc;
+
+ /// The resize reads the terminal size: run on a tty, or with `TERM` set for the tput fallback.
+ #[test]
+ fn a_keystroke_in_the_same_poll_as_a_resize_arrives() {
+ let (input, mut keyboard) = UnixStream::pair().unwrap();
+ let mut source =
+ UnixInternalEventSource::from_file_descriptor(FileDesc::Owned(input.into())).unwrap();
+ signal_hook::low_level::raise(signal_hook::consts::SIGWINCH).unwrap();
+ keyboard.write_all(b"ab").unwrap();
+
+ let mut events = Vec::new();
+ while let Some(event) = source.try_read(Some(Duration::from_millis(200))).unwrap() {
+ if let InternalEvent::Event(event) = event {
+ events.push(event);
+ }
+ }
+ assert!(matches!(events.first(), Some(Event::Resize(..))), "{events:?}");
+ let keys: Vec<KeyCode> = events
+ .iter()
+ .filter_map(|event| match event {
+ Event::Key(key) => Some(key.code),
+ _ => None,
+ })
+ .collect();
+ assert_eq!(keys, [KeyCode::Char('a'), KeyCode::Char('b')], "{events:?}");
+ }
+}