aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAram Drevekenin <aram@poor.dev>2026-04-07 08:53:35 +0200
committerGitHub <noreply@github.com>2026-04-07 08:53:35 +0200
commit93c7ef52495049193a1c4f4f3a13dc8d10bb1cb4 (patch)
treeb3fc5b93bf29dd177c326def0bacd0e7e23d8e48
parentdocs(changelog): underline reset fix (diff)
downloadsl0ppij-93c7ef52495049193a1c4f4f3a13dc8d10bb1cb4.tar.gz
sl0ppij-93c7ef52495049193a1c4f4f3a13dc8d10bb1cb4.tar.xz
sl0ppij-93c7ef52495049193a1c4f4f3a13dc8d10bb1cb4.zip
Fix pty race when opening new panes (#5010)
* fix: race condition when starting panes * rustfmt * add pr
-rw-r--r--CHANGELOG.md1
-rw-r--r--zellij-server/src/screen.rs38
-rw-r--r--zellij-server/src/unit/screen_tests.rs83
3 files changed, 119 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a5ce02e8..38268296 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* fix: encode modifiers in raw bytes for non-Char keys in windows (https://github.com/zellij-org/zellij/pull/4967)
* fix: respect --name flag when creating new panes (https://github.com/zellij-org/zellij/pull/4979)
* fix: do not reset underline with CSI 22 (https://github.com/zellij-org/zellij/pull/4962)
+* fix: race when starting short lived command pane (https://github.com/zellij-org/zellij/pull/5010)
## [0.44.0] - 2026-03-23
* fix: meta key handling in web client (https://github.com/zellij-org/zellij/pull/4376)
diff --git a/zellij-server/src/screen.rs b/zellij-server/src/screen.rs
index f1be251c..81ee975b 100644
--- a/zellij-server/src/screen.rs
+++ b/zellij-server/src/screen.rs
@@ -5042,6 +5042,8 @@ pub(crate) fn screen_thread_main(
// tab_index
let mut pending_events_waiting_for_tab: Vec<ScreenInstruction> = vec![];
let mut pending_events_waiting_for_client: Vec<ScreenInstruction> = vec![];
+ let mut pending_events_waiting_for_pane: HashMap<PaneId, Vec<ScreenInstruction>> =
+ HashMap::new();
let mut plugin_loading_message_cache = HashMap::new();
let mut keybind_intercepts = HashMap::new();
loop {
@@ -5057,13 +5059,22 @@ pub(crate) fn screen_thread_main(
match event {
ScreenInstruction::PtyBytes(pid, vte_bytes) => {
let all_tabs = screen.get_tabs_mut();
+ let mut vte_bytes = Some(vte_bytes);
for tab in all_tabs.values_mut() {
if tab.has_terminal_pid(pid) {
- tab.handle_pty_bytes(pid, vte_bytes)
- .context("failed to process pty bytes")?;
+ if let Some(bytes) = vte_bytes.take() {
+ tab.handle_pty_bytes(pid, bytes)
+ .context("failed to process pty bytes")?;
+ }
break;
}
}
+ if let Some(vte_bytes) = vte_bytes {
+ pending_events_waiting_for_pane
+ .entry(PaneId::Terminal(pid))
+ .or_default()
+ .push(ScreenInstruction::PtyBytes(pid, vte_bytes));
+ }
let _ = screen
.bus
.senders
@@ -5217,6 +5228,11 @@ pub(crate) fn screen_thread_main(
}
},
};
+ if let Some(pending_events) = pending_events_waiting_for_pane.remove(&pid) {
+ for event in pending_events {
+ screen.bus.senders.send_to_screen(event).non_fatal();
+ }
+ }
screen.log_and_report_session_state()?;
screen.render(None)?;
@@ -6165,12 +6181,20 @@ pub(crate) fn screen_thread_main(
));
},
None => {
+ let mut found = false;
for tab in screen.tabs.values_mut() {
if tab.get_all_pane_ids().contains(&id) {
tab.close_pane(id, false, exit_status);
+ found = true;
break;
}
}
+ if !found {
+ pending_events_waiting_for_pane
+ .entry(id)
+ .or_default()
+ .push(ScreenInstruction::ClosePane(id, None, None, exit_status));
+ }
},
}
@@ -6189,12 +6213,20 @@ pub(crate) fn screen_thread_main(
},
ScreenInstruction::HoldPane(id, exit_status, run_command) => {
let is_first_run = false;
+ let mut found = false;
for tab in screen.tabs.values_mut() {
if tab.get_all_pane_ids().contains(&id) {
- tab.hold_pane(id, exit_status, is_first_run, run_command);
+ tab.hold_pane(id, exit_status, is_first_run, run_command.clone());
+ found = true;
break;
}
}
+ if !found {
+ pending_events_waiting_for_pane
+ .entry(id)
+ .or_default()
+ .push(ScreenInstruction::HoldPane(id, exit_status, run_command));
+ }
screen.log_and_report_session_state()?;
},
ScreenInstruction::UpdatePaneName(
diff --git a/zellij-server/src/unit/screen_tests.rs b/zellij-server/src/unit/screen_tests.rs
index cf6c7e3a..a93c63bf 100644
--- a/zellij-server/src/unit/screen_tests.rs
+++ b/zellij-server/src/unit/screen_tests.rs
@@ -8171,3 +8171,86 @@ fn cli_rename_focused_pane_single_char_via_rename_active_pane() {
"Single-char CLI rename should replace the name, not append"
);
}
+
+#[test]
+pub fn pty_bytes_and_hold_pane_buffered_before_new_pane() {
+ // Regression test: when a command exits very quickly (e.g. `zellij run -- echo hello`),
+ // PtyBytes and HoldPane can arrive at the screen thread before NewPane because the async
+ // reader and quit_cb run on separate threads. This test verifies that such early-arriving
+ // events are buffered and replayed once NewPane is processed.
+ let size = Size { cols: 80, rows: 20 };
+ let client_id = 10;
+ let mut mock_screen = MockScreen::new(size);
+ let session_metadata = mock_screen.clone_session_metadata();
+ let screen_thread = mock_screen.run(None, vec![]);
+ let received_server_instructions = Arc::new(Mutex::new(vec![]));
+ let server_receiver = mock_screen.server_receiver.take().unwrap();
+ let server_thread = log_actions_in_thread!(
+ received_server_instructions,
+ ServerInstruction::KillSession,
+ server_receiver
+ );
+
+ // The initial layout creates pane id 0. We will use pane id 2 for the new pane
+ // (id 1 is used by the plugin in the initial layout).
+ let new_pane_id = 2;
+
+ // Simulate the race: send PtyBytes for the new pane BEFORE NewPane
+ let _ = mock_screen.to_screen.send(ScreenInstruction::PtyBytes(
+ new_pane_id,
+ "hello\r\n".as_bytes().to_vec(),
+ ));
+
+ // Send HoldPane before NewPane as well
+ let run_command = RunCommand {
+ command: PathBuf::from("echo"),
+ args: vec!["hello".to_string()],
+ hold_on_close: true,
+ ..Default::default()
+ };
+ let _ = mock_screen.to_screen.send(ScreenInstruction::HoldPane(
+ PaneId::Terminal(new_pane_id),
+ Some(0),
+ run_command,
+ ));
+
+ // Small sleep to ensure the above messages are processed (and buffered) first
+ std::thread::sleep(std::time::Duration::from_millis(100));
+
+ // Now send NewPane — this should replay the buffered PtyBytes and HoldPane
+ let _ = mock_screen.to_screen.send(ScreenInstruction::NewPane(
+ PaneId::Terminal(new_pane_id),
+ Some("echo hello".to_string()),
+ None, // hold_for_command
+ None, // invoked_with
+ NewPanePlacement::default(),
+ false, // start_suppressed
+ ClientTabIndexOrPaneId::ClientId(client_id),
+ None, // completion_tx
+ false, // set_blocking
+ ));
+ std::thread::sleep(std::time::Duration::from_millis(100));
+
+ // Use DumpScreen to verify the pane received the bytes
+ let cli_action = CliAction::DumpScreen {
+ path: Some(PathBuf::from("/tmp/dump_early_bytes")),
+ full: true,
+ pane_id: None,
+ ansi: false,
+ };
+ send_cli_action_to_server(&session_metadata, cli_action, client_id);
+ std::thread::sleep(std::time::Duration::from_millis(100));
+
+ mock_screen.teardown(vec![server_thread, screen_thread]);
+
+ let filesystem = mock_screen.os_input.fake_filesystem.lock().unwrap();
+ let dumped = filesystem
+ .values()
+ .next()
+ .expect("DumpScreen should have written a file");
+ assert!(
+ dumped.contains("hello"),
+ "Pane should contain the buffered output 'hello', but got: {:?}",
+ dumped
+ );
+}