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-server: answer the control plane off the main thread
RoversXc30e6a59fec8
diff --git a/wezterm-mux-server-impl/src/connections.rs b/wezterm-mux-server-impl/src/connections.rsnew file mode 100644index 00000000..a701fe47--- /dev/null+++ b/wezterm-mux-server-impl/src/connections.rs@@ -0,0 +1,65 @@+//! Where client connections run.+//!+//! The server's main thread is where the mux lives: panes are spawned,+//! split and killed there, and every request that changes the mux hops to+//! it. Until now each client connection ran there too -- decoding+//! requests, encoding replies and output pushes, answering Ping and the+//! version handshake -- so a main thread held up by one pane's work held+//! up every client's connection with it, and from outside the server+//! simply stopped answering.+//!+//! Connections now run here, on a few threads of their own. What has to+//! touch the mux still hops to the main thread the way it always did; what+//! does not -- the handshake, a Ping, the encoding of a reply -- is+//! answered regardless of what the main thread is doing. A stuck pane is+//! then a stuck pane, visible as such, rather than a server that has gone+//! away.++use smol::Executor;+use std::future::Future;+use std::sync::{Arc, LazyLock};++static EXECUTOR: LazyLock<Arc<Executor<'static>>> = LazyLock::new(|| {+ let executor = Arc::new(Executor::new());+ // Two at least: a reply being encoded for one client must not hold+ // the Ping of another. More than a few buys nothing; the mux work+ // itself is still serialised on the main thread.+ let threads = std::thread::available_parallelism()+ .map(|n| n.get())+ .unwrap_or(2)+ .clamp(2, 4);+ for n in 0..threads {+ let executor = Arc::clone(&executor);+ std::thread::Builder::new()+ .name(format!("mux-conn-{n}"))+ .spawn(move || smol::block_on(executor.run(std::future::pending::<()>())))+ .expect("spawning a connection thread");+ }+ executor+});++/// Run `future` -- one client connection, start to finish -- on the+/// connection threads.+pub fn spawn<F>(future: F)+where+ F: Future<Output = ()> + Send + 'static,+{+ EXECUTOR.spawn(future).detach();+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn a_connection_runs_off_the_calling_thread() {+ let (tx, rx) = std::sync::mpsc::channel();+ spawn(async move {+ tx.send(std::thread::current().id()).unwrap();+ });+ let ran_on = rx+ .recv_timeout(std::time::Duration::from_secs(5))+ .expect("the future ran");+ assert_ne!(ran_on, std::thread::current().id());+ }+}diff --git a/wezterm-mux-server-impl/src/dispatch.rs b/wezterm-mux-server-impl/src/dispatch.rsindex 5b8b8b80..409acb16 100644--- a/wezterm-mux-server-impl/src/dispatch.rs+++ b/wezterm-mux-server-impl/src/dispatch.rs@@ -109,6 +109,10 @@ impl Liveness {}}+/// One client connection, from accept to close. Runs on the connection+/// threads (see `connections`), never on the main thread: everything+/// here that changes the mux hops there explicitly, and everything that+/// does not is answered whatever the main thread is doing.pub async fn process<T>(stream: T) -> anyhow::Result<()>whereT: 'static,diff --git a/wezterm-mux-server-impl/src/lib.rs b/wezterm-mux-server-impl/src/lib.rsindex 2a7f9669..ed80353d 100644--- a/wezterm-mux-server-impl/src/lib.rs+++ b/wezterm-mux-server-impl/src/lib.rs@@ -5,6 +5,7 @@ use mux::Mux;use std::sync::Arc;use wezterm_client::domain::{ClientDomain, ClientDomainConfig};+pub mod connections;pub mod dispatch;pub mod local;pub mod pki;diff --git a/wezterm-mux-server-impl/src/local.rs b/wezterm-mux-server-impl/src/local.rsindex 52d59087..83f522c8 100644--- a/wezterm-mux-server-impl/src/local.rs+++ b/wezterm-mux-server-impl/src/local.rs@@ -1,6 +1,5 @@use anyhow::{anyhow, Context as _};use config::{create_user_owned_dirs, UnixDomain};-use promise::spawn::spawn_into_main_thread;use wezterm_uds::UnixListener;pub struct LocalListener {@@ -21,13 +20,11 @@ impl LocalListener {for stream in self.listener.incoming() {match stream {Ok(stream) => {- spawn_into_main_thread(async move {- crate::dispatch::process(stream).await.map_err(|e| {- log::error!("{:#}", e);- e- })- })- .detach();+ crate::connections::spawn(async move {+ if let Err(err) = crate::dispatch::process(stream).await {+ log::error!("{err:#}");+ }+ });}Err(err) => {log::error!("accept failed: {}", err);diff --git a/wezterm-mux-server/src/ossl.rs b/wezterm-mux-server/src/ossl.rsindex 727b036b..f14baf83 100644--- a/wezterm-mux-server/src/ossl.rs+++ b/wezterm-mux-server/src/ossl.rs@@ -3,7 +3,6 @@ use async_ossl::AsyncSslStream;use config::TlsDomainServer;use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod, SslStream, SslVerifyMode};use openssl::x509::X509;-use promise::spawn::spawn_into_main_thread;use std::net::TcpListener;use std::path::Path;use std::sync::Arc;@@ -82,18 +81,16 @@ impl OpenSSLNetListener {log::error!("problem with peer cert: {}", err);break;}- spawn_into_main_thread(async move {- log::error!("Making new AsyncSslStream");- wezterm_mux_server_impl::dispatch::process(AsyncSslStream::new(- stream,- ))+ wezterm_mux_server_impl::connections::spawn(async move {+ log::debug!("Making new AsyncSslStream");+ if let Err(err) = wezterm_mux_server_impl::dispatch::process(+ AsyncSslStream::new(stream),+ ).await- .map_err(|e| {- log::error!("process: {:?}", e);- e- })- })- .detach();+ {+ log::error!("process: {err:?}");+ }+ });}Err(e) => {log::error!("failed TlsAcceptor: {}", e);