From d9d9d24205a76fd6ceea6d62f17063abcf9119ff Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 09:11:35 +0000 Subject: [PATCH] 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