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
mux: close two races in the deferred resize, and the input drain's hand-back
RoversX1e065bc345f4
diff --git a/mux/src/localpane.rs b/mux/src/localpane.rsindex 9a494699..a1888cba 100644--- a/mux/src/localpane.rs+++ b/mux/src/localpane.rs@@ -632,6 +632,17 @@ impl Pane for LocalPane {term.perform_actions(actions);self.apply_pending_resize(&mut term);self.store_summary(&mut term);+ drop(term);+ // A resize that arrived after that last look, while the terminal+ // was still held here, found both of its own probes failing and+ // left its note; nothing else would read it until the next batch,+ // which a quiet program may never write. Looked at once more with+ // the terminal free, so one side or the other always applies it.+ if self.pending_resize.lock().is_some() {+ let mut term = self.terminal.lock();+ self.apply_pending_resize(&mut term);+ self.store_summary(&mut term);+ }}fn mouse_event(&self, event: MouseEvent) -> Result<(), Error> {@@ -1359,7 +1370,11 @@ impl LocalPane {/// output written in answer to the resize is parsed at the new size;/// after, for a resize that arrived during the batch.fn apply_pending_resize(&self, term: &mut Terminal) {- if let Some(size) = self.pending_resize.lock().take() {+ // The note is taken and its lock let go before the resize: a+ // resize reflows the scrollback, and `summary_without_waiting`+ // reads the note on the thread that must not wait.+ let pending = self.pending_resize.lock().take();+ if let Some(size) = pending {term.resize(size);}}@@ -1671,6 +1686,22 @@ mod summary_tests {assert!(pane.pending_resize.lock().is_none());}+ /// A resize that lands while the parser is between its last look at+ /// the note and letting go of the terminal is still applied by the+ /// parser, without another batch of output.+ #[test]+ fn a_resize_noted_during_the_parsers_last_moments_is_applied_by_the_parser() {+ let (pane, _sizes, _exit) = pane();+ let guard = pane.terminal.lock();+ // What `resize` does when both of its probes fail: the note alone.+ *pane.pending_resize.lock() = Some(size(70, 20));+ drop(guard);+ // The parser's batch ended; its trailing look finds the note.+ pane.perform_actions(vec![]);+ assert_eq!(pane.get_dimensions().cols, 70);+ assert!(pane.pending_resize.lock().is_none());+ }+#[test]fn a_resize_with_the_terminal_free_lands_at_once() {let (pane, _sizes, _exit) = pane();diff --git a/wezterm-mux-server-impl/src/sessionhandler.rs b/wezterm-mux-server-impl/src/sessionhandler.rsindex f02e754d..30b10ece 100644--- a/wezterm-mux-server-impl/src/sessionhandler.rs+++ b/wezterm-mux-server-impl/src/sessionhandler.rs@@ -501,10 +501,13 @@ async fn drain_pane_inputs(let mut drain = InputDrain {per_pane: Arc::clone(&per_pane),why: "the drain of this pane's input ended before it was applied",+ done: false,};loop {- let Some(item) = per_pane.lock().unwrap().inputs.pop_front() else {- drain.finish();+ // Empty check and hand-back under the one lock: input queued+ // between the two would start its own drain, whose queue this+ // task's guard would then take for its own and fail.+ let Some(item) = drain.next() else {return;};let pane = match pane_when_free(pane_id, &sender, "its input").await {@@ -532,17 +535,32 @@ async fn drain_pane_inputs(struct InputDrain {per_pane: Arc<Mutex<PerPane>>,why: &'static str,+ /// The queue was found empty and given back; the guard has nothing+ /// left to do, and must not touch a drain that started since.+ done: bool,}impl InputDrain {- fn finish(&mut self) {+ /// The next piece, or None once the queue is empty -- in which case+ /// the drain is over, decided under the same lock as the look.+ fn next(&mut self) -> Option<QueuedInput> {let mut per_pane = self.per_pane.lock().unwrap();- per_pane.input_draining = false;+ match per_pane.inputs.pop_front() {+ Some(item) => Some(item),+ None => {+ per_pane.input_draining = false;+ self.done = true;+ None+ }+ }}}impl Drop for InputDrain {fn drop(&mut self) {+ if self.done {+ return;+ }let left = {let Ok(mut per_pane) = self.per_pane.lock() else {return;@@ -2702,6 +2720,7 @@ mod tests {drop(InputDrain {per_pane: Arc::clone(&per_pane),why: "ended in the test",+ done: false,});assert_eq!(answered.lock().unwrap().as_slice(),@@ -2713,5 +2732,27 @@ mod tests {per_pane.lock().unwrap().queue_input(item("c")),"and the next input starts a new drain");++ // A drain that found its queue empty is over: input queued after+ // that belongs to the drain it starts, and the old guard must not+ // take it.+ let mut drain = InputDrain {+ per_pane: Arc::clone(&per_pane),+ why: "ended in the test",+ done: false,+ };+ assert!(drain.next().is_some(), "c");+ assert!(drain.next().is_none(), "empty: the drain is over");+ assert!(+ per_pane.lock().unwrap().queue_input(item("d")),+ "so the next input starts a new one"+ );+ drop(drain);+ assert_eq!(+ per_pane.lock().unwrap().inputs.len(),+ 1,+ "d is still queued for the new drain"+ );+ assert!(!answered.lock().unwrap().iter().any(|(tag, _)| *tag == "d"));}}