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.

mux: review fixes below the server (codec 65)

RoversX20f34ea41697
diff --git a/Cargo.lock b/Cargo.lock
index 42ad42dd..9c95116d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -868,7 +868,6 @@ dependencies = [
"thinkterm-proto",
"thiserror 1.0.69",
"varbincode",
- "wezterm-input-types 0.1.0",
"wezterm-term",
"zstd",
]
diff --git a/codec/Cargo.toml b/codec/Cargo.toml
index db9938ca..6895851b 100644
--- a/codec/Cargo.toml
+++ b/codec/Cargo.toml
@@ -17,7 +17,6 @@ thinkterm-proto.workspace = true
serde = {workspace=true, features = ["rc", "derive"]}
futures-lite.workspace = true
termwiz.workspace = true
-wezterm-input-types.workspace = true
thiserror.workspace = true
varbincode.workspace = true
wezterm-term = { workspace=true, features=["use_serde"] }
diff --git a/codec/src/lib.rs b/codec/src/lib.rs
index 4251a5ab..31f4b477 100644
--- a/codec/src/lib.rs
+++ b/codec/src/lib.rs
@@ -29,10 +29,10 @@ use std::io::Cursor;
use std::ops::Range;
use std::path::PathBuf;
use std::sync::Arc;
+use termwiz::escape::csi::KittyKeyboardFlags;
use termwiz::hyperlink::Hyperlink;
use termwiz::image::{ImageData, TextureCoordinate};
use termwiz::input::KeyboardEncoding;
-use wezterm_input_types::KittyKeyboardFlags;
use termwiz::surface::{Line, SequenceNo};
use thiserror::Error;
use wezterm_term::color::ColorPalette;
@@ -492,9 +492,12 @@ macro_rules! pdu {
/// 64: Image cells carry the image's generation, so a client notices when
/// an animation it already fetched has grown; GetImageCell can ask for
/// only the frames it lacks and is told the generation it received.
-/// The server probes silent clients with Ping and a client answers
-/// Pong; a remote pane reports its keyboard encoding.
-pub const CODEC_VERSION: usize = 64;
+/// The server probes silent clients with Ping and a client answers Pong.
+/// 65: A remote pane reports its keyboard encoding
+/// (GetPaneRenderChangesResponse.keyboard_encoding). Its own number,
+/// because a server built between the two changes would otherwise
+/// answer 64 and mis-decode every render push.
+pub const CODEC_VERSION: usize = 65;
// Defines the Pdu enum.
// Each struct has an explicit identifying number.
@@ -1825,7 +1828,7 @@ mod golden {
1, 4, 104, 111, 115, 116, 4, 117, 115, 101, 114, 100, 2, 3, 0, 128, 226, 207, 170, 6, 1, 7,
100, 101, 102, 97, 117, 108, 116, 128, 226, 207, 170, 6, 1, 4,
];
- // Re-captured for codec 64, when `keyboard_encoding` (the `3, 3`
+ // Re-captured for codec 65, when `keyboard_encoding` (the `3, 3`
// after `alt_screen`: variant Kitty, flag bits 3) joined the response.
const RENDER_CHANGES: &[u8] = &[
1, 0, 1, 3, 3, 0, 0, 0, 1, 80, 24, 100, 121, 180, 127, 96, 128, 5, 128, 3, 0, 2, 0, 2, 5,
@@ -2197,7 +2200,7 @@ mod test {
// The exact assertion is the tripwire: whoever bumps the codec must
// come here, confirm the round-trips still cover the new version,
// and advance it deliberately.
- assert_eq!(CODEC_VERSION, 64);
+ assert_eq!(CODEC_VERSION, 65);
use thinkterm_proto::{AgentEvidence, AgentState, AgentStatus};
fn round_trip(pdu: Pdu) {
diff --git a/mux/src/lib.rs b/mux/src/lib.rs
index 553b1be8..89da692b 100644
--- a/mux/src/lib.rs
+++ b/mux/src/lib.rs
@@ -1045,11 +1045,22 @@ impl Mux {
/// is free to drive.
pub fn frontend_viewport_state(&self, tab_id: TabId) -> Option<FrontendViewportState> {
let tab = self.get_tab(tab_id)?;
- let lease = self.frontend_lease.lock();
- let access = Self::access_state_locked(&lease);
- let (owner, view, generation) = match lease.tabs.get(&tab_id) {
- Some(state) => (state.owner.clone(), state.view.clone(), state.generation),
- None => (None, None, 0),
+ // The lease is released before the tab is asked its size, the way
+ // the publishing path does it: Tab methods notify with their own
+ // lock held, and a subscriber that ever touches the lease would
+ // close a cycle.
+ let (owner, view, generation, access) = {
+ let lease = self.frontend_lease.lock();
+ let access = Self::access_state_locked(&lease);
+ match lease.tabs.get(&tab_id) {
+ Some(state) => (
+ state.owner.clone(),
+ state.view.clone(),
+ state.generation,
+ access,
+ ),
+ None => (None, None, 0, access),
+ }
};
Some(FrontendViewportState {
tab_id,
@@ -1596,7 +1607,28 @@ impl Mux {
lease.access_generation = generation;
Self::access_state_locked(&lease)
};
- self.notify(MuxNotification::FrontendAccessChanged(state));
+ // Published from wherever the lease moved, which since connections
+ // run on their own threads includes a client's unregistration;
+ // subscribers are only ever run on the main thread.
+ self.notify_on_main_thread(MuxNotification::FrontendAccessChanged(state));
+ }
+
+ /// `notify`, from a thread that may not be the main one. Subscribers
+ /// assume the main thread (several spawn local tasks or reach
+ /// thread-local frontend state), and since connections run on their
+ /// own threads the lease moves from there too, when a client
+ /// unregisters.
+ fn notify_on_main_thread(&self, notification: MuxNotification) {
+ if self.is_main_thread() {
+ self.notify(notification);
+ return;
+ }
+ promise::spawn::spawn_into_main_thread(async move {
+ if let Some(mux) = Mux::try_get() {
+ mux.notify(notification);
+ }
+ })
+ .detach();
}
fn publish_frontend_viewport_state(&self, tab_id: TabId) {
@@ -1616,7 +1648,7 @@ impl Mux {
let Some(tab) = self.get_tab(tab_id) else {
return;
};
- self.notify(MuxNotification::FrontendLeaseChanged(
+ self.notify_on_main_thread(MuxNotification::FrontendLeaseChanged(
FrontendViewportState {
tab_id,
owner,
@@ -2524,10 +2556,15 @@ impl Mux {
}
pub fn get_window(&self, window_id: WindowId) -> Option<MappedRwLockReadGuard<'_, Window>> {
- if !self.windows.read().contains_key(&window_id) {
+ // One acquisition: a check under one guard and an unwrap under the
+ // next was safe only while the main thread was the sole caller.
+ // Connection threads read windows now, and a window removed in the
+ // gap between the two would have been an unwrap of None.
+ let windows = self.windows.read();
+ if !windows.contains_key(&window_id) {
return None;
}
- Some(RwLockReadGuard::map(self.windows.read(), |windows| {
+ Some(RwLockReadGuard::map(windows, |windows| {
windows.get(&window_id).unwrap()
}))
}
@@ -2536,10 +2573,11 @@ impl Mux {
&self,
window_id: WindowId,
) -> Option<MappedRwLockWriteGuard<'_, Window>> {
- if !self.windows.read().contains_key(&window_id) {
+ let windows = self.windows.write();
+ if !windows.contains_key(&window_id) {
return None;
}
- Some(RwLockWriteGuard::map(self.windows.write(), |windows| {
+ Some(RwLockWriteGuard::map(windows, |windows| {
windows.get_mut(&window_id).unwrap()
}))
}
diff --git a/mux/src/termwiztermtab.rs b/mux/src/termwiztermtab.rs
index 00ee2e26..436bbb73 100644
--- a/mux/src/termwiztermtab.rs
+++ b/mux/src/termwiztermtab.rs
@@ -126,6 +126,10 @@ impl TermWizTerminalPane {
}
impl Pane for TermWizTerminalPane {
+ fn render_state_is_contended(&self) -> bool {
+ self.terminal.try_lock().is_none()
+ }
+
fn pane_id(&self) -> PaneId {
self.pane_id
}
diff --git a/term/src/terminalstate/kitty.rs b/term/src/terminalstate/kitty.rs
index 91f4329c..48239787 100644
--- a/term/src/terminalstate/kitty.rs
+++ b/term/src/terminalstate/kitty.rs
@@ -732,12 +732,6 @@ impl TerminalState {
/// it doubles as the glyph cache's key, and changing it per frame would
/// rebuild the decoded image on every paint and reset the animation clock.
fn kitty_touch_placements_for_image(&mut self, image_id: u32) {
- // The identity stays; the generation says the pixels moved. A mux
- // client holds a copy of the image, not the shared Arc, and this is
- // the only signal that its copy has fallen behind.
- if let Some(data) = self.kitty_img.id_to_data.get(&image_id) {
- data.bump_generation();
- }
let placements: Vec<PlacementInfo> = self
.kitty_img
.placements
@@ -917,14 +911,14 @@ impl TerminalState {
anyhow::anyhow!("missing target frame")
})? as usize;
- let img = Arc::clone(
+ let image = Arc::clone(
self.kitty_img
.id_to_data
.get(&image_id)
.ok_or_else(|| anyhow::anyhow!("invalid image id {}", image_id))?,
);
- let mut img = img.data();
+ let mut img = image.data();
match &mut *img {
ImageDataType::EncodedLease(_) | ImageDataType::EncodedFile(_) => {
anyhow::bail!("invalid image type")
@@ -1013,6 +1007,9 @@ impl TerminalState {
}
}
+ // Under the data lock, so whoever reads the payload and the
+ // generation together sees them agree.
+ image.bump_generation();
drop(img);
self.kitty_touch_placements_for_image(image_id);
@@ -1261,6 +1258,7 @@ impl TerminalState {
}
}
+ image.bump_generation();
drop(anim);
// Frames are appended behind the Mutex, so the total measured when
diff --git a/wezterm-cell/src/image.rs b/wezterm-cell/src/image.rs
index d667d1b9..a788a02e 100644
--- a/wezterm-cell/src/image.rs
+++ b/wezterm-cell/src/image.rs
@@ -695,10 +695,9 @@ impl ImageData {
self.generation.load(Ordering::Acquire)
}
- /// Record an in-place change to `data`. Called after the change, not
- /// under the data lock, so a reader that takes the generation first and
- /// the payload second can only end up believing it is *behind*, never
- /// ahead.
+ /// Record an in-place change to `data`. Called with the data lock
+ /// still held, so a reader that takes the payload and the generation
+ /// under one guard sees a matching pair.
pub fn bump_generation(&self) -> u64 {
self.generation.fetch_add(1, Ordering::AcqRel) + 1
}