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.

fix(term): keep no pixels alive in the image dedup cache

RoversXb07495378110
diff --git a/term/src/terminalstate/image.rs b/term/src/terminalstate/image.rs
index 160c76d2..8d5099e4 100644
--- a/term/src/terminalstate/image.rs
+++ b/term/src/terminalstate/image.rs
@@ -301,8 +301,8 @@ impl TerminalState {
let data = data.swap_out()?;
return Ok(Arc::new(ImageData::with_data_and_hash(data, key)));
}
- if let Some(item) = self.image_cache.get(&key) {
- Ok(Arc::clone(item))
+ if let Some(item) = self.image_cache.get(&key).and_then(std::sync::Weak::upgrade) {
+ Ok(item)
} else {
// swap_out preserves the key byte-for-byte (Rgba8/AnimRgba8
// pass through; EncodedFile becomes an EncodedLease whose
@@ -311,7 +311,7 @@ impl TerminalState {
// hashing the full payload a second time.
let data = data.swap_out()?;
let image_data = Arc::new(ImageData::with_data_and_hash(data, key));
- self.image_cache.put(key, Arc::clone(&image_data));
+ self.image_cache.put(key, Arc::downgrade(&image_data));
Ok(image_data)
}
}
@@ -426,4 +426,21 @@ mod tests {
assert!(Arc::ptr_eq(&s1, &s2));
assert_eq!(term.image_cache.len(), 1);
}
+
+ #[test]
+ fn the_dedup_cache_keeps_no_pixels_alive() {
+ let mut term = test_term();
+ let first = term.raw_image_to_image_data(frame(4, 4, 7)).unwrap();
+ let key = first.hash();
+ let released = Arc::downgrade(&first);
+ drop(first);
+ assert!(released.upgrade().is_none(), "unused pixels are freed at once");
+
+ // The same content arriving later gets the same key, and dedups
+ // again while it is in use.
+ let again = term.raw_image_to_image_data(frame(4, 4, 7)).unwrap();
+ let repeat = term.raw_image_to_image_data(frame(4, 4, 7)).unwrap();
+ assert_eq!(again.hash(), key);
+ assert!(Arc::ptr_eq(&again, &repeat));
+ }
}
diff --git a/term/src/terminalstate/mod.rs b/term/src/terminalstate/mod.rs
index bb992c4b..b3e0900c 100644
--- a/term/src/terminalstate/mod.rs
+++ b/term/src/terminalstate/mod.rs
@@ -426,7 +426,10 @@ pub struct TerminalState {
writer: BufWriter<ThreadedWriter>,
- image_cache: lru::LruCache<[u8; 32], Arc<ImageData>>,
+ /// Dedup for small images that are still shown or stored. A weak
+ /// reference: pixels nothing uses any more are freed at once, not kept
+ /// until sixteen newer images push them out.
+ image_cache: lru::LruCache<[u8; 32], std::sync::Weak<ImageData>>,
sixel_scrolls_right: bool,
user_vars: HashMap<String, String>,