From d9d9d24205a76fd6ceea6d62f17063abcf9119ff Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 09:11:35 +0000 Subject: [PATCH 1/4] fix(appd): make the IPC relay part of the session it serves dispatch opened each session's IPC socket and registered its sender before the supervisor existed, and nothing released them: a relay whose runtime never connected waited forever with its socket file in place, and every stopped session left its sender registered, so app messages for it were still accepted and queued. The supervisor now opens the relay after its early checks, and the session owns it: every exit path stops the relay task, removes the socket and unregisters the sender. A session whose socket cannot be opened does not start. The relay accepts a single connection. A dispatch-level test launches a package whose runtime reports ready and exits without connecting; it requires the stopped session to leave no sender and no socket, and fails against the previous code. The Counter and Notes checks pass end to end. --- crates/weft-appd/src/main.rs | 123 ++++++++++++++++++++++++++------ crates/weft-appd/src/runtime.rs | 72 ++++++++++++++----- docs/architecture.md | 2 +- 3 files changed, 156 insertions(+), 41 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 8857ada..83f0eae 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -468,27 +468,11 @@ pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response { }); return Response::LaunchAck { session_id, app_id }; } - let ipc_socket = session_ipc_socket_path(session_id); - let broadcast = registry.lock().await.broadcast().clone(); - if let Some(ref sock_path) = ipc_socket - && let Some(tx) = - runtime::spawn_ipc_relay(session_id, sock_path.clone(), broadcast).await - { - registry.lock().await.register_ipc_sender(session_id, tx); - } let reg = Arc::clone(registry); let aid = app_id.clone(); tokio::spawn(async move { - if let Err(e) = runtime::supervise( - session_id, - &aid, - grants, - reg, - abort_rx, - compositor_tx, - ipc_socket, - ) - .await + if let Err(e) = + runtime::supervise(session_id, &aid, grants, reg, abort_rx, compositor_tx).await { tracing::warn!(session_id, error = %e, "runtime supervisor error"); } @@ -634,6 +618,16 @@ mod tests { static ENV_LOCK: std::sync::OnceLock> = std::sync::OnceLock::new(); + /// Points XDG_RUNTIME_DIR at a private directory for the session's IPC + /// socket. Callers hold env_lock. + fn use_test_runtime_dir() { + let dir = + std::env::temp_dir().join(format!("weft_test_runtime_dir_{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + // SAFETY: callers hold env_lock on a current_thread runtime. + unsafe { std::env::set_var("XDG_RUNTIME_DIR", &dir) }; + } + pub(crate) fn env_lock() -> &'static tokio::sync::Mutex<()> { ENV_LOCK.get_or_init(|| tokio::sync::Mutex::new(())) } @@ -1003,6 +997,84 @@ mod tests { } #[cfg(unix)] + #[tokio::test(flavor = "current_thread")] + async fn a_stopped_session_releases_its_ipc_relay() { + use std::os::unix::fs::PermissionsExt; + let _env = env_lock().lock().await; + use_test_runtime_dir(); + let dir = std::env::temp_dir().join(format!("weft_test_relay_{}", std::process::id())); + let app = dir.join("store/org.example.relay"); + std::fs::create_dir_all(&app).unwrap(); + std::fs::write( + app.join("wapp.toml"), + "[package]\nid = \"org.example.relay\"\n", + ) + .unwrap(); + // The runtime reports ready and exits without connecting to the relay. + let child = dir.join("child.sh"); + std::fs::write( + &child, + "#!/bin/sh\necho READY $WEFT_READY_TOKEN\nexec sleep 1\n", + ) + .unwrap(); + std::fs::set_permissions(&child, std::fs::Permissions::from_mode(0o755)).unwrap(); + let vars = [ + ("WEFT_RUNTIME_BIN", child.as_os_str()), + ("WEFT_APP_SHELL_BIN", child.as_os_str()), + ("WEFT_DISABLE_CGROUP", std::ffi::OsStr::new("1")), + ("WEFT_APP_STORE", dir.join("store").into_os_string().leak()), + ]; + let prior: Vec<_> = vars + .iter() + .map(|(k, _)| (*k, std::env::var_os(k))) + .collect(); + for (key, value) in &vars { + // SAFETY: env_lock is held on a current_thread runtime. + unsafe { std::env::set_var(key, value) }; + } + + let registry = make_registry(); + let mut rx = registry.lock().await.subscribe(); + let ack = dispatch( + Request::LaunchApp { + app_id: "org.example.relay".into(), + surface_id: 0, + }, + ®istry, + ) + .await; + let Response::LaunchAck { session_id, .. } = ack else { + panic!("expected LaunchAck, got {ack:?}"); + }; + let stopped = tokio::time::timeout(std::time::Duration::from_secs(10), async { + loop { + if let Ok(Response::AppState { + session_id: id, + state: AppStateKind::Stopped, + }) = rx.recv().await + && id == session_id + { + break; + } + } + }) + .await; + + for (key, value) in prior { + // SAFETY: as above. + unsafe { + match value { + Some(v) => std::env::set_var(key, v), + None => std::env::remove_var(key), + } + } + } + let _ = std::fs::remove_dir_all(&dir); + assert!(stopped.is_ok(), "session did not stop"); + assert!(registry.lock().await.ipc_sender_for(session_id).is_none()); + assert!(!session_ipc_socket_path(session_id).unwrap().exists()); + } + #[tokio::test(flavor = "current_thread")] async fn supervisor_transitions_through_ready_to_stopped() { use std::os::unix::fs::PermissionsExt; @@ -1040,6 +1112,8 @@ mod tests { let session_id = registry.lock().await.launch("test.app"); let abort_rx = registry.lock().await.register_abort(session_id); + use_test_runtime_dir(); + runtime::supervise( session_id, "test.app", @@ -1047,7 +1121,6 @@ mod tests { Arc::clone(®istry), abort_rx, None, - None, ) .await .unwrap(); @@ -1056,6 +1129,10 @@ mod tests { registry.lock().await.state(session_id), AppStateKind::Stopped )); + // The session's IPC relay ends with it, although the test runtime + // never connected to it. + assert!(registry.lock().await.ipc_sender_for(session_id).is_none()); + assert!(!session_ipc_socket_path(session_id).unwrap().exists()); let notification = tokio::time::timeout(std::time::Duration::from_millis(200), rx.recv()).await; @@ -1110,6 +1187,8 @@ mod tests { registry.lock().await.terminate(session_id); + use_test_runtime_dir(); + runtime::supervise( session_id, "test.abort.startup", @@ -1117,7 +1196,6 @@ mod tests { Arc::clone(®istry), abort_rx, None, - None, ) .await .unwrap(); @@ -1159,6 +1237,8 @@ mod tests { let session_id = registry.lock().await.launch("test.spawn.fail"); let abort_rx = registry.lock().await.register_abort(session_id); + use_test_runtime_dir(); + runtime::supervise( session_id, "test.spawn.fail", @@ -1166,7 +1246,6 @@ mod tests { Arc::clone(®istry), abort_rx, None, - None, ) .await .unwrap(); @@ -1316,6 +1395,7 @@ mod tests { let session_id = registry.lock().await.launch("test.app"); let abort_rx = registry.lock().await.register_abort(session_id); let started = std::time::Instant::now(); + use_test_runtime_dir(); tokio::time::timeout( std::time::Duration::from_secs(20), runtime::supervise( @@ -1325,7 +1405,6 @@ mod tests { Arc::clone(®istry), abort_rx, None, - None, ), ) .await diff --git a/crates/weft-appd/src/runtime.rs b/crates/weft-appd/src/runtime.rs index 516d31d..2478ab2 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -8,21 +8,40 @@ use crate::Registry; use crate::compositor_client::CompositorSender; use crate::ipc::{AppStateKind, Response}; +/// The per-session relay between the component's IPC socket and the app +/// bridge. It belongs to the session: closing it stops the relay task and +/// removes the socket, whether or not the component ever connected. +pub(crate) struct IpcRelay { + task: tokio::task::JoinHandle<()>, + socket_path: PathBuf, +} + +impl IpcRelay { + fn close(self) { + self.task.abort(); + let _ = std::fs::remove_file(&self.socket_path); + } +} + +/// Listens on `socket_path` for the session's component and relays +/// newline-delimited messages between it and the returned sender (towards +/// the component) and the broadcast channel (from the component). #[cfg(unix)] -pub(crate) async fn spawn_ipc_relay( +pub(crate) fn spawn_ipc_relay( session_id: u64, socket_path: PathBuf, broadcast: tokio::sync::broadcast::Sender, -) -> Option> { +) -> std::io::Result<(tokio::sync::mpsc::Sender, IpcRelay)> { let _ = std::fs::remove_file(&socket_path); - let listener = tokio::net::UnixListener::bind(&socket_path).ok()?; + let listener = tokio::net::UnixListener::bind(&socket_path)?; let (html_to_wasm_tx, mut html_to_wasm_rx) = tokio::sync::mpsc::channel::(64); - tokio::spawn(async move { + let task = tokio::spawn(async move { let Ok((stream, _)) = listener.accept().await else { tracing::warn!(session_id, "IPC relay: failed to accept connection"); - let _ = std::fs::remove_file(&socket_path); return; }; + // One component per session: no further connections are accepted. + drop(listener); tracing::debug!(session_id, "IPC relay: component connected"); let (reader, writer) = tokio::io::split(stream); let mut reader = BufReader::new(reader); @@ -55,18 +74,17 @@ pub(crate) async fn spawn_ipc_relay( } } } - let _ = std::fs::remove_file(&socket_path); }); - Some(html_to_wasm_tx) + Ok((html_to_wasm_tx, IpcRelay { task, socket_path })) } #[cfg(not(unix))] -pub(crate) async fn spawn_ipc_relay( +pub(crate) fn spawn_ipc_relay( _session_id: u64, _socket_path: PathBuf, _broadcast: tokio::sync::broadcast::Sender, -) -> Option> { - None +) -> std::io::Result<(tokio::sync::mpsc::Sender, IpcRelay)> { + Err(std::io::ErrorKind::Unsupported.into()) } const READY_TIMEOUT: Duration = Duration::from_secs(30); @@ -211,7 +229,6 @@ pub(crate) async fn supervise( registry: Registry, abort_rx: tokio::sync::oneshot::Receiver<()>, compositor_tx: Option, - ipc_socket_path: Option, ) -> anyhow::Result<()> { let mut abort_rx = abort_rx; let bin = match std::env::var("WEFT_RUNTIME_BIN") { @@ -234,6 +251,22 @@ pub(crate) async fn supervise( } }; + let Some(ipc_socket_path) = crate::session_ipc_socket_path(session_id) else { + tracing::warn!(session_id, %app_id, "no runtime directory for the IPC socket"); + return stop_unstarted(®istry, session_id).await; + }; + let broadcast = registry.lock().await.broadcast().clone(); + let relay = match spawn_ipc_relay(session_id, ipc_socket_path.clone(), broadcast) { + Ok((tx, relay)) => { + registry.lock().await.register_ipc_sender(session_id, tx); + relay + } + Err(e) => { + tracing::warn!(session_id, %app_id, error = %e, "cannot open the IPC socket"); + return stop_unstarted(®istry, session_id).await; + } + }; + let (mount_orch, store_override) = crate::mount::MountOrchestrator::mount_if_needed(app_id, session_id); @@ -254,9 +287,7 @@ pub(crate) async fn supervise( .env(READY_TOKEN_ENV, &token) .kill_on_drop(true); - if let Some(ref sock) = ipc_socket_path { - cmd.arg("--ipc-socket").arg(sock); - } + cmd.arg("--ipc-socket").arg(&ipc_socket_path); if let Some(ref root) = store_override { cmd.env("WEFT_APP_STORE", root); @@ -278,6 +309,7 @@ pub(crate) async fn supervise( runtime: None, app_shell: None, portal, + relay: Some(relay), mount: mount_orch, compositor_tx, surface_announced: false, @@ -394,15 +426,15 @@ async fn stop_unstarted(registry: &Registry, session_id: u64) -> anyhow::Result< Ok(()) } -/// The processes, portal, compositor association and image mount a session -/// owns. `settle` releases all of them on every exit path: spawn failure, -/// readiness failure, timeout, abort and child exit. The IPC relay is not yet -/// owned here. +/// The processes, IPC relay, portal, compositor association and image mount +/// a session owns. `settle` releases all of them on every exit path: spawn +/// failure, readiness failure, timeout, abort and child exit. struct OwnedSession { session_id: u64, runtime: Option, app_shell: Option, portal: Option<(PathBuf, tokio::process::Child)>, + relay: Option, mount: crate::mount::MountOrchestrator, compositor_tx: Option, surface_announced: bool, @@ -427,7 +459,11 @@ impl OwnedSession { } self.mount.umount(); kill_portal(self.portal).await; + if let Some(relay) = self.relay { + relay.close(); + } let mut reg = registry.lock().await; + reg.remove_ipc_sender(session_id); reg.set_state(session_id, AppStateKind::Stopped); reg.remove_abort_sender(session_id); let _ = reg.broadcast().send(Response::AppState { diff --git a/docs/architecture.md b/docs/architecture.md index 7bf7e2b..ee5dc2e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -20,7 +20,7 @@ Per-application Servo host. Spawned by `weft-appd` after the Wasm runtime signal ### weft-appd -Session supervisor. Listens on a Unix socket (MessagePack protocol) and a WebSocket port (JSON). For each session: spawns `weft-runtime` and waits for its READY, spawns `weft-app-shell` (`WEFT_APP_SHELL_BIN` is required) and waits for its READY, and only then reports the session running (`APP_READY`). A child reports readiness by printing `READY ` on stdout, where the token is a random per-session value appd passes in `WEFT_READY_TOKEN`; Wasm guests and pages share those stdout streams but cannot read the token, so their output cannot fake readiness. Each wait times out after 30 seconds. If either child fails to start, fails to become ready or exits, or the session is terminated, the whole session is stopped: both children, the file portal, the compositor association and any image mount are released. Manages the per-session IPC relay and applies cgroup resource limits to the runtime via systemd-run when available. +Session supervisor. Listens on a Unix socket (MessagePack protocol) and a WebSocket port (JSON). For each session: spawns `weft-runtime` and waits for its READY, spawns `weft-app-shell` (`WEFT_APP_SHELL_BIN` is required) and waits for its READY, and only then reports the session running (`APP_READY`). A child reports readiness by printing `READY ` on stdout, where the token is a random per-session value appd passes in `WEFT_READY_TOKEN`; Wasm guests and pages share those stdout streams but cannot read the token, so their output cannot fake readiness. Each wait times out after 30 seconds. If either child fails to start, fails to become ready or exits, or the session is terminated, the whole session is stopped: both children, the IPC relay (its task, socket and registered sender), the file portal, the compositor association and any image mount are released. The IPC relay is created with the session and accepts one connection, from the session's runtime. appd applies cgroup resource limits to the runtime via systemd-run when available. ### weft-runtime From 26bd5b5079a1d81eef523b2b49f75c6eb33a4fa1 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 09:18:39 +0000 Subject: [PATCH 2/4] fix(appd): end sessions whose IPC connection closes and never block on it IPC_FORWARD held the registry lock while awaiting a send to the component; with a full queue it stalled appd, and once the relay was closed it locked the registry again and deadlocked. It now sends without waiting and reports an error when the session has no relay or its queue is full. A relay whose component disconnected left the session running with no way to exchange messages. The session now stops when its IPC connection closes. The relay releases its task and socket when dropped, so a cancelled supervisor leaks neither, and the socket is created with mode 0600. New tests fail against the previous code: IPC_FORWARD against a missing relay and a full queue must answer at once, and a runtime that closes its IPC connection while still running must end its session. The test runtime directory lives under target/ instead of one directory per run. --- crates/weft-appd/src/main.rs | 147 ++++++++++++++++++++++++++++---- crates/weft-appd/src/runtime.rs | 21 ++++- docs/architecture.md | 2 +- 3 files changed, 150 insertions(+), 20 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 83f0eae..ccd71c9 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -514,15 +514,31 @@ pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response { session_id, payload, } => { - if let Some(tx) = registry.lock().await.ipc_sender_for(session_id) - && tx.send(payload).await.is_err() - { - tracing::warn!(session_id, "IPC relay sender closed"); - registry.lock().await.remove_ipc_sender(session_id); - } - Response::AppState { - session_id, - state: ipc::AppStateKind::Running, + // The registry lock is released before sending; a component that + // stops reading must not stall appd. + let sender = registry.lock().await.ipc_sender_for(session_id); + let Some(tx) = sender else { + return Response::Error { + code: 1, + message: format!("session {session_id} has no IPC relay"), + }; + }; + match tx.try_send(payload) { + Ok(()) => Response::AppState { + session_id, + state: ipc::AppStateKind::Running, + }, + Err(tokio::sync::mpsc::error::TrySendError::Full(_)) => Response::Error { + code: 1, + message: format!("session {session_id} is not reading messages"), + }, + Err(tokio::sync::mpsc::error::TrySendError::Closed(_)) => { + registry.lock().await.remove_ipc_sender(session_id); + Response::Error { + code: 1, + message: format!("session {session_id} has no IPC relay"), + } + } } } Request::PanelGesture { @@ -622,9 +638,10 @@ mod tests { /// socket. Callers hold env_lock. fn use_test_runtime_dir() { let dir = - std::env::temp_dir().join(format!("weft_test_runtime_dir_{}", std::process::id())); + std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/test-runtime-dir"); std::fs::create_dir_all(&dir).unwrap(); - // SAFETY: callers hold env_lock on a current_thread runtime. + // SAFETY: env_lock serialises the tests that change the environment, + // and std serialises the environment accesses themselves. unsafe { std::env::set_var("XDG_RUNTIME_DIR", &dir) }; } @@ -997,6 +1014,106 @@ mod tests { } #[cfg(unix)] + #[tokio::test] + async fn ipc_forward_never_waits_on_a_component() { + let registry = make_registry(); + let forward = |session_id| Request::IpcForward { + session_id, + payload: "x".into(), + }; + let reply = dispatch(forward(7), ®istry).await; + assert!(matches!(reply, Response::Error { .. }), "{reply:?}"); + + let (tx, _rx) = tokio::sync::mpsc::channel(1); + registry.lock().await.register_ipc_sender(7, tx); + assert!(matches!( + dispatch(forward(7), ®istry).await, + Response::AppState { .. } + )); + // The queue is full and nothing reads it: the reply comes at once. + let reply = tokio::time::timeout( + std::time::Duration::from_secs(1), + dispatch(forward(7), ®istry), + ) + .await + .expect("IPC_FORWARD blocked"); + assert!(matches!(reply, Response::Error { .. }), "{reply:?}"); + } + + #[tokio::test(flavor = "current_thread")] + async fn a_closed_ipc_connection_ends_the_session() { + use std::os::unix::fs::PermissionsExt; + let _env = env_lock().lock().await; + use_test_runtime_dir(); + let dir = std::env::temp_dir().join(format!("weft_test_ipc_close_{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + // The runtime connects to its IPC socket ($4), closes the connection + // and keeps running; the app shell just reports ready. + let runtime = dir.join("runtime.sh"); + std::fs::write( + &runtime, + "#!/bin/sh\necho READY $WEFT_READY_TOKEN\n\ + python3 -c 'import socket,sys; s=socket.socket(socket.AF_UNIX); \ + s.connect(sys.argv[1]); s.close()' \"$4\"\nexec sleep 30\n", + ) + .unwrap(); + let shell = dir.join("shell.sh"); + std::fs::write( + &shell, + "#!/bin/sh\necho READY $WEFT_READY_TOKEN\nexec sleep 30\n", + ) + .unwrap(); + for script in [&runtime, &shell] { + std::fs::set_permissions(script, std::fs::Permissions::from_mode(0o755)).unwrap(); + } + let prior: Vec<_> = [ + "WEFT_RUNTIME_BIN", + "WEFT_APP_SHELL_BIN", + "WEFT_DISABLE_CGROUP", + ] + .iter() + .map(|k| (*k, std::env::var_os(k))) + .collect(); + // SAFETY: env_lock serialises the tests that change the environment. + unsafe { + std::env::set_var("WEFT_RUNTIME_BIN", &runtime); + std::env::set_var("WEFT_APP_SHELL_BIN", &shell); + std::env::set_var("WEFT_DISABLE_CGROUP", "1"); + } + + let registry = make_registry(); + let session_id = registry.lock().await.launch("test.app"); + let abort_rx = registry.lock().await.register_abort(session_id); + let finished = tokio::time::timeout( + std::time::Duration::from_secs(15), + runtime::supervise( + session_id, + "test.app", + grants::SessionGrants::default(), + Arc::clone(®istry), + abort_rx, + None, + ), + ) + .await; + + for (key, value) in prior { + // SAFETY: as above. + unsafe { + match value { + Some(v) => std::env::set_var(key, v), + None => std::env::remove_var(key), + } + } + } + let _ = std::fs::remove_dir_all(&dir); + assert!(finished.is_ok(), "the session outlived its IPC connection"); + assert!(matches!( + registry.lock().await.state(session_id), + AppStateKind::Stopped + )); + } + #[tokio::test(flavor = "current_thread")] async fn a_stopped_session_releases_its_ipc_relay() { use std::os::unix::fs::PermissionsExt; @@ -1019,10 +1136,10 @@ mod tests { .unwrap(); std::fs::set_permissions(&child, std::fs::Permissions::from_mode(0o755)).unwrap(); let vars = [ - ("WEFT_RUNTIME_BIN", child.as_os_str()), - ("WEFT_APP_SHELL_BIN", child.as_os_str()), - ("WEFT_DISABLE_CGROUP", std::ffi::OsStr::new("1")), - ("WEFT_APP_STORE", dir.join("store").into_os_string().leak()), + ("WEFT_RUNTIME_BIN", child.clone().into_os_string()), + ("WEFT_APP_SHELL_BIN", child.clone().into_os_string()), + ("WEFT_DISABLE_CGROUP", "1".into()), + ("WEFT_APP_STORE", dir.join("store").into_os_string()), ]; let prior: Vec<_> = vars .iter() diff --git a/crates/weft-appd/src/runtime.rs b/crates/weft-appd/src/runtime.rs index 2478ab2..e9ca7e0 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -17,7 +17,15 @@ pub(crate) struct IpcRelay { } impl IpcRelay { - fn close(self) { + /// Completes when the relay stops: the component closed its connection + /// or the listener failed. + async fn ended(&mut self) { + let _ = (&mut self.task).await; + } +} + +impl Drop for IpcRelay { + fn drop(&mut self) { self.task.abort(); let _ = std::fs::remove_file(&self.socket_path); } @@ -34,6 +42,10 @@ pub(crate) fn spawn_ipc_relay( ) -> std::io::Result<(tokio::sync::mpsc::Sender, IpcRelay)> { let _ = std::fs::remove_file(&socket_path); let listener = tokio::net::UnixListener::bind(&socket_path)?; + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(&socket_path, std::fs::Permissions::from_mode(0o600))?; + } let (html_to_wasm_tx, mut html_to_wasm_rx) = tokio::sync::mpsc::channel::(64); let task = tokio::spawn(async move { let Ok((stream, _)) = listener.accept().await else { @@ -410,6 +422,9 @@ pub(crate) async fn supervise( format!("app shell exited ({status:?})") } _ = &mut abort_rx => "terminate requested".to_owned(), + _ = session.relay.as_mut().expect("relay opened").ended() => { + "component closed its IPC connection".to_owned() + } }; session.settle(®istry, &reason).await } @@ -459,9 +474,7 @@ impl OwnedSession { } self.mount.umount(); kill_portal(self.portal).await; - if let Some(relay) = self.relay { - relay.close(); - } + drop(self.relay); let mut reg = registry.lock().await; reg.remove_ipc_sender(session_id); reg.set_state(session_id, AppStateKind::Stopped); diff --git a/docs/architecture.md b/docs/architecture.md index ee5dc2e..fd59065 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -20,7 +20,7 @@ Per-application Servo host. Spawned by `weft-appd` after the Wasm runtime signal ### weft-appd -Session supervisor. Listens on a Unix socket (MessagePack protocol) and a WebSocket port (JSON). For each session: spawns `weft-runtime` and waits for its READY, spawns `weft-app-shell` (`WEFT_APP_SHELL_BIN` is required) and waits for its READY, and only then reports the session running (`APP_READY`). A child reports readiness by printing `READY ` on stdout, where the token is a random per-session value appd passes in `WEFT_READY_TOKEN`; Wasm guests and pages share those stdout streams but cannot read the token, so their output cannot fake readiness. Each wait times out after 30 seconds. If either child fails to start, fails to become ready or exits, or the session is terminated, the whole session is stopped: both children, the IPC relay (its task, socket and registered sender), the file portal, the compositor association and any image mount are released. The IPC relay is created with the session and accepts one connection, from the session's runtime. appd applies cgroup resource limits to the runtime via systemd-run when available. +Session supervisor. Listens on a Unix socket (MessagePack protocol) and a WebSocket port (JSON). For each session: spawns `weft-runtime` and waits for its READY, spawns `weft-app-shell` (`WEFT_APP_SHELL_BIN` is required) and waits for its READY, and only then reports the session running (`APP_READY`). A child reports readiness by printing `READY ` on stdout, where the token is a random per-session value appd passes in `WEFT_READY_TOKEN`; Wasm guests and pages share those stdout streams but cannot read the token, so their output cannot fake readiness. Each wait times out after 30 seconds. If either child fails to start, fails to become ready or exits, or the session is terminated, the whole session is stopped: both children, the IPC relay (its task, socket and registered sender), the file portal, the compositor association and any image mount are released. The IPC relay is created with the session in `$XDG_RUNTIME_DIR/weft` (a session cannot start without it); its socket is accessible only to the user and accepts a single connection, which the session's runtime makes before it starts the component. If that connection closes while the session runs, the session is stopped. `IPC_FORWARD` on the Unix socket never waits: it reports an error when the session has no relay or its queue is full. appd applies cgroup resource limits to the runtime via systemd-run when available. ### weft-runtime From ec00531c750bf3edc83d4cd889fd973e6fc003ab Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 09:24:49 +0000 Subject: [PATCH 3/4] test(appd): give each test process its own runtime directory Session sockets are named by session ID and every test registry starts at 1, so test processes running at the same time shared one socket path in the common test runtime directory and broke each other's sessions. Each test process now uses its own directory under the system temporary directory, and directories of ended test processes are removed. The test of a closed IPC connection connects to the socket itself instead of relying on python3 in a child script. A session whose component closes its IPC connection during startup now stops before it is reported ready. The weft runtime directory is created with mode 0700, and the socket is removed if its permissions cannot be restricted. --- crates/weft-appd/src/main.rs | 89 +++++++++++++++++++++------------ crates/weft-appd/src/runtime.rs | 10 +++- 2 files changed, 65 insertions(+), 34 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index ccd71c9..79aa255 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -415,7 +415,13 @@ async fn handle_connection( pub(crate) fn session_ipc_socket_path(session_id: u64) -> Option { let runtime_dir = std::env::var("XDG_RUNTIME_DIR").ok()?; let dir = PathBuf::from(runtime_dir).join("weft"); - std::fs::create_dir_all(&dir).ok()?; + // Created accessible to the user only, so session sockets are private + // whatever the umask. + let mut builder = std::fs::DirBuilder::new(); + builder.recursive(true); + #[cfg(unix)] + std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700); + builder.create(&dir).ok()?; Some(dir.join(format!("ipc-{session_id}.sock"))) } @@ -634,11 +640,25 @@ mod tests { static ENV_LOCK: std::sync::OnceLock> = std::sync::OnceLock::new(); - /// Points XDG_RUNTIME_DIR at a private directory for the session's IPC - /// socket. Callers hold env_lock. + /// Points XDG_RUNTIME_DIR at a directory private to this test process, + /// since session sockets are named by session ID and every test registry + /// starts at 1. Directories of test processes that have ended are + /// removed. Callers hold env_lock. fn use_test_runtime_dir() { - let dir = - std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/test-runtime-dir"); + let base = std::env::temp_dir().join("weft-appd-tests"); + if let Ok(entries) = std::fs::read_dir(&base) { + for entry in entries.flatten() { + let alive = entry + .file_name() + .to_str() + .and_then(|pid| pid.parse::().ok()) + .is_some_and(|pid| std::path::Path::new(&format!("/proc/{pid}")).exists()); + if !alive { + let _ = std::fs::remove_dir_all(entry.path()); + } + } + } + let dir = base.join(std::process::id().to_string()); std::fs::create_dir_all(&dir).unwrap(); // SAFETY: env_lock serialises the tests that change the environment, // and std serialises the environment accesses themselves. @@ -1047,25 +1067,15 @@ mod tests { use_test_runtime_dir(); let dir = std::env::temp_dir().join(format!("weft_test_ipc_close_{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); - // The runtime connects to its IPC socket ($4), closes the connection - // and keeps running; the app shell just reports ready. - let runtime = dir.join("runtime.sh"); + // Both children report ready and keep running; the test plays the + // component, connecting to the session's IPC socket and closing it. + let child = dir.join("child.sh"); std::fs::write( - &runtime, - "#!/bin/sh\necho READY $WEFT_READY_TOKEN\n\ - python3 -c 'import socket,sys; s=socket.socket(socket.AF_UNIX); \ - s.connect(sys.argv[1]); s.close()' \"$4\"\nexec sleep 30\n", - ) - .unwrap(); - let shell = dir.join("shell.sh"); - std::fs::write( - &shell, + &child, "#!/bin/sh\necho READY $WEFT_READY_TOKEN\nexec sleep 30\n", ) .unwrap(); - for script in [&runtime, &shell] { - std::fs::set_permissions(script, std::fs::Permissions::from_mode(0o755)).unwrap(); - } + std::fs::set_permissions(&child, std::fs::Permissions::from_mode(0o755)).unwrap(); let prior: Vec<_> = [ "WEFT_RUNTIME_BIN", "WEFT_APP_SHELL_BIN", @@ -1076,25 +1086,38 @@ mod tests { .collect(); // SAFETY: env_lock serialises the tests that change the environment. unsafe { - std::env::set_var("WEFT_RUNTIME_BIN", &runtime); - std::env::set_var("WEFT_APP_SHELL_BIN", &shell); + std::env::set_var("WEFT_RUNTIME_BIN", &child); + std::env::set_var("WEFT_APP_SHELL_BIN", &child); std::env::set_var("WEFT_DISABLE_CGROUP", "1"); } let registry = make_registry(); + let mut rx = registry.lock().await.subscribe(); let session_id = registry.lock().await.launch("test.app"); let abort_rx = registry.lock().await.register_abort(session_id); - let finished = tokio::time::timeout( - std::time::Duration::from_secs(15), - runtime::supervise( - session_id, - "test.app", - grants::SessionGrants::default(), - Arc::clone(®istry), - abort_rx, - None, - ), - ) + let socket = session_ipc_socket_path(session_id).unwrap(); + let component = async { + // Connect once the session is running, then close the connection. + loop { + if let Ok(Response::AppReady { session_id: id, .. }) = rx.recv().await + && id == session_id + { + break; + } + } + drop(tokio::net::UnixStream::connect(&socket).await.unwrap()); + }; + let supervised = runtime::supervise( + session_id, + "test.app", + grants::SessionGrants::default(), + Arc::clone(®istry), + abort_rx, + None, + ); + let finished = tokio::time::timeout(std::time::Duration::from_secs(15), async { + tokio::join!(supervised, component) + }) .await; for (key, value) in prior { diff --git a/crates/weft-appd/src/runtime.rs b/crates/weft-appd/src/runtime.rs index e9ca7e0..11c91e2 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -44,7 +44,12 @@ pub(crate) fn spawn_ipc_relay( let listener = tokio::net::UnixListener::bind(&socket_path)?; { use std::os::unix::fs::PermissionsExt; - std::fs::set_permissions(&socket_path, std::fs::Permissions::from_mode(0o600))?; + if let Err(e) = + std::fs::set_permissions(&socket_path, std::fs::Permissions::from_mode(0o600)) + { + let _ = std::fs::remove_file(&socket_path); + return Err(e); + } } let (html_to_wasm_tx, mut html_to_wasm_rx) = tokio::sync::mpsc::channel::(64); let task = tokio::spawn(async move { @@ -397,6 +402,9 @@ pub(crate) async fn supervise( status = session.runtime.as_mut().expect("runtime spawned").wait() => { Err(format!("runtime exited before the app shell was ready ({status:?})")) } + _ = session.relay.as_mut().expect("relay opened").ended() => { + Err("component closed its IPC connection before the session was ready".to_owned()) + } }; let shell_stdout = match shell_stdout { Ok(reader) => reader, From 010573eb16257071d9e8e67a21de086a5cc811a6 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 09:27:28 +0000 Subject: [PATCH 4/4] fix(appd): create the runtime directory private wherever it is made appd created $XDG_RUNTIME_DIR/weft with the umask mode when it opened its own socket, wrote its port or system token, or opened a portal socket, so the 0700 mode requested for session sockets never applied. All of them now use one helper that creates the directory 0700 and tightens an existing one. --- crates/weft-appd/src/main.rs | 25 ++++++++++++++++++------- crates/weft-appd/src/runtime.rs | 2 +- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 79aa255..c59130a 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -190,7 +190,7 @@ async fn run() -> anyhow::Result<()> { let socket_path = appd_socket_path()?; if let Some(parent) = socket_path.parent() { - std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?; + private_dir(parent).with_context(|| format!("create {}", parent.display()))?; } let _ = std::fs::remove_file(&socket_path); @@ -366,7 +366,7 @@ fn system_token_path() -> std::io::Result { .map_err(|_| std::io::Error::other("XDG_RUNTIME_DIR not set"))?, ) .join("weft"); - std::fs::create_dir_all(&dir)?; + private_dir(&dir)?; Ok(dir.join("appd.systoken")) } @@ -375,7 +375,7 @@ fn write_ws_port(port: u16) -> anyhow::Result<()> { let runtime_dir = std::env::var("XDG_RUNTIME_DIR").context("XDG_RUNTIME_DIR not set")?; let path = PathBuf::from(runtime_dir).join("weft/appd.wsport"); if let Some(parent) = path.parent() { - std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?; + private_dir(parent).with_context(|| format!("create {}", parent.display()))?; } std::fs::write(&path, port.to_string()).with_context(|| format!("write {}", path.display()))?; Ok(()) @@ -415,14 +415,25 @@ async fn handle_connection( pub(crate) fn session_ipc_socket_path(session_id: u64) -> Option { let runtime_dir = std::env::var("XDG_RUNTIME_DIR").ok()?; let dir = PathBuf::from(runtime_dir).join("weft"); - // Created accessible to the user only, so session sockets are private - // whatever the umask. + private_dir(&dir).ok()?; + Some(dir.join(format!("ipc-{session_id}.sock"))) +} + +/// Creates appd's runtime directory (`$XDG_RUNTIME_DIR/weft`) or another of +/// its directories, accessible to the user only whatever the umask, also +/// when it already existed. +pub(crate) fn private_dir(dir: &std::path::Path) -> std::io::Result<()> { let mut builder = std::fs::DirBuilder::new(); builder.recursive(true); #[cfg(unix)] std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700); - builder.create(&dir).ok()?; - Some(dir.join(format!("ipc-{session_id}.sock"))) + builder.create(dir)?; + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700))?; + } + Ok(()) } pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response { diff --git a/crates/weft-appd/src/runtime.rs b/crates/weft-appd/src/runtime.rs index 11c91e2..9026c82 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -212,7 +212,7 @@ fn spawn_app_shell( fn portal_socket_path(session_id: u64) -> Option { let runtime_dir = std::env::var("XDG_RUNTIME_DIR").ok()?; let dir = PathBuf::from(runtime_dir).join("weft"); - std::fs::create_dir_all(&dir).ok()?; + crate::private_dir(&dir).ok()?; Some(dir.join(format!("portal-{session_id}.sock"))) }