#5606·zellij

Returning focus to a tab can stall pane updates for 5–10 seconds during palette queries

Author: jzyrobertCreated Sep 10, 2026Updated Sep 29, 2026
Labelssuspected bug

2. Issues with the Zellij UI / behavior / crash

Issue description

Returning focus to a tab can make the pane appear frozen for 5–10 seconds when the application refreshes its terminal color palette. herdr is one example of an application that triggers this.

This occurs when switching between Zellij tabs and when leaving and returning focus to the host terminal. The same application responds promptly when run directly outside Zellij.

Tracing shows that herdr processes input and writes an updated frame within approximately 30 ms, but Zellij delays displaying that frame until the preceding terminal queries have completed.

Expected behavior: Returning focus should allow pane updates to appear promptly, including when the application queries the terminal palette.

Actual behavior: Updated pane contents remain buffered for several seconds, making the application appear unresponsive.

Minimal reproduction

  1. Start Zellij in a terminal connection with nontrivial terminal-query round-trip latency. The delay is readily reproducible with approximately 20–40 ms of query latency.
  2. Run herdr in a Zellij tab.
  3. Create or switch to another Zellij tab.
  4. Switch back to the tab running herdr and immediately interact with it.
  5. Observe the delay before the pane displays the updated application state.

Leaving focus on the host terminal and returning to it also triggers the issue.

The underlying triggering output is a foreground/background query followed by a full 256-color palette refresh:

  • An OSC 10 foreground-color query.
  • An OSC 11 background-color query.
  • OSC 4 queries for palette registers 0 through 255.
  • An updated screen written immediately afterward.

No continuous high-volume output is required.

Other relevant information

  • Observed with Zellij 0.45.1 on Linux.
  • Example triggering application: herdr 0.8.2.
  • Reproduced in isolated sessions using real herdr clients.
  • Terminal-query reply delays were injected by a controlled PTY harness.

Measurements

Terminal-query round-trip latency Before fix: focus-return display delay With suggested fix
20 ms 5.44 seconds 92 ms
40 ms 10.64 seconds 152 ms

Outside Zellij, the same workload responded in approximately 25–30 ms.

Switching between Zellij tabs also improved from 5.43 seconds to 85 ms in the 20 ms test.

Root cause

Zellij forwards each palette query separately and appends a device-attributes barrier to each request. Processing of subsequent pane output pauses while the corresponding host response is outstanding.

A full foreground/background and palette refresh therefore incurs approximately 258 sequential round trips. The application can already have written its updated screen, but that output remains buffered behind the queries.

Suggested fix

Batch consecutive, complete OSC 4 palette queries into a bounded exchange containing at most 256 queries. Forward the batch through the existing host-query channel with one device-attributes barrier.

Foreground/background queries retain their existing handling, reducing the full refresh from 258 exchanges to approximately three. Batch collection stops at text, setters, other queries, and incomplete sequences. Cache fallback and per-pane reply ordering are preserved.

The batch must be collected from the pending PTY buffer: the Kitty interceptor flushes input at escape-sequence boundaries, so individual VTE input slices do not necessarily contain the remainder of the query burst.

Suggested implementation diff
diff
diff --git a/zellij-server/src/host_query.rs b/zellij-server/src/host_query.rs
index fdc850226..f8f0fbf34 100644
--- a/zellij-server/src/host_query.rs
+++ b/zellij-server/src/host_query.rs
@@ -56,6 +56,8 @@ pub enum HostQuery {
         index: u8,
         terminator: OscTerminator,
     },
+    /// Consecutive palette queries share one host round trip and DA barrier.
+    PaletteRegisters { registers: Vec<(u8, OscTerminator)> },
     ClipboardContent {
         selection: char,
         terminator: OscTerminator,
@@ -94,6 +96,12 @@ impl HostQuery {
                 v.extend_from_slice(terminator.as_bytes());
                 v
             },
+            HostQuery::PaletteRegisters { registers } => registers
+                .iter()
+                .flat_map(|&(index, terminator)| {
+                    HostQuery::PaletteRegister { index, terminator }.to_query_bytes()
+                })
+                .collect(),
             HostQuery::ClipboardContent {
                 selection,
                 terminator,
diff --git a/zellij-server/src/panes/terminal_pane.rs b/zellij-server/src/panes/terminal_pane.rs
index c90c50edf..160f9115b 100644
--- a/zellij-server/src/panes/terminal_pane.rs
+++ b/zellij-server/src/panes/terminal_pane.rs
@@ -1,3 +1,4 @@
+use crate::host_query::HostQuery;
 use crate::output::{CharacterChunk, KittyImageChunk, SixelImageChunk};
 use crate::panes::kitty_graphics::{
     InterceptorResult, KittyApcInterceptor, KittyHostSupport, KittyImageStore,
@@ -40,6 +41,7 @@ use zellij_utils::{
 use crate::ui::pane_boundaries_frame::{FrameParams, PaneFrame};
 
 pub const SELECTION_SCROLL_INTERVAL_MS: u64 = 10;
+const MAX_PALETTE_QUERY_BATCH: usize = 256;
 
 // Some keys in different formats but are used in the code
 const LEFT_ARROW: &[u8] = &[27, 91, 68];
@@ -258,6 +260,9 @@ impl Pane for TerminalPane {
                                 self.pending_pty_input.extend(&forwarded[consumed..]);
                                 self.pending_pty_input.extend(&bytes[index - 1..]);
                                 self.kitty_interceptor.reset();
+                                self.batch_pending_palette_queries(
+                                    forwarded[..consumed].last().copied(),
+                                );
                                 return;
                             }
                             forwarded.clear();
@@ -276,6 +281,9 @@ impl Pane for TerminalPane {
                             self.pending_pty_input.extend(&cmd);
                             self.pending_pty_input.extend(b"\x1b\\");
                             self.pending_pty_input.extend(&bytes[index..]);
+                            self.batch_pending_palette_queries(
+                                forwarded[..consumed].last().copied(),
+                            );
                             return;
                         }
                         forwarded.clear();
@@ -296,6 +304,7 @@ impl Pane for TerminalPane {
             .advance_until_terminated(&mut self.grid, &forwarded);
         if consumed < forwarded.len() {
             self.pending_pty_input.extend(&forwarded[consumed..]);
+            self.batch_pending_palette_queries(forwarded[..consumed].last().copied());
         }
     }
     fn cursor_coordinates(&self, client_id: Option<ClientId>) -> Option<(usize, usize, bool)> {
@@ -780,7 +789,23 @@ impl Pane for TerminalPane {
     }
 
     fn drain_forwarded_queries(&mut self) -> Vec<crate::host_query::HostQuery> {
-        self.grid.pending_forwarded_queries.drain(..).collect()
+        let queries: Vec<_> = self.grid.pending_forwarded_queries.drain(..).collect();
+        if queries.len() > 1
+            && queries
+                .iter()
+                .all(|query| matches!(query, HostQuery::PaletteRegister { .. }))
+        {
+            let registers = queries
+                .into_iter()
+                .map(|query| match query {
+                    HostQuery::PaletteRegister { index, terminator } => (index, terminator),
+                    _ => unreachable!(),
+                })
+                .collect();
+            vec![HostQuery::PaletteRegisters { registers }]
+        } else {
+            queries
+        }
     }
 
     fn drain_nested_session_messages(&mut self) -> Vec<NestedSessionMessage> {
@@ -1348,7 +1373,59 @@ impl Pane for TerminalPane {
     }
 }
 
+fn palette_query_prefix_len(bytes: &[u8]) -> Option<usize> {
+    let params = bytes.strip_prefix(b"\x1b]4;")?;
+    let digits = params
+        .iter()
+        .take(3)
+        .take_while(|b| b.is_ascii_digit())
+        .count();
+    std::str::from_utf8(&params[..digits])
+        .ok()?
+        .parse::<u8>()
+        .ok()?;
+    let suffix = &params[digits..];
+    let suffix_len = if suffix.starts_with(b";?\x07") {
+        3
+    } else if suffix.starts_with(b";?\x1b\\") {
+        4
+    } else {
+        return None;
+    };
+    Some(4 + digits + suffix_len)
+}
+
 impl TerminalPane {
+    fn batch_pending_palette_queries(&mut self, last_parsed_byte: Option<u8>) {
+        if !matches!(
+            self.grid.pending_forwarded_queries.first(),
+            Some(HostQuery::PaletteRegister { .. })
+        ) {
+            return;
+        }
+
+        let bytes = self.pending_pty_input.make_contiguous();
+        let mut consumed = 0;
+        // VTE dispatches an ST-terminated OSC at ESC, leaving the final backslash.
+        if last_parsed_byte == Some(0x1b) && bytes.first() == Some(&b'\\') {
+            self.vte_parser
+                .advance(&mut self.grid, &bytes[consumed..consumed + 1]);
+            consumed += 1;
+        }
+
+        // Only look ahead through complete palette getters. In particular, a setter
+        // or a locally answered query must not overtake the outstanding host reply.
+        while self.grid.pending_forwarded_queries.len() < MAX_PALETTE_QUERY_BATCH {
+            let Some(len) = palette_query_prefix_len(&bytes[consumed..]) else {
+                break;
+            };
+            self.vte_parser
+                .advance(&mut self.grid, &bytes[consumed..consumed + len]);
+            consumed += len;
+        }
+        self.pending_pty_input.drain(..consumed);
+    }
+
     fn title_with_bell_indicator(&self, title: String) -> String {
         if self.has_bell_notification {
             format!("{} [!]", title)
diff --git a/zellij-server/src/screen.rs b/zellij-server/src/screen.rs
index 0f6c5c982..b62a220e0 100644
--- a/zellij-server/src/screen.rs
+++ b/zellij-server/src/screen.rs
@@ -4131,6 +4131,12 @@ impl Screen {
                     Vec::new()
                 }
             },
+            HostQuery::PaletteRegisters { registers } => registers
+                .iter()
+                .flat_map(|&(index, terminator)| {
+                    self.synthesize_cached_reply(&HostQuery::PaletteRegister { index, terminator })
+                })
+                .collect(),
             HostQuery::ClipboardContent { .. } => query.empty_reply_bytes(),
             // Should not reach here: ColorPaletteMode short-circuits in
             // `forward_host_query` before any cache-fallback path runs.

Validation

The implementation was tested with the following regression coverage:

  • A full 258-query refresh completes in three host exchanges.
  • Fragmented request streams preserve every query.
  • Batching stops before text, setters, other queries, and Kitty graphics sequences.
  • Batches remain bounded at 256 queries.
  • Timeout fallback, cross-pane routing, and stale-reply handling retain their ordering.
  • A fragmented 256-color response batch is collected through one device-attributes barrier.