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.

wezterm-client: review fixes for images, writes, and keepalive

RoversXed4c843412c2
diff --git a/wezterm-client/src/client.rs b/wezterm-client/src/client.rs
index bccaf34e..5478ff83 100644
--- a/wezterm-client/src/client.rs
+++ b/wezterm-client/src/client.rs
@@ -946,7 +946,13 @@ async fn client_thread_async(
match Pdu::decode_async(&mut stream, Some(next_serial)).await {
Ok(decoded) => {
crate::domain::wake_thinkterm_frontend();
- keepalive_deadline = std::time::Instant::now() + KEEPALIVE_INTERVAL;
+ // Traffic postpones the next ping, but not the
+ // verdict on one already sent: a server that keeps
+ // pushing output while never answering is still
+ // one that never answers.
+ if pending_ping.is_none() {
+ keepalive_deadline = std::time::Instant::now() + KEEPALIVE_INTERVAL;
+ }
log::debug!(
"decoded serial {} {}",
decoded.serial,
@@ -966,6 +972,7 @@ async fn client_thread_async(
}
if pending_ping.map_or(false, |(serial, _)| serial == decoded.serial) {
pending_ping = None;
+ keepalive_deadline = std::time::Instant::now() + KEEPALIVE_INTERVAL;
} else if decoded.serial == 0 {
if registration.is_complete() {
process_unilateral(local_domain_id, connection_generation, decoded)
diff --git a/wezterm-client/src/domain.rs b/wezterm-client/src/domain.rs
index 70f60f85..00414162 100644
--- a/wezterm-client/src/domain.rs
+++ b/wezterm-client/src/domain.rs
@@ -1882,6 +1882,16 @@ pub(crate) fn deliver_thinkterm_session(
}
impl ClientDomain {
+ /// The unix socket this domain connects to, if it is a unix domain.
+ /// A mux server uses it to tell a client domain that leads back to its
+ /// own socket from one that leads elsewhere.
+ pub fn unix_socket_path(&self) -> Option<std::path::PathBuf> {
+ match &self.config {
+ ClientDomainConfig::Unix(unix) => Some(unix.socket_path()),
+ _ => None,
+ }
+ }
+
pub fn new(config: ClientDomainConfig) -> Self {
let local_domain_id = alloc_domain_id();
let label = config.label();
@@ -1969,7 +1979,9 @@ impl ClientDomain {
/// included; `is_attaching` is per-attempt and reads false while the
/// engine waits between attempts.
pub fn is_attach_retrying(&self) -> bool {
- self.attach_retries.load(std::sync::atomic::Ordering::SeqCst) > 0
+ self.attach_retries
+ .load(std::sync::atomic::Ordering::SeqCst)
+ > 0
}
pub fn perform_detach(&self) {
@@ -2404,8 +2416,7 @@ impl ClientDomain {
continue;
}
if let Some(client_pane) = pane.downcast_ref::<ClientPane>() {
- if !client_pane
- .belongs_to_remote_server(connected_server_id.as_deref())
+ if !client_pane.belongs_to_remote_server(connected_server_id.as_deref())
{
client_pane.ignore_next_kill();
}
@@ -3526,6 +3537,10 @@ impl ClientDomain {
let threshold = domain.config.local_echo_threshold_ms();
let overlay_lag_indicator = domain.config.overlay_lag_indicator();
+ // The server behind this attach may be a new process: its image
+ // generations start over, and a copy kept from the old one would
+ // pass for current.
+ crate::pane::forget_images_for_domain(domain_id);
let inner = Arc::new(ClientInner::new(
domain_id,
client,
@@ -5054,8 +5069,7 @@ impl ClientDomain {
// hop, at this client's version. Anything else, and a
// declined offer, keeps the original error.
let mismatch = err.downcast_ref::<crate::client::IncompatibleVersionError>();
- let (ClientDomainConfig::Ssh(ssh), Some(mismatch)) = (&config, mismatch)
- else {
+ let (ClientDomainConfig::Ssh(ssh), Some(mismatch)) = (&config, mismatch) else {
return Err(err);
};
let outcome = {
diff --git a/wezterm-client/src/pane/clientpane.rs b/wezterm-client/src/pane/clientpane.rs
index 65632700..62f000f8 100644
--- a/wezterm-client/src/pane/clientpane.rs
+++ b/wezterm-client/src/pane/clientpane.rs
@@ -978,6 +978,11 @@ impl Pane for ClientPane {
}
fn writer(&self) -> MappedMutexGuard<'_, dyn std::io::Write> {
+ // Kitty-protocol and win32-input-mode keys arrive here rather than
+ // through key_down, so this is where they count as input: for the
+ // client list's idle time, and for the laggy-link indicator.
+ Mux::get().record_input_for_current_identity();
+ self.renderable.lock().inner.borrow_mut().update_last_send();
MutexGuard::map(self.writer.lock(), |writer| {
let w: &mut dyn std::io::Write = writer;
w
@@ -1405,10 +1410,22 @@ struct WriteQueue {
draining: bool,
}
+/// What may wait for a stalled link before further writes are dropped
+/// with a warning: a paste, not a runaway.
+const WRITE_QUEUE_LIMIT: usize = 4 * 1024 * 1024;
+
impl WriteQueue {
/// Queue `data`; true when the caller has to start the drain.
fn push(&mut self, data: &[u8]) -> bool {
- self.pending.extend_from_slice(data);
+ if self.pending.len() + data.len() > WRITE_QUEUE_LIMIT {
+ log::warn!(
+ "dropping {} bytes for a remote pane: {} bytes are already waiting for the link",
+ data.len(),
+ self.pending.len()
+ );
+ } else {
+ self.pending.extend_from_slice(data);
+ }
!std::mem::replace(&mut self.draining, true)
}
@@ -1423,32 +1440,51 @@ impl WriteQueue {
}
}
+/// Marks the drain over when the task ends, however it ends. A drain task
+/// dropped without finishing (a panic caught by the executor, a scheduler
+/// torn down with it queued) would otherwise leave `draining` set, and
+/// every later write to the pane would wait for a drain that never comes.
+struct Draining(Arc<Mutex<WriteQueue>>);
+
+impl Drop for Draining {
+ fn drop(&mut self) {
+ self.0.lock().draining = false;
+ }
+}
+
async fn drain_pane_writes(
client: Arc<ClientInner>,
remote_pane_id: TabId,
remote_tab_id: Arc<AtomicUsize>,
queue: Arc<Mutex<WriteQueue>>,
) {
+ let _draining = Draining(Arc::clone(&queue));
loop {
let Some(data) = queue.lock().take() else {
return;
};
let remote_tab_id = remote_tab_id.load(Ordering::Relaxed);
+ let len = data.len();
let sent = async {
- if client.prepare_remote_tab_input(remote_tab_id).await? {
- client
- .client
- .write_to_pane(WriteToPane {
- pane_id: remote_pane_id,
- data,
- })
- .await?;
+ if !client.prepare_remote_tab_input(remote_tab_id).await? {
+ log::warn!(
+ "dropping {len} bytes for remote pane {remote_pane_id}: this client may \
+ not drive the tab right now"
+ );
+ return Ok(());
}
+ client
+ .client
+ .write_to_pane(WriteToPane {
+ pane_id: remote_pane_id,
+ data,
+ })
+ .await?;
Ok::<(), anyhow::Error>(())
}
.await;
if let Err(err) = sent {
- log::error!("writing to remote pane {remote_pane_id}: {err:#}");
+ log::error!("writing {len} bytes to remote pane {remote_pane_id}: {err:#}");
}
}
}
@@ -1834,4 +1870,20 @@ mod palette_delivery_tests {
assert!(queue.take().is_none(), "empty: the drain ends");
assert!(queue.push(b"c"), "and the next write starts a new one");
}
+
+ #[test]
+ fn a_drain_that_ends_early_gives_the_queue_back() {
+ let queue = std::sync::Arc::new(parking_lot::Mutex::new(super::WriteQueue::default()));
+ assert!(queue.lock().push(b"a"));
+ {
+ let _draining = super::Draining(std::sync::Arc::clone(&queue));
+ // ...the task is dropped here without draining.
+ }
+ assert!(!queue.lock().draining, "the slot is free again");
+ assert_eq!(
+ queue.lock().take().as_deref(),
+ Some(&b"a"[..]),
+ "and the bytes are still waiting for the next drain"
+ );
+ }
}
diff --git a/wezterm-client/src/pane/images.rs b/wezterm-client/src/pane/images.rs
index 23342f29..d0c0c318 100644
--- a/wezterm-client/src/pane/images.rs
+++ b/wezterm-client/src/pane/images.rs
@@ -78,6 +78,9 @@ pub(crate) fn merge_into(
if !matches {
return false;
}
+ // `theirs` is taken apart first; `mine` is emptied last and for
+ // the shortest possible stretch, since every cached line and the
+ // glyph cache point at it.
let taken = std::mem::replace(&mut *theirs, ImageDataType::EncodedFile(Vec::new()));
let ImageDataType::AnimRgba8 {
width,
@@ -93,7 +96,10 @@ pub(crate) fn merge_into(
{
ImageDataType::AnimRgba8 { frames, .. } => frames,
ImageDataType::Rgba8 { data, .. } => vec![data],
- _ => unreachable!("checked above"),
+ other => {
+ *mine = other;
+ return false;
+ }
};
frames.extend(tail);
ImageDataType::AnimRgba8 {
diff --git a/wezterm-client/src/pane/mod.rs b/wezterm-client/src/pane/mod.rs
index a13bc9b0..d0dad9f9 100644
--- a/wezterm-client/src/pane/mod.rs
+++ b/wezterm-client/src/pane/mod.rs
@@ -1,7 +1,8 @@
-pub use clientpane::ClientPane;
pub(crate) use clientpane::remote_server_identity_matches;
+pub use clientpane::ClientPane;
mod clientpane;
mod images;
mod mousestate;
mod renderable;
+pub(crate) use renderable::forget_images_for_domain;
diff --git a/wezterm-client/src/pane/renderable.rs b/wezterm-client/src/pane/renderable.rs
index 06f3edd8..476dfb1c 100644
--- a/wezterm-client/src/pane/renderable.rs
+++ b/wezterm-client/src/pane/renderable.rs
@@ -4,6 +4,7 @@ use anyhow::anyhow;
use codec::*;
use config::{configuration, ConfigHandle};
use lru::LruCache;
+use mux::domain::DomainId;
use mux::pane::PaneId;
use mux::renderable::{RenderableDimensions, StableCursorPosition};
use mux::Mux;
@@ -323,6 +324,11 @@ pub struct RenderableInner {
poll_interval: Duration,
cursor_position: StableCursorPosition,
+ /// Whether the remote pane showed its alternate screen at the last
+ /// push. The two screens share one stable-row space on the wire, so a
+ /// switch changes what every cached row means without any row being
+ /// resent: the cache is cleared on the switch.
+ alt_screen: bool,
pub dimensions: RenderableDimensions,
/// The dimensions most recently confirmed by the remote pane. A GUI
/// takeover may resize its local surface optimistically; keeping the
@@ -383,6 +389,7 @@ impl RenderableInner {
poll_interval: BASE_POLL_INTERVAL,
cursor_position: StableCursorPosition::default(),
dimensions,
+ alt_screen: false,
server_dimensions: dimensions,
frontend_preview: None,
lines: LruCache::new(
@@ -610,6 +617,15 @@ impl RenderableInner {
self.poll_interval = BASE_POLL_INTERVAL;
self.last_recv_time = now;
+ if delta.alt_screen != self.alt_screen {
+ self.alt_screen = delta.alt_screen;
+ // Rows cached from the other screen are stamped with seqnos
+ // the server will never move past, so nothing else would ever
+ // refetch them; the alternate screen's rows in particular sit
+ // at the stable indices of the primary's oldest scrollback.
+ self.invalidate_line_cache(true);
+ }
+
let live_preview_accepts_snapshot = self.frontend_preview.is_none_or(|preview| {
preview.policy != FrontendPreviewPolicy::LiveResize
|| render_dimensions_match_terminal_size(delta.dimensions, preview.size)
@@ -1174,7 +1190,26 @@ impl RenderableInner {
}
lazy_static::lazy_static! {
- static ref IMAGE_LRU: Mutex<LruCache<[u8;32], Arc<ImageData>>> = Mutex::new(LruCache::new(NonZeroUsize::new(128).unwrap()));
+ /// Images fetched from remote panes, by the domain they came from and
+ /// their hash. The domain is part of the key because the generation an
+ /// image carries is a counter private to one server-side object: the
+ /// same picture from two servers, or from one server before and after a
+ /// restart, must not share a copy or compare generations.
+ static ref IMAGE_LRU: Mutex<LruCache<(DomainId, [u8;32]), Arc<ImageData>>> = Mutex::new(LruCache::new(NonZeroUsize::new(128).unwrap()));
+}
+
+/// Drop every image held for `domain_id`: on (re)attach the server may be
+/// a different process, whose generations start over.
+pub(crate) fn forget_images_for_domain(domain_id: DomainId) {
+ let mut lru = IMAGE_LRU.lock().unwrap();
+ let stale: Vec<(DomainId, [u8; 32])> = lru
+ .iter()
+ .filter(|((domain, _), _)| *domain == domain_id)
+ .map(|(key, _)| *key)
+ .collect();
+ for key in stale {
+ lru.pop(&key);
+ }
}
pub(crate) async fn hydrate_lines(
@@ -1190,8 +1225,13 @@ pub(crate) async fn hydrate_lines(
let mut requests = HashMap::new();
let mut data_by_hash = HashMap::new();
+ let domain_id = client.local_domain_id;
for im in &image_cells {
- let held = IMAGE_LRU.lock().unwrap().get(&im.data_hash).cloned();
+ let held = IMAGE_LRU
+ .lock()
+ .unwrap()
+ .get(&(domain_id, im.data_hash))
+ .cloned();
match held {
// A copy at or past the generation the cell was sent with is
// current. An animation grows behind an unchanging hash, so
@@ -1235,7 +1275,7 @@ pub(crate) async fn hydrate_lines(
IMAGE_LRU
.lock()
.unwrap()
- .put(data.hash(), Arc::clone(&data));
+ .put((domain_id, data.hash()), Arc::clone(&data));
data_by_hash.insert(data.hash(), data);
}
@@ -1272,9 +1312,11 @@ pub(crate) async fn hydrate_lines(
/// Fetch the image `request` names and bring `held`, the copy already
/// filed under that hash, up to date in place; the Arc to file is returned.
/// A delta the copy cannot take (its leading frames no longer match) is
-/// followed by one fetch of the whole image; a whole image the copy cannot
-/// take is adopted as a fresh Arc, and lines hydrated from now on point at
-/// that one.
+/// followed by one fetch of the whole image. A copy is never replaced by
+/// a fresh Arc: the glyph cache is keyed by hash and holds the copy, so a
+/// second Arc under the same hash would leave the painter on the old one
+/// for good. A whole image the copy still will not take (it would shrink
+/// under a painter) leaves the copy as it is.
async fn fetch_image(
client: &Arc<ClientInner>,
held: Option<Arc<ImageData>>,
@@ -1284,9 +1326,9 @@ async fn fetch_image(
have_frames: 0,
..request
};
- let asked_for_delta = request.have_frames > 0;
let mut response = client.client.get_image_cell(request).await;
- for _ in 0..2 {
+ let mut asked_for_whole = false;
+ loop {
match response {
Ok(GetImageCellResponse {
data: Some(fresh),
@@ -1294,20 +1336,31 @@ async fn fetch_image(
frames_from,
..
}) => {
- let Some(held) = &held else {
- fresh.set_generation(data_generation);
- return Some(fresh);
- };
- if super::images::merge_into(held, &fresh, frames_from, data_generation) {
- return Some(Arc::clone(held));
+ match &held {
+ None if frames_from == 0 => {
+ fresh.set_generation(data_generation);
+ return Some(fresh);
+ }
+ Some(held)
+ if super::images::merge_into(
+ held,
+ &fresh,
+ frames_from,
+ data_generation,
+ ) =>
+ {
+ return Some(Arc::clone(held));
+ }
+ _ => {}
}
- if frames_from > 0 && asked_for_delta {
+ if !asked_for_whole {
+ asked_for_whole = true;
log::debug!("image delta did not fit the copy held; fetching the whole image");
response = client.client.get_image_cell(GetImageCell { ..whole }).await;
continue;
}
- fresh.set_generation(data_generation);
- return Some(fresh);
+ log::debug!("the whole image did not fit the copy held either; keeping the copy");
+ return held;
}
Ok(GetImageCellResponse { data: None, .. }) => {
// Not an error: the image has been let go of on the server
@@ -1324,7 +1377,6 @@ async fn fetch_image(
}
}
}
- None
}
impl RenderableState {