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(gui): attach the session server before reopening a window

RoversXde0e81bf9dab
diff --git a/wezterm-gui/src/frontend.rs b/wezterm-gui/src/frontend.rs
index 6e6fc961..b2a18136 100644
--- a/wezterm-gui/src/frontend.rs
+++ b/wezterm-gui/src/frontend.rs
@@ -817,6 +817,9 @@ impl GuiFrontEnd {
async fn materialize_space_window_for_app(
space_id: &str,
) -> anyhow::Result<(MuxWindowId, bool)> {
+ // With no window left the host was detached, and the thread asked
+ // for may be live in it still.
+ crate::local_sessions::attach_host_if_detached().await;
let mux = Mux::get();
let thread_id = crate::workspace_threads::ensure_active_thread_for_space(space_id)
.ok_or_else(|| anyhow!("failed to ensure active thread for Space {space_id}"))?;
diff --git a/wezterm-gui/src/local_sessions.rs b/wezterm-gui/src/local_sessions.rs
index e6dbf965..625d4aff 100644
--- a/wezterm-gui/src/local_sessions.rs
+++ b/wezterm-gui/src/local_sessions.rs
@@ -235,6 +235,34 @@ pub(crate) fn is_host_domain_id(id: mux::domain::DomainId) -> bool {
.is_some_and(|domain| domain.domain_name() == host)
}
+/// Attach the host again if the last window closing detached it (see
+/// `GuiFrontEnd::forget_known_window`) and the process lived on. A window
+/// opened then has to find the host's terminals mirrored before it decides
+/// what to spawn, as the first window does: deciding first took a live
+/// thread for a cold one and spawned its saved layout beside the terminals
+/// the spawn's own attach then brought back. A failure is left to that
+/// spawn, which attaches by itself.
+pub(crate) async fn attach_host_if_detached() {
+ let Some(name) = host_domain_name() else {
+ return;
+ };
+ let Some(domain) = Mux::get().get_domain_by_name(&name) else {
+ return;
+ };
+ let Some(client) = domain.downcast_ref::<ClientDomain>() else {
+ return;
+ };
+ if client.state() == mux::domain::DomainState::Attached {
+ return;
+ }
+ if let Err(err) = client
+ .attach_with_ui(None, mux::connui::ConnectionUI::new_headless())
+ .await
+ {
+ log::warn!("local sessions: attaching the session server for a new window: {err:#}");
+ }
+}
+
/// The host could not be attached at launch: run this launch in process.
/// The domain stays registered but detached; nothing retries it, since a
/// host attached later would find windows already classified as local.