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
fix(mux): report a takeover once the old server has let go
RoversX00d8fb0c9800
diff --git a/wezterm-mux-server-impl/src/handoff.rs b/wezterm-mux-server-impl/src/handoff.rsindex 80d63a50..2661c1ae 100644--- a/wezterm-mux-server-impl/src/handoff.rs+++ b/wezterm-mux-server-impl/src/handoff.rs@@ -1116,17 +1116,30 @@ fn register(Ok(())}-/// Blocks until the old server has closed its end: it is gone, or on macOS-/// holds nothing any more (see `hold`).-pub fn wait_for_predecessor_exit(mut stream: UnixStream) {- stream.set_read_timeout(None).ok();+/// How long the successor waits for the old server to let go before it+/// reports the takeover as done all the same (see `wait_for_predecessor_exit`).+pub const LET_GO_TIMEOUT: Duration = Duration::from_secs(5);++/// Blocks until the old server has closed its end -- it is gone, or on+/// macOS holds nothing any more (see `hold`) -- or for `limit` at most:+/// whether it did. Until then it may still accept on the socket.+pub fn wait_for_predecessor_exit(stream: &mut UnixStream, limit: Option<Duration>) -> bool {+ stream.set_read_timeout(limit).ok();let mut buf = [0u8; 64];loop {match stream.read(&mut buf) {- Ok(0) => return,+ Ok(0) => return true,Ok(_) => {}Err(err) if err.kind() == std::io::ErrorKind::Interrupted => {}- Err(_) => return,+ Err(err)+ if matches!(+ err.kind(),+ std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut+ ) =>+ {+ return false+ }+ Err(_) => return true,}}}@@ -1474,6 +1487,14 @@ mod tests {assert!(terminal.kitty_frame_selections(None).unwrap().1.iter().all(|s| s.image_id != 2));}+ #[test]+ fn waiting_for_the_predecessor_tells_whether_it_let_go() {+ let (mut ours, theirs) = UnixStream::pair().unwrap();+ assert!(!wait_for_predecessor_exit(&mut ours, Some(Duration::from_millis(50))));+ drop(theirs);+ assert!(wait_for_predecessor_exit(&mut ours, Some(Duration::from_millis(50))));+ }+#[test]fn the_handoff_socket_sits_next_to_the_socket() {assert_eq!(diff --git a/wezterm-mux-server/src/main.rs b/wezterm-mux-server/src/main.rsindex e3a09f47..10971d8a 100644--- a/wezterm-mux-server/src/main.rs+++ b/wezterm-mux-server/src/main.rs@@ -500,7 +500,6 @@ async fn async_run(std::process::exit(2);}};- wezterm_mux_server_impl::handoff::report_takeover(Ok(()));match pid_file_fd {Some(fd) => adopt_pid_file(fd),// The predecessor ran in the foreground and had no pid file to@@ -516,15 +515,27 @@ async fn async_run(},None => {}}- // The handoff listener and the TCP listeners wait for the old- // server to exit: it still answers on the handoff path and holds- // the ports until then, and a path someone answers on is not bound- // over. Everything is ours already; not listening for the next- // successor is worth a log line, not an exit.+ // The report and the listeners wait for the old server to exit (or,+ // on macOS, to become a process that only waits): until then it may+ // still accept on the socket, and a client told "ok" that reconnects+ // at once could reach it and be refused as another build. It also+ // still answers on the handoff path and holds the ports, and a path+ // someone answers on is not bound over. Everything is ours already;+ // not listening for the next successor is worth a log line, not an+ // exit.let tls_servers = config.tls_servers.clone();let web_servers = config.web_servers.clone();thread::spawn(move || {- wezterm_mux_server_impl::handoff::wait_for_predecessor_exit(stream);+ let mut stream = stream;+ let let_go = wezterm_mux_server_impl::handoff::wait_for_predecessor_exit(+ &mut stream,+ Some(wezterm_mux_server_impl::handoff::LET_GO_TIMEOUT),+ );+ wezterm_mux_server_impl::handoff::report_takeover(Ok(()));+ if !let_go {+ log::warn!("the old server has not let go yet; reported the takeover all the same");+ wezterm_mux_server_impl::handoff::wait_for_predecessor_exit(&mut stream, None);+ }// The predecessor's node goes with it, as its `thinkterm-tailnet`// ends with it: two processes on one node's keys would take the// node from each other.