fix(compositor): buffer appd-bound frames and ignore stale session disconnects

A full socket buffer toward a stalled appd closed the connection and
with it every app. Frames for appd are now queued whole and flushed on
each send and read, and only a backlog beyond the frame limit or a
write error ends the connection. A disconnect report is applied only
when it comes from the session's current client, so a client closed
with an earlier appd cannot remove the mapping of a new session that
reused its ID. A failure to watch a new appd connection no longer
leaves the compositor refusing every later one.

weft-appd refuses a launch with 503 before anything starts while the
compositor is not connected, and waits up to 10 s for the connection
before restoring the previous session.
This commit is contained in:
Marco Allegretti 2026-10-09 15:20:29 +00:00
parent 4c778330a3
commit d6a2f94a1d
No known key found for this signature in database
4 changed files with 142 additions and 34 deletions

View file

@ -218,6 +218,14 @@ 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;
}
}
tracing::info!(count = app_ids.len(), "restoring previous session");
for app_id in app_ids {
let _ = dispatch(
@ -438,8 +446,20 @@ pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response {
// The package is resolved and its capabilities granted before a
// session exists; a package this host cannot satisfy is refused,
// not started.
let compositor_down = registry
.lock()
.await
.compositor_tx
.as_ref()
.is_some_and(|tx| !tx.is_connected());
let launch = if !weft_ipc_types::package::is_valid_app_id(&app_id) {
Err(grants::Refusal::new(400, "invalid app ID"))
} else if compositor_down {
// An app started now could never show a window.
Err(grants::Refusal::new(
503,
"weft-compositor is not connected",
))
} else if std::env::var("WEFT_RUNTIME_BIN").is_ok() {
resolve_launch(app_id.clone()).await.map(Some)
} else {
@ -1242,7 +1262,7 @@ mod tests {
}
#[tokio::test(flavor = "current_thread")]
async fn without_a_connected_compositor_the_app_shell_is_not_started() {
async fn without_a_connected_compositor_nothing_is_started() {
use std::os::unix::fs::PermissionsExt;
let _env = env_lock().lock().await;
use_test_runtime_dir();
@ -1283,8 +1303,7 @@ mod tests {
tx,
std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
));
let mut rx = registry.lock().await.subscribe();
let _ = dispatch(
let refused = dispatch(
Request::LaunchApp {
app_id: "org.example.nocomp".into(),
surface_id: 0,
@ -1292,16 +1311,7 @@ mod tests {
&registry,
)
.await;
let stopped = tokio::time::timeout(std::time::Duration::from_secs(10), async {
while !matches!(
rx.recv().await,
Ok(Response::AppState {
state: AppStateKind::Stopped,
..
})
) {}
})
.await;
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
let started = std::fs::read_to_string(&log).unwrap_or_default();
for (key, value) in prior {
@ -1314,9 +1324,12 @@ mod tests {
}
}
let _ = std::fs::remove_dir_all(&dir);
assert!(stopped.is_ok(), "session did not stop");
// Only the runtime started; no app shell, and nothing was queued.
assert_eq!(started.lines().count(), 1, "{started}");
assert!(
matches!(refused, Response::Error { code: 503, .. }),
"{refused:?}"
);
// Nothing started and nothing was queued for the compositor.
assert_eq!(started, "");
assert!(compositor.try_recv().is_err());
}

View file

@ -43,7 +43,10 @@ pub struct WeftAppdIpc {
/// Sessions whose first toplevel was reported to appd.
reported: HashSet<u64>,
/// Where clients report their session's disconnection.
disconnected: Option<channel::Sender<u64>>,
disconnected: Option<channel::Sender<(u64, ClientId)>>,
/// Bytes for appd that did not fit in the socket buffer, sent before
/// anything else so frames stay whole.
out_buf: Vec<u8>,
}
#[cfg(unix)]
@ -58,28 +61,59 @@ impl WeftAppdIpc {
sessions: HashMap::new(),
reported: HashSet::new(),
disconnected: None,
out_buf: Vec::new(),
}
}
pub fn send(&mut self, msg: &CompositorToAppd) {
use std::io::Write;
let Some(stream) = &mut self.write_stream else {
return;
};
match frame_encode(msg) {
Ok(frame) => {
if stream.write_all(&frame).is_err() {
// A partly written frame breaks the stream's framing:
// end the connection, whose read side then cleans up.
tracing::warn!("compositor IPC write failed; closing the appd connection");
let _ = stream.shutdown(std::net::Shutdown::Both);
self.write_stream = None;
if self.write_stream.is_some() {
self.out_buf.extend_from_slice(&frame);
self.flush();
}
}
Err(e) => tracing::warn!(?e, "failed to encode compositor IPC message"),
}
}
/// Writes what appd's socket accepts now; the rest waits for the next
/// send or read. A backlog appd never drains, or a write error, ends the
/// connection, whose read side then cleans up.
fn flush(&mut self) {
use std::io::Write;
let Some(stream) = &mut self.write_stream else {
self.out_buf.clear();
return;
};
while !self.out_buf.is_empty() {
match stream.write(&self.out_buf) {
Ok(0) => break,
Ok(n) => {
self.out_buf.drain(..n);
}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => break,
Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
Err(e) => {
tracing::warn!(
?e,
"compositor IPC write failed; closing the appd connection"
);
let _ = stream.shutdown(std::net::Shutdown::Both);
self.write_stream = None;
self.out_buf.clear();
return;
}
}
}
if self.out_buf.len() > MAX_FRAME_LEN {
tracing::warn!("weft-appd is not reading compositor IPC; closing the connection");
let _ = stream.shutdown(std::net::Shutdown::Both);
self.write_stream = None;
self.out_buf.clear();
}
}
/// 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) {
@ -176,6 +210,7 @@ impl WeftAppdIpc {
fn disconnect(&mut self) -> Vec<ClientId> {
self.connected = false;
self.write_stream = None;
self.out_buf.clear();
self.read_buf.clear();
self.fds.clear();
self.reported.clear();
@ -293,15 +328,18 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> {
tracing::info!(path = %socket_path.display(), "compositor IPC socket open");
// Clients bound to a session report their disconnection here.
let (sender, receiver) = channel::channel::<u64>();
let (sender, receiver) = channel::channel::<(u64, ClientId)>();
if let Some(ipc) = state.appd_ipc.as_mut() {
ipc.disconnected = Some(sender);
}
state
.loop_handle
.insert_source(receiver, |event, _, state| {
if let channel::Event::Msg(session_id) = event
// Only the session's current client: a client closed when its
// appd went away may report after a new appd reused the ID.
if let channel::Event::Msg((session_id, client)) = event
&& let Some(ipc) = state.appd_ipc.as_mut()
&& ipc.sessions.get(&session_id) == Some(&client)
{
ipc.sessions.remove(&session_id);
ipc.reported.remove(&session_id);
@ -343,7 +381,7 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> {
ipc.fds.clear();
}
stream.set_nonblocking(true).ok();
let _ = handle.insert_source(
let registered = handle.insert_source(
Generic::new(stream, Interest::READ, Mode::Edge),
|_, stream, state| {
// Safety: calloop wraps the fd in NoIoDrop to prevent
@ -356,6 +394,9 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> {
for msg in messages {
handle_message(state, msg);
}
if let Some(ipc) = state.appd_ipc.as_mut() {
ipc.flush();
}
if eof {
tracing::info!(
"weft-appd disconnected from compositor IPC; \
@ -378,6 +419,12 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> {
}
},
);
if let Err(e) = registered {
tracing::warn!(?e, "cannot watch the appd IPC connection");
if let Some(ipc) = state.appd_ipc.as_mut() {
ipc.disconnect();
}
}
}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => break,
Err(e) => tracing::warn!(?e, "accept error on compositor IPC socket"),
@ -502,6 +549,54 @@ mod tests {
assert!(ipc.fds.is_empty());
}
#[test]
fn a_slow_appd_gets_whole_frames_later_instead_of_losing_its_apps() {
let (appd, compositor) = UnixStream::pair().unwrap();
compositor.set_nonblocking(true).unwrap();
let mut ipc = WeftAppdIpc::new(PathBuf::from("/nonexistent"));
ipc.write_stream = Some(compositor);
ipc.connected = true;
// More than the socket buffer holds while appd is not reading.
let count = 20_000u64;
for session_id in 0..count {
ipc.send(&CompositorToAppd::SurfaceReady { session_id });
}
assert!(
ipc.write_stream.is_some(),
"a full buffer closed the connection"
);
assert!(!ipc.out_buf.is_empty());
// appd reads; every flush moves more, and every frame arrives whole.
appd.set_nonblocking(true).unwrap();
let mut received = Vec::new();
let mut buf = [0u8; 65536];
loop {
ipc.flush();
use std::io::Read;
match (&appd).read(&mut buf) {
Ok(n) => received.extend_from_slice(&buf[..n]),
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
if ipc.out_buf.is_empty() {
break;
}
}
Err(e) => panic!("{e}"),
}
}
let mut frames = 0u64;
while !received.is_empty() {
let len = u32::from_le_bytes(received[..4].try_into().unwrap()) as usize;
let frame: CompositorToAppd = frame_decode(&received[..4 + len]).unwrap();
assert!(
matches!(frame, CompositorToAppd::SurfaceReady { session_id } if session_id == frames)
);
received.drain(..4 + len);
frames += 1;
}
assert_eq!(frames, count);
}
#[test]
fn an_undecodable_frame_ends_the_connection() {
let (appd, compositor) = UnixStream::pair().unwrap();

View file

@ -64,7 +64,7 @@ pub struct SessionBinding {
pub session_id: u64,
pub app_id: String,
/// Reports the session's disconnection to the event loop.
pub disconnected: smithay::reexports::calloop::channel::Sender<u64>,
pub disconnected: smithay::reexports::calloop::channel::Sender<(u64, ClientId)>,
}
/// Whether `client` connected through the display socket rather than
@ -77,9 +77,9 @@ pub fn outside_sessions(client: &Client) -> bool {
impl ClientData for WeftClientState {
fn initialized(&self, _client_id: ClientId) {}
fn disconnected(&self, _client_id: ClientId, _reason: DisconnectReason) {
fn disconnected(&self, client_id: ClientId, _reason: DisconnectReason) {
if let Some(binding) = &self.session {
let _ = binding.disconnected.send(binding.session_id);
let _ = binding.disconnected.send((binding.session_id, client_id));
}
}
}

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 while appd is not connected to the compositor, 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, before any process starts, rather than starting an app that could never show a window.
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.