Skip to content

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.

perf(mux): treat the process at the other end of the unix socket as local

RoversX455a89dabb83
diff --git a/Cargo.lock b/Cargo.lock
index ee2b69cc..63a5a687 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -10742,6 +10742,7 @@ dependencies = [
"lru",
"metrics",
"mux",
+ "nix 0.29.0",
"passfd",
"portable-pty",
"promise",
diff --git a/wezterm-mux-server-impl/Cargo.toml b/wezterm-mux-server-impl/Cargo.toml
index 1d9382d1..62d4a7fb 100644
--- a/wezterm-mux-server-impl/Cargo.toml
+++ b/wezterm-mux-server-impl/Cargo.toml
@@ -50,6 +50,7 @@ thinkterm-proto.workspace = true
[target."cfg(unix)".dependencies]
passfd.workspace = true
+nix = { workspace = true, features = ["socket"] }
[target."cfg(windows)".dependencies]
winapi = { workspace=true, features = [ "winuser" ]}
diff --git a/wezterm-mux-server-impl/src/dispatch.rs b/wezterm-mux-server-impl/src/dispatch.rs
index 5706ed4d..d7bf1d70 100644
--- a/wezterm-mux-server-impl/src/dispatch.rs
+++ b/wezterm-mux-server-impl/src/dispatch.rs
@@ -192,8 +192,12 @@ where
T: async_io::IoSafe,
T: Send + Sync,
{
+ let local = match peer {
+ ConnectionPeer::Local => LocalSocket::of(&stream),
+ ConnectionPeer::Tls | ConnectionPeer::Web(_) => None,
+ };
let stream = smol::Async::new(stream)?;
- process_stream(stream, peer).await
+ process_stream_on(stream, peer, local).await
}
pub async fn process_async<S: ConnectionStream>(stream: S) -> anyhow::Result<()> {
@@ -206,26 +210,121 @@ pub async fn process_stream<S: ConnectionStream>(
stream: S,
peer: ConnectionPeer,
) -> anyhow::Result<()> {
- process_async_with_peer(stream, Liveness::new(Instant::now()), peer).await
+ process_stream_on(stream, peer, None).await
+}
+
+/// What `process` and `process_stream` share; `local` is the unix socket's
+/// own end, when the accept layer has one.
+async fn process_stream_on<S: ConnectionStream>(
+ stream: S,
+ peer: ConnectionPeer,
+ local: Option<LocalSocket>,
+) -> anyhow::Result<()> {
+ process_async_with_peer(stream, Liveness::new(Instant::now()), peer, local).await
}
pub(crate) async fn process_async_with<S: ConnectionStream>(
stream: S,
liveness: Liveness,
) -> anyhow::Result<()> {
- process_async_with_peer(stream, liveness, ConnectionPeer::Local).await
+ process_async_with_peer(stream, liveness, ConnectionPeer::Local, None).await
+}
+
+/// A unix-socket connection's own end, kept from accept until its client
+/// registers. A client that is the process at the other end of the socket
+/// -- on this machine, reading it itself -- gets payloads uncompressed and
+/// a wide send buffer: macOS starts a unix socket at 8 KiB, and a 20 MiB
+/// picture crosses that in thousands of writes that cost the kernel more
+/// than the copy. Anything else -- a `cli proxy` relaying a remote client,
+/// a relay that does not say so -- keeps compression and the narrow
+/// buffer: what queues there waits on a network link.
+#[cfg(unix)]
+struct LocalSocket {
+ /// A duplicate of the connection's descriptor, closed once the client
+ /// has registered.
+ fd: std::os::fd::OwnedFd,
+ peer_pid: u32,
+}
+
+/// Elsewhere the peer's pid is not read, so no client is taken to be on
+/// this machine.
+#[cfg(not(unix))]
+enum LocalSocket {}
+
+#[cfg(unix)]
+impl LocalSocket {
+ const SEND_BUFFER: usize = 4 * 1024 * 1024;
+
+ fn of(stream: &impl std::os::fd::AsFd) -> Option<Self> {
+ let fd = stream.as_fd();
+ Some(Self {
+ peer_pid: peer_pid(fd)?,
+ fd: fd.try_clone_to_owned().ok()?,
+ })
+ }
+
+ fn peer_pid(&self) -> u32 {
+ self.peer_pid
+ }
+
+ fn widen(self) {
+ use nix::sys::socket::{setsockopt, sockopt};
+ if let Err(err) = setsockopt(&self.fd, sockopt::SndBuf, &Self::SEND_BUFFER) {
+ log::debug!("could not widen a local client's send buffer: {err}");
+ }
+ }
+}
+
+#[cfg(not(unix))]
+impl LocalSocket {
+ fn of<T>(_: &T) -> Option<Self> {
+ None
+ }
+
+ fn peer_pid(&self) -> u32 {
+ match *self {}
+ }
+
+ fn widen(self) {
+ match self {}
+ }
+}
+
+/// The pid of the process at the other end of a unix socket.
+#[cfg(target_os = "macos")]
+fn peer_pid(fd: std::os::fd::BorrowedFd) -> Option<u32> {
+ use nix::sys::socket::{getsockopt, sockopt};
+ use std::convert::TryFrom;
+ u32::try_from(getsockopt(&fd, sockopt::LocalPeerPid).ok()?).ok()
+}
+
+#[cfg(any(target_os = "linux", target_os = "android"))]
+fn peer_pid(fd: std::os::fd::BorrowedFd) -> Option<u32> {
+ use nix::sys::socket::{getsockopt, sockopt};
+ use std::convert::TryFrom;
+ u32::try_from(getsockopt(&fd, sockopt::PeerCredentials).ok()?.pid()).ok()
+}
+
+#[cfg(all(
+ unix,
+ not(any(target_os = "macos", target_os = "linux", target_os = "android"))
+))]
+fn peer_pid(_: std::os::fd::BorrowedFd) -> Option<u32> {
+ None
}
async fn process_async_with_peer<S: ConnectionStream>(
mut stream: S,
mut liveness: Liveness,
peer: ConnectionPeer,
+ mut local: Option<LocalSocket>,
) -> anyhow::Result<()> {
log::trace!("process_async called");
let revoked = match &peer {
ConnectionPeer::Web(web) => Some(web.revoked.clone()),
ConnectionPeer::Local | ConnectionPeer::Tls => None,
};
+ let mut client_link = codec::Link::Network;
let (item_tx, item_rx) = smol::channel::unbounded::<Item>();
let kitty_frames = codec::kitty_queue::KittyFrameMailbox::default();
@@ -415,7 +514,19 @@ async fn process_async_with_peer<S: ConnectionStream>(
return Ok(());
}
liveness.heard(Instant::now());
+ let registers = matches!(decoded.pdu, Pdu::SetClientId(_));
handler.process_one(decoded);
+ // The first registration settles it. A proxy registers
+ // itself before relaying anyone, which is not registering
+ // directly.
+ if registers {
+ if let Some(socket) = local.take() {
+ if handler.direct_client_pid() == Some(socket.peer_pid()) {
+ client_link = codec::Link::SameMachine;
+ socket.widen();
+ }
+ }
+ }
}
Ok(Item::LivenessTick) => unreachable!("handled above"),
Ok(Item::Shutdown) => {
@@ -433,6 +544,7 @@ async fn process_async_with_peer<S: ConnectionStream>(
// A revocation also stops a transfer mid-way: pane contents
// must not go on streaming to a browser whose token is gone.
let moved = AtomicU64::new(0);
+ let link = client_link;
let written = smol::future::or(
smol::future::or(
async {
@@ -442,7 +554,7 @@ async fn process_async_with_peer<S: ConnectionStream>(
};
decoded
.pdu
- .encode_async(&mut counted, decoded.serial)
+ .encode_async_over(&mut counted, decoded.serial, link)
.await
.map_err(WriteFailure::Encode)?;
counted.flush().await.map_err(WriteFailure::Flush)
@@ -766,6 +878,28 @@ mod tests {
const S: Duration = Duration::from_secs(1);
+ #[cfg(unix)]
+ #[test]
+ fn a_local_socket_knows_its_peer_and_widens_its_send_buffer() {
+ use nix::sys::socket::{getsockopt, sockopt};
+ let (ours, _theirs) = std::os::unix::net::UnixStream::pair().unwrap();
+ let narrow = getsockopt(&ours, sockopt::SndBuf).unwrap();
+ let Some(socket) = LocalSocket::of(&ours) else {
+ assert!(
+ cfg!(not(any(target_os = "macos", target_os = "linux", target_os = "android"))),
+ "the peer's pid is readable here"
+ );
+ return;
+ };
+ assert_eq!(socket.peer_pid(), std::process::id());
+ socket.widen();
+ let wide = getsockopt(&ours, sockopt::SndBuf).unwrap();
+ // Linux caps the request at net.core.wmem_max.
+ assert!(wide >= narrow, "{} -> {}", narrow, wide);
+ #[cfg(target_os = "macos")]
+ assert_eq!(wide, LocalSocket::SEND_BUFFER);
+ }
+
#[test]
fn kitty_pushes_coalesce_while_replies_and_legacy_messages_keep_their_order() {
let owner = codec::kitty_queue::KittyFrameMailbox::default();
@@ -897,7 +1031,7 @@ mod tests {
});
let outcome = smol::block_on(smol::future::or(
async {
- process_async_with_peer(stream, Liveness::new(Instant::now()), peer).await?;
+ process_async_with_peer(stream, Liveness::new(Instant::now()), peer, None).await?;
Ok(true)
},
async {
@@ -961,7 +1095,7 @@ mod tests {
});
let outcome = smol::block_on(smol::future::or(
async {
- process_async_with_peer(stream, Liveness::new(Instant::now()), peer).await?;
+ process_async_with_peer(stream, Liveness::new(Instant::now()), peer, None).await?;
Ok(true)
},
async {
diff --git a/wezterm-mux-server-impl/src/sessionhandler.rs b/wezterm-mux-server-impl/src/sessionhandler.rs
index 8f9e183d..c47f1d4b 100644
--- a/wezterm-mux-server-impl/src/sessionhandler.rs
+++ b/wezterm-mux-server-impl/src/sessionhandler.rs
@@ -1193,6 +1193,18 @@ impl SessionHandler {
}
}
+ /// The pid of the client registered on this connection, when it
+ /// registered itself on the unix socket: not relayed by a `cli proxy`,
+ /// not a browser. `None` before it registers.
+ pub(crate) fn direct_client_pid(&self) -> Option<u32> {
+ match self.peer {
+ ConnectionPeer::Local if self.proxy_client_id.is_none() => {
+ self.client_id.as_ref().map(|id| id.pid)
+ }
+ _ => None,
+ }
+ }
+
/// Tell a client that has just registered where the frontend lease
/// stands: the access mode and every tab's ownership.
///
@@ -3155,6 +3167,32 @@ mod tests {
use wezterm_term::color::ColorPalette;
use wezterm_term::TerminalSize;
+ #[test]
+ fn only_a_client_registered_on_the_socket_itself_is_direct() {
+ let id = |pid| ClientId {
+ hostname: "devbox".to_string(),
+ username: "user".to_string(),
+ pid,
+ epoch: 0,
+ id: 0,
+ ssh_auth_sock: None,
+ };
+ let mut handler = super::SessionHandler::new(super::PduSender::new(|_| Ok(())));
+ assert_eq!(handler.direct_client_pid(), None, "not registered yet");
+ handler.client_id = Some(Arc::new(id(42)));
+ assert_eq!(handler.direct_client_pid(), Some(42));
+ // A proxy registers itself before relaying its client's identity.
+ handler.proxy_client_id = Some(id(7));
+ assert_eq!(handler.direct_client_pid(), None);
+
+ let mut tls = super::SessionHandler::for_peer(
+ super::PduSender::new(|_| Ok(())),
+ super::ConnectionPeer::Tls,
+ );
+ tls.client_id = Some(Arc::new(id(42)));
+ assert_eq!(tls.direct_client_pid(), None);
+ }
+
#[test]
fn frame_subscriptions_end_even_when_a_deferred_task_still_holds_the_pane_state() {
use std::sync::atomic::{AtomicUsize, Ordering};