From 2e27adc4f65b1801702c46c89269ae90356f6552 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 15:23:41 +0000 Subject: [PATCH] fix(compositor): retry queued appd frames until sent and restore sessions in the background Frames queued for a slow appd were sent only on later traffic, so the end of a burst could wait indefinitely. A timer now retries the flush every 20 ms, armed only while frames are queued. weft-appd restores the previous session in its own task, so waiting for the compositor no longer delays clients or shutdown, and the security docs describe the launch refusal and the backlog limit exactly. --- crates/weft-appd/src/main.rs | 41 +++++++++++++++----------- crates/weft-compositor/src/appd_ipc.rs | 41 +++++++++++++++++++++++++- docs/security.md | 2 +- 3 files changed, 64 insertions(+), 20 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 92a4c72..eaf99f5 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -218,25 +218,30 @@ async fn run() -> anyhow::Result<()> { } if let Some(app_ids) = load_session() { - // Apps need the compositor connection; give it a moment to come up. - let compositor = registry.lock().await.compositor_tx.clone(); - if let Some(compositor) = compositor { - let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(10); - while !compositor.is_connected() && tokio::time::Instant::now() < deadline { - tokio::time::sleep(std::time::Duration::from_millis(100)).await; + // Restoring runs beside the request loops, so waiting for the + // compositor delays neither clients nor shutdown. + let registry = Arc::clone(®istry); + tokio::spawn(async move { + // Apps need the compositor connection; give it a moment. + let compositor = registry.lock().await.compositor_tx.clone(); + if let Some(compositor) = compositor { + let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(10); + while !compositor.is_connected() && tokio::time::Instant::now() < deadline { + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + } } - } - tracing::info!(count = app_ids.len(), "restoring previous session"); - for app_id in app_ids { - let _ = dispatch( - crate::ipc::Request::LaunchApp { - app_id, - surface_id: 0, - }, - ®istry, - ) - .await; - } + tracing::info!(count = app_ids.len(), "restoring previous session"); + for app_id in app_ids { + let _ = dispatch( + crate::ipc::Request::LaunchApp { + app_id, + surface_id: 0, + }, + ®istry, + ) + .await; + } + }); } #[cfg(unix)] diff --git a/crates/weft-compositor/src/appd_ipc.rs b/crates/weft-compositor/src/appd_ipc.rs index bcf658c..95f37dc 100644 --- a/crates/weft-compositor/src/appd_ipc.rs +++ b/crates/weft-compositor/src/appd_ipc.rs @@ -9,7 +9,11 @@ use std::{ }; #[cfg(unix)] -use smithay::reexports::calloop::{Interest, Mode, PostAction, channel, generic::Generic}; +use smithay::reexports::calloop::{ + Interest, LoopHandle, Mode, PostAction, RegistrationToken, channel, + generic::Generic, + timer::{TimeoutAction, Timer}, +}; #[cfg(unix)] use smithay::reexports::wayland_server::backend::{ClientId, DisconnectReason}; @@ -47,6 +51,9 @@ pub struct WeftAppdIpc { /// Bytes for appd that did not fit in the socket buffer, sent before /// anything else so frames stay whole. out_buf: Vec, + /// Retries `flush` while `out_buf` holds bytes; armed only then. + flush_retry: Option, + loop_handle: Option>, } #[cfg(unix)] @@ -62,6 +69,8 @@ impl WeftAppdIpc { reported: HashSet::new(), disconnected: None, out_buf: Vec::new(), + flush_retry: None, + loop_handle: None, } } @@ -71,6 +80,7 @@ impl WeftAppdIpc { if self.write_stream.is_some() { self.out_buf.extend_from_slice(&frame); self.flush(); + self.schedule_flush(); } } Err(e) => tracing::warn!(?e, "failed to encode compositor IPC message"), @@ -114,6 +124,32 @@ impl WeftAppdIpc { } } + /// Retries the flush until appd has taken every queued byte, so the + /// last frames of a burst do not wait for unrelated traffic. + fn schedule_flush(&mut self) { + if self.out_buf.is_empty() || self.flush_retry.is_some() { + return; + } + let Some(handle) = &self.loop_handle else { + return; + }; + let retry = Timer::from_duration(std::time::Duration::from_millis(20)); + self.flush_retry = handle + .insert_source(retry, |_, _, state| { + let Some(ipc) = state.appd_ipc.as_mut() else { + return TimeoutAction::Drop; + }; + ipc.flush(); + if ipc.out_buf.is_empty() { + ipc.flush_retry = None; + TimeoutAction::Drop + } else { + TimeoutAction::ToDuration(std::time::Duration::from_millis(20)) + } + }) + .ok(); + } + /// Reports a session's first toplevel to appd. pub fn surface_created(&mut self, session_id: u64) { if self.sessions.contains_key(&session_id) && self.reported.insert(session_id) { @@ -327,6 +363,9 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { tracing::info!(path = %socket_path.display(), "compositor IPC socket open"); + if let Some(ipc) = state.appd_ipc.as_mut() { + ipc.loop_handle = Some(state.loop_handle.clone()); + } // Clients bound to a session report their disconnection here. let (sender, receiver) = channel::channel::<(u64, ClientId)>(); if let Some(ipc) = state.appd_ipc.as_mut() { diff --git a/docs/security.md b/docs/security.md index e16c9b0..f145533 100644 --- a/docs/security.md +++ b/docs/security.md @@ -30,7 +30,7 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF ## Wayland Surface Isolation -Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, before any process starts, rather than starting an app that could never show a window. +Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, rather than starting an app that could never show a window; if the connection drops between that check and the app shell's start, the session is stopped before the app shell starts. Frames for appd that its socket cannot take at once are queued and retried; a backlog beyond 4 MiB, from an appd that stopped reading, ends the connection and with it the sessions' clients. Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement, stacking, focus and close are not yet decided by the compositor per session (all toplevels start at the origin); and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open.