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.
This commit is contained in:
Marco Allegretti 2026-10-09 15:23:41 +00:00
parent d6a2f94a1d
commit 2e27adc4f6
No known key found for this signature in database
3 changed files with 64 additions and 20 deletions

View file

@ -218,7 +218,11 @@ async fn run() -> anyhow::Result<()> {
}
if let Some(app_ids) = load_session() {
// Apps need the compositor connection; give it a moment to come up.
// Restoring runs beside the request loops, so waiting for the
// compositor delays neither clients nor shutdown.
let registry = Arc::clone(&registry);
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);
@ -237,6 +241,7 @@ async fn run() -> anyhow::Result<()> {
)
.await;
}
});
}
#[cfg(unix)]

View file

@ -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<u8>,
/// Retries `flush` while `out_buf` holds bytes; armed only then.
flush_retry: Option<RegistrationToken>,
loop_handle: Option<LoopHandle<'static, WeftCompositorState>>,
}
#[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() {

View file

@ -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.