A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
perf(mux): read a flood of output on macOS without waiting
RoversX544c849cc8a0
diff --git a/mux/src/lib.rs b/mux/src/lib.rsindex 01765e10..251def26 100644--- a/mux/src/lib.rs+++ b/mux/src/lib.rs@@ -681,6 +681,13 @@ const PTY_READ_BUFSIZE: usize = 64 * 1024;/// a time, and less than Linux does.const FULL_PTY_READ: usize = 1024;+/// How many times a reader following a flood tries again when the pty runs+/// dry. The program fills it again within microseconds, which is much less+/// than waiting to be told takes, and a read that finds nothing is cheap.+/// More catch no more of a flood, and cost a program that writes in bursts.+#[cfg(unix)]+const FLOOD_RETRIES: usize = 8;+/// Bytes of external kitty payloads one flush may read into memory. A/// parser that fell behind a frame stream finds hundreds of frame escapes/// in a single read; reading every file they name held gigabytes for@@ -1113,6 +1120,52 @@ lazy_static::lazy_static! {parking_lot::Mutex::new(HashMap::new());}+/// Reads a flood of output without waiting to be told of each piece. macOS+/// hands out a pty's output a KiB at a time, and finding out that there is+/// more takes longer than reading it, which made that the limit on how fast a+/// pane could take in output. Only for output with more behind it: anything+/// less than a full read goes back to waiting, so typing never comes here.+#[cfg(unix)]+struct FloodReader {+ /// A duplicate of the pty that, unlike the pane's reader, says when there+ /// is nothing to read. Non-blocking is a mode of the pty and not of this+ /// duplicate: the pane's reader and writer see it too, and allow for it.+ pty: FileDescriptor,+ following: bool,+}++#[cfg(unix)]+impl FloodReader {+ fn new(pty_fd: i32) -> Option<Self> {+ Some(Self {+ pty: FileDescriptor::dup(&pty_fd).ok()?,+ following: false,+ })+ }++ /// Whether what was just read has more behind it.+ fn follow(&mut self, more: bool) {+ self.following = more && (self.following || self.pty.set_non_blocking(true).is_ok());+ }++ /// Read what is there, or return None for the reader to go back to+ /// waiting, which is also where the pty's end and any error are met.+ fn read(&mut self, buf: &mut [u8]) -> Option<usize> {+ if !self.following {+ return None;+ }+ for _ in 0..FLOOD_RETRIES {+ match self.pty.read(buf) {+ Ok(size) if size > 0 => return Some(size),+ Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => std::hint::spin_loop(),+ _ => break,+ }+ }+ self.following = false;+ None+ }+}+/// A pane reader that is parked. Dropping it lets the reader run again;/// `keep` does not, for the pty that now belongs to another process.pub struct PausedReader {@@ -1155,11 +1208,11 @@ pub fn pause_pane_reader(pane_id: PaneId, timeout: Duration) -> anyhow::Result<P})}-/// This function is run in a separate thread; its purpose is to perform-/// blocking reads from the pty (non-blocking reads are not portable to-/// all platforms) and to feed the bytes to the parser thread through a-/// bounded byte buffer. With a pty file descriptor to poll, the reader can also be-/// parked; see [`pause_pane_reader`].+/// This function is run in a separate thread; its purpose is to read from+/// the pty, blocking except through a flood on macOS (see `FloodReader`), and+/// to feed the bytes to the parser thread through a bounded byte buffer. With+/// a pty file descriptor to poll, the reader can also be parked; see+/// [`pause_pane_reader`].fn read_from_pane_pty(pane: Weak<dyn Pane>,banner: Option<String>,@@ -1170,10 +1223,10 @@ fn read_from_pane_pty(let dead = Arc::new(AtomicBool::new(false));- let (pane_id, exit_behavior) = match pane.upgrade() {- Some(pane) => (pane.pane_id(), pane.exit_behavior()),- None => return,+ let Some(held) = pane.upgrade() else {+ return;};+ let (pane_id, exit_behavior) = (held.pane_id(), held.exit_behavior());let (mut tx, rx) = output_buffer::channel();@@ -1196,6 +1249,15 @@ fn read_from_pane_pty(}None => (None, None, None),};+ // Only while the pane is held: once it has gone, the number of its pty can+ // be another pane's, and this would read that pane's output.+ #[cfg(unix)]+ let mut flood = if cfg!(target_os = "macos") {+ poll_fd.and_then(FloodReader::new)+ } else {+ None+ };+ drop(held);// Every reader can be paused here: a cancel ends the read it blocks in.#[cfg(windows)]let control = {@@ -1237,8 +1299,23 @@ fn read_from_pane_pty(continue;}}+ // What is read here needs no waiting. A pause goes the usual way, and+ // whatever happens to the pty meanwhile, the flood starts again after.+ #[cfg(unix)]+ let flood_read = match (&control, &mut flood) {+ (Some(control), Some(flood)) if control.paused.load(Ordering::SeqCst) => {+ flood.follow(false);+ None+ }+ (_, Some(flood)) => flood.read(&mut buf),+ _ => None,+ };+ #[cfg(not(unix))]+ let flood_read: Option<usize> = None;#[cfg(unix)]- if let (Some(control), Some(wake_read), Some(fd)) = (&control, &mut wake_read, poll_fd) {+ if let (None, Some(control), Some(wake_read), Some(fd)) =+ (flood_read, &control, &mut wake_read, poll_fd)+ {let mut pfd = [pollfd {fd: fd as _,@@ -1286,7 +1363,10 @@ fn read_from_pane_pty(if let Some(control) = &control {*control.in_read.lock() = true;}- let read = reader.read(&mut buf);+ let read = match flood_read {+ Some(size) => Ok(size),+ None => reader.read(&mut buf),+ };#[cfg(windows)]if let Some(control) = &control {*control.in_read.lock() = false;@@ -1316,7 +1396,12 @@ fn read_from_pane_pty(}// A full read says more is coming, and the wake is the most// expensive part of a read. Anything less goes straight on.- let written = if can_flood && size >= FULL_PTY_READ {+ let flooding = can_flood && size >= FULL_PTY_READ;+ #[cfg(unix)]+ if let Some(flood) = &mut flood {+ flood.follow(flooding);+ }+ let written = if flooding {tx.write_all_flooding(&buf[..size])} else {tx.write_all(&buf[..size])@@ -5681,3 +5766,58 @@ mod tests {mux.notify(MuxNotification::PaneRemoved(999_998));}}++#[cfg(all(test, target_os = "macos"))]+mod flood_reader_tests {+ use super::*;+ use portable_pty::{native_pty_system, PtySize};++ /// Typing into the pty echoes it back as output. A child printing it+ /// instead could not exit until that output had been read.+ fn echo(typing: &mut Box<dyn Write + Send>, fd: i32, text: &str) {+ typing.write_all(text.as_bytes()).unwrap();+ let mut output = [pollfd {+ fd,+ events: POLLIN,+ revents: 0,+ }];+ assert_eq!(poll(&mut output, Some(Duration::from_secs(10))).unwrap(), 1);+ }++ #[test]+ fn a_flood_is_read_until_it_stops_and_the_pane_reader_carries_on() {+ let pair = native_pty_system().openpty(PtySize::default()).unwrap();+ let fd = pair.master.as_raw_fd().unwrap();+ let mut reader = pair.master.try_clone_reader().unwrap();+ let mut typing = pair.master.take_writer().unwrap();+ let mut flood = FloodReader::new(fd).unwrap();+ let mut buf = [0; 64];++ // Only a full read starts it, or a quiet pane would never be waited on.+ echo(&mut typing, fd, "first");+ assert_eq!(flood.read(&mut buf), None);+ let size = reader.read(&mut buf).unwrap();+ assert_eq!(&buf[..size], b"first");++ echo(&mut typing, fd, "second");+ flood.follow(true);+ let size = flood.read(&mut buf).unwrap();+ assert_eq!(&buf[..size], b"second");++ // Dry, it gives up rather than waiting for what may never come.+ flood.follow(true);+ assert_eq!(flood.read(&mut buf), None);+ assert_eq!(flood.read(&mut buf), None);++ // The pty stays non-blocking, so the pane's reader has to wait for the+ // next output itself rather than take the empty pty for an error.+ let late = std::thread::spawn(move || {+ std::thread::sleep(Duration::from_millis(100));+ typing.write_all(b"third").unwrap();+ typing+ });+ let size = reader.read(&mut buf).unwrap();+ assert_eq!(&buf[..size], b"third");+ drop(late.join().unwrap());+ }+}diff --git a/mux/src/localpane.rs b/mux/src/localpane.rsindex 303f982a..1ac61b6d 100644--- a/mux/src/localpane.rs+++ b/mux/src/localpane.rs@@ -3287,17 +3287,20 @@ mod summary_tests {}}- /// While a reader is paused, what the program writes stays in the pty;- /// resuming applies it, and a kept pause never does.+ /// A pane with its reader running, and the end of the pipe it reads from.#[cfg(unix)]- #[test]- fn a_paused_reader_leaves_the_pty_output_where_it_is() {+ fn pane_on_a_pipe(+ id: PaneId,+ ) -> (+ Arc<dyn Pane>,+ filedescriptor::FileDescriptor,+ std::sync::mpsc::Sender<()>,+ ) {use std::os::unix::io::AsRawFd;ensure_scheduler();let pipe = filedescriptor::Pipe::new().unwrap();- let mut program = pipe.write;let raw = pipe.read.as_raw_fd();- let (_exit_tx, exit_rx) = std::sync::mpsc::channel();+ let (exit_tx, exit_rx) = std::sync::mpsc::channel();let terminal = Terminal::new(size(80, 24),Arc::new(config::TermConfig::new()),@@ -3306,7 +3309,7 @@ mod summary_tests {Box::new(Vec::new()),);let pane: Arc<dyn Pane> = Arc::new(LocalPane::new(- 4242,+ id,terminal,Box::new(SleepingChild(exit_rx)),Box::new(PipePty { read: pipe.read }),@@ -3317,25 +3320,68 @@ mod summary_tests {let reader = pane.reader().unwrap().unwrap();let weak = Arc::downgrade(&pane);std::thread::spawn(move || crate::read_from_pane_pty(weak, None, reader, Some(raw)));+ (pane, pipe.write, exit_tx)+ }- let title = |pane: &Arc<dyn Pane>| pane.summary_without_waiting().title;- let wait_for = |pane: &Arc<dyn Pane>, want: &str| {- let deadline = Instant::now() + Duration::from_secs(5);- while title(pane) != want && Instant::now() < deadline {- std::thread::sleep(Duration::from_millis(5));+ #[cfg(unix)]+ fn wait_for_title(pane: &Arc<dyn Pane>, want: &str) {+ let title = || pane.summary_without_waiting().title;+ let deadline = Instant::now() + Duration::from_secs(5);+ while title() != want && Instant::now() < deadline {+ std::thread::sleep(Duration::from_millis(5));+ }+ assert_eq!(title(), want);+ }++ /// A reader going through a flood has no wait to be woken from, and stops+ /// for a pause all the same.+ #[cfg(unix)]+ #[test]+ fn a_reader_in_a_flood_stops_for_a_pause() {+ let (pane, mut program, _exit_tx) = pane_on_a_pipe(4244);+ program.write_all(b"\x1b]0;started\x07").unwrap();+ wait_for_title(&pane, "started");++ let stop = Arc::new(std::sync::atomic::AtomicBool::new(false));+ let flood = std::thread::spawn({+ let stop = Arc::clone(&stop);+ move || {+ while !stop.load(std::sync::atomic::Ordering::Relaxed) {+ program.write_all(&[b' '; 4096]).unwrap();+ }+ program}- assert_eq!(title(pane), want);- };+ });+ std::thread::sleep(Duration::from_millis(20));++ let paused = crate::pause_pane_reader(4244, Duration::from_secs(2)).expect("pause");+ // The program stops only once the reader has gone on and made room.+ stop.store(true, std::sync::atomic::Ordering::Relaxed);+ std::thread::sleep(Duration::from_millis(20));+ drop(paused);+ let mut program = flood.join().unwrap();++ program.write_all(b"\x1b]0;after\x07").unwrap();+ wait_for_title(&pane, "after");+ }++ /// While a reader is paused, what the program writes stays in the pty;+ /// resuming applies it, and a kept pause never does.+ #[cfg(unix)]+ #[test]+ fn a_paused_reader_leaves_the_pty_output_where_it_is() {+ let (pane, mut program, _exit_tx) = pane_on_a_pipe(4242);+ let title = |pane: &Arc<dyn Pane>| pane.summary_without_waiting().title;program.write_all(b"\x1b]0;first\x07").unwrap();- wait_for(&pane, "first");+ wait_for_title(&pane, "first");let paused = crate::pause_pane_reader(4242, Duration::from_secs(2)).expect("pause");program.write_all(b"\x1b]0;second\x07").unwrap();std::thread::sleep(Duration::from_millis(150));assert_eq!(title(&pane), "first", "nothing is read while paused");drop(paused);- wait_for(&pane, "second");+ wait_for_title(&pane, "second");let paused = crate::pause_pane_reader(4242, Duration::from_secs(2)).expect("pause again");program.write_all(b"\x1b]0;third\x07").unwrap();diff --git a/mux/src/pane.rs b/mux/src/pane.rsindex 46aab8b4..a93744ec 100644--- a/mux/src/pane.rs+++ b/mux/src/pane.rs@@ -366,7 +366,7 @@ pub trait Pane: Downcast + Send + Sync {fn reader(&self) -> anyhow::Result<Option<Box<dyn std::io::Read + Send>>>;/// The pty behind `reader`, when there is one: the reader thread waits/// for it with poll, which is what lets it be parked (see- /// `crate::pause_pane_reader`).+ /// `crate::pause_pane_reader`), and can read a flood from it directly.#[cfg(unix)]fn pty_raw_fd(&self) -> Option<std::os::unix::io::RawFd> {None