From 57f364890f754d6df1e57607fe2670d7b1642a5b Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 21:06:58 +0000 Subject: [PATCH 1/3] fix(runtime): end the runtime when its session's IPC connection closes A runtime outlived the weft-appd that started it. Runtimes run in their own systemd scopes, outside weft-appd's service, and a component that waits for messages, as the Counter and Notes demos do, keeps polling a closed connection forever. Each time weft-appd stopped or restarted and restored the previous session, the earlier runtimes stayed behind. The connection is the session: a thread waits for it to hang up, without reading from it, and the runtime then exits whatever the component is doing. That covers weft-appd closing the connection, weft-appd ending, and the runtime ending the connection itself after a failed send or an overlong message. A runtime given an IPC socket it cannot connect to now fails instead of running unreachable. weft-appd's relay notes that it must not shut down half of the connection, which would now end the session. tests/runtime/check_grants.py runs the real runtime on the Counter component over a Unix socket: it answers an increment, and once the test closes its end the runtime exits with status 0 within 10 seconds; with the watcher removed it keeps running and the check fails. A unit test checks that the wait sees a hangup but neither wakes on nor consumes pending data; CI's runtime-grants job now runs the runtime's unit tests with its features. The nested desktop Counter and Notes checks pass with this runtime. --- .github/workflows/ci.yml | 2 + crates/weft-appd/src/runtime.rs | 4 ++ crates/weft-runtime/Cargo.toml | 2 +- crates/weft-runtime/src/ipc.rs | 71 +++++++++++++++++++++++++++++++++ crates/weft-runtime/src/main.rs | 5 ++- tests/runtime/check_grants.py | 52 +++++++++++++++++++++++- 6 files changed, 133 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 34f1e8d..42a80bf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -91,6 +91,8 @@ jobs: --all-targets -- -D warnings - name: cargo build (weft-runtime with wasmtime-runtime and net-fetch) run: cargo build -p weft-runtime --features wasmtime-runtime,net-fetch + - name: cargo test (weft-runtime with wasmtime-runtime and net-fetch) + run: cargo test -p weft-runtime --features wasmtime-runtime,net-fetch - name: grants check run: python3 -B tests/runtime/check_grants.py diff --git a/crates/weft-appd/src/runtime.rs b/crates/weft-appd/src/runtime.rs index fb8d1b5..7536a99 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -62,6 +62,10 @@ pub(crate) fn spawn_ipc_relay( // One component per session: no further connections are accepted. drop(listener); tracing::debug!(session_id, "IPC relay: component connected"); + // The runtime ends when its side of this connection is shut down, + // even only for writing, so the connection stays whole until the + // session ends: tokio::io::split does not shut down a dropped half, + // as UnixStream::into_split would. let (reader, writer) = tokio::io::split(stream); let mut reader = BufReader::new(reader); let mut writer = BufWriter::new(writer); diff --git a/crates/weft-runtime/Cargo.toml b/crates/weft-runtime/Cargo.toml index 8fd3c2a..2ac987b 100644 --- a/crates/weft-runtime/Cargo.toml +++ b/crates/weft-runtime/Cargo.toml @@ -10,7 +10,7 @@ path = "src/main.rs" [features] default = [] -wasmtime-runtime = ["dep:wasmtime", "dep:wasmtime-wasi", "dep:cap-std"] +wasmtime-runtime = ["dep:wasmtime", "dep:wasmtime-wasi", "dep:cap-std", "dep:libc"] seccomp = ["dep:seccompiler", "dep:libc"] net-fetch = ["dep:ureq", "dep:url"] diff --git a/crates/weft-runtime/src/ipc.rs b/crates/weft-runtime/src/ipc.rs index 998ac4e..4fda19f 100644 --- a/crates/weft-runtime/src/ipc.rs +++ b/crates/weft-runtime/src/ipc.rs @@ -5,8 +5,14 @@ //! which would split it into several messages. Received data is buffered //! up to one message; a peer that sends a longer line, or closes the //! connection, ends it for good. +//! +//! The connection is the session: once it has ended, by weft-appd closing +//! it or stopping, the runtime exits, whatever the component is doing, and +//! a runtime that cannot connect does not start, so no runtime outlives the +//! weft-appd that started it. use std::io::{Read, Write}; +use std::os::fd::AsRawFd; use std::os::unix::net::UnixStream; /// The longest message in either direction, in bytes. @@ -39,6 +45,19 @@ impl IpcState { Self::new(socket) } + /// Exits the process once the connection has ended. + pub fn exit_on_hangup(&self) -> std::io::Result<()> { + let socket = self.socket.try_clone()?; + std::thread::Builder::new() + .name("ipc-hangup".into()) + .spawn(move || { + wait_for_hangup(&socket); + tracing::info!("the session's IPC connection ended; exiting"); + std::process::exit(0); + })?; + Ok(()) + } + fn new(socket: UnixStream) -> Option { socket.set_nonblocking(true).ok()?; Some(Self { @@ -141,10 +160,62 @@ impl IpcState { } } +/// The peer shutting down its side; elsewhere only a full hangup is seen. +#[cfg(any(target_os = "linux", target_os = "android"))] +const PEER_SHUTDOWN: libc::c_short = libc::POLLRDHUP; +#[cfg(not(any(target_os = "linux", target_os = "android")))] +const PEER_SHUTDOWN: libc::c_short = 0; + +/// Blocks until the peer has closed `socket` or it was shut down. Data +/// waiting to be read neither wakes it nor is consumed. +fn wait_for_hangup(socket: &UnixStream) { + let mut pfd = libc::pollfd { + fd: socket.as_raw_fd(), + events: PEER_SHUTDOWN, + revents: 0, + }; + loop { + let ready = unsafe { libc::poll(&mut pfd, 1, -1) }; + if ready < 0 { + if std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted { + continue; + } + return; + } + if pfd.revents & (PEER_SHUTDOWN | libc::POLLHUP | libc::POLLERR | libc::POLLNVAL) != 0 { + return; + } + } +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn a_hangup_is_seen_and_pending_data_is_not() { + let (ours, mut theirs) = UnixStream::pair().unwrap(); + let (done_tx, done_rx) = std::sync::mpsc::channel(); + let watched = ours.try_clone().unwrap(); + std::thread::spawn(move || { + wait_for_hangup(&watched); + let _ = done_tx.send(()); + }); + theirs.write_all(b"message\n").unwrap(); + assert!( + done_rx + .recv_timeout(std::time::Duration::from_millis(300)) + .is_err() + ); + drop(theirs); + done_rx + .recv_timeout(std::time::Duration::from_secs(5)) + .expect("the hangup was not seen"); + // The message is still there to read. + let mut ipc = IpcState::new(ours).unwrap(); + assert_eq!(ipc.recv().as_deref(), Some("message")); + } + fn pair() -> (IpcState, UnixStream) { let (ours, theirs) = UnixStream::pair().unwrap(); (IpcState::new(ours).unwrap(), theirs) diff --git a/crates/weft-runtime/src/main.rs b/crates/weft-runtime/src/main.rs index 9c5b92d..d5b5097 100644 --- a/crates/weft-runtime/src/main.rs +++ b/crates/weft-runtime/src/main.rs @@ -295,9 +295,12 @@ fn run_module( if let Some(socket_path) = ipc_socket { ctx_builder.env("WEFT_IPC_SOCKET", socket_path); if let Some(ipc) = ipc::IpcState::connect(socket_path) { + ipc.exit_on_hangup().context("watch the IPC connection")?; *ipc_state.lock().unwrap_or_else(|p| p.into_inner()) = Some(ipc); } else { - tracing::warn!("weft:app/ipc: could not connect to IPC socket {socket_path}"); + // Without its connection the session cannot be reached or + // ended; the runtime would only be left behind. + anyhow::bail!("cannot connect to the IPC socket {socket_path}"); } } diff --git a/tests/runtime/check_grants.py b/tests/runtime/check_grants.py index 2ef7bfb..fcee9a3 100644 --- a/tests/runtime/check_grants.py +++ b/tests/runtime/check_grants.py @@ -25,7 +25,9 @@ scenario passes a different set of `--grant` and `--preopen` arguments: It also checks that the runtime refuses, with a specific error, filesystem capabilities passed as `--grant`, unknown capabilities and preopens -without an access mode. +without an access mode, and that a runtime running the Counter demo, which +waits for messages forever, answers over its IPC connection and exits once +weft-appd's end of that connection closes. Requires cargo with the wasm32-wasip2 target. """ @@ -33,6 +35,7 @@ Requires cargo with the wasm32-wasip2 target. import argparse import http.server import shutil +import socket import subprocess import sys import tempfile @@ -40,6 +43,7 @@ import threading from pathlib import Path ROOT = Path(__file__).resolve().parents[2] +COUNTER = ROOT / "examples/org.weft.demo.counter/app.wasm" PROBE_DIR = ROOT / "tests/components/grants-probe" APP_ID = "org.weft.test.grants" NOT_GRANTED = "is not granted" @@ -169,6 +173,51 @@ def check(runner, data): f"{message!r}:\n{result.stderr}") +def check_hangup(runtime, work): + path = work / "ipc.sock" + listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + listener.bind(str(path)) + listener.listen(1) + listener.settimeout(30) + process = subprocess.Popen( + [str(runtime), "org.weft.demo.counter", "1", "--module", str(COUNTER), + "--ipc-socket", str(path)], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, + env={"PATH": "/usr/bin:/bin", "RUST_LOG": "info"}) + try: + try: + connection, _ = listener.accept() + except socket.timeout: + raise AssertionError("the runtime did not connect to its IPC socket") + connection.settimeout(30) + connection.sendall(b"increment\n") + reply = b"" + while not reply.endswith(b"\n"): + try: + chunk = connection.recv(4096) + except socket.timeout: + raise AssertionError("the counter did not answer over IPC") + if not chunk: + break + reply += chunk + if reply != b'{"count":1}\n': + raise AssertionError(f"the counter did not answer over IPC: {reply!r}") + connection.close() + try: + process.wait(timeout=10) + except subprocess.TimeoutExpired: + raise AssertionError("the runtime kept running after its IPC connection closed") + output = process.stdout.read() + process.stderr.read() + if process.returncode != 0 or "IPC connection ended" not in output: + raise AssertionError(f"the runtime did not end with the connection: " + f"exit {process.returncode}") + finally: + if process.poll() is None: + process.kill() + process.wait() + listener.close() + + def main(argv=None): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) @@ -190,6 +239,7 @@ def main(argv=None): data = work / "data" data.mkdir() check(runner, data) + check_hangup(args.runtime, work) except AssertionError as failure: print(f"grants check failed: {failure}", file=sys.stderr) return 1 From 83fca91772244b46dc931355a9407f414eb3e06d Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 21:13:29 +0000 Subject: [PATCH 2/3] fix(runtime): log to standard error, without colours when piped weft-runtime and weft-app-shell logged to standard output, which carries their readiness line to weft-appd. weft-appd keeps that stream only at debug level, so the runtime's warnings and errors, such as a failed grant or an ended IPC connection, never reached its log, and the lines carried terminal colour codes. Both now log to standard error, which weft-appd records at info level, with colours only on a terminal. In the nested desktop Counter check, the runtime's own lines appear in weft-appd's log; the grants check reads the hangup message from standard error. --- crates/weft-app-shell/src/main.rs | 4 ++++ crates/weft-runtime/src/main.rs | 4 ++++ tests/runtime/check_grants.py | 5 +++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/weft-app-shell/src/main.rs b/crates/weft-app-shell/src/main.rs index 3b68c29..4bed9ee 100644 --- a/crates/weft-app-shell/src/main.rs +++ b/crates/weft-app-shell/src/main.rs @@ -11,7 +11,11 @@ mod keyutils; use anyhow::Context; fn main() -> anyhow::Result<()> { + // Standard output carries the readiness line to weft-appd, which keeps + // standard error as the process's log. tracing_subscriber::fmt() + .with_writer(std::io::stderr) + .with_ansi(std::io::IsTerminal::is_terminal(&std::io::stderr())) .with_env_filter( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")), diff --git a/crates/weft-runtime/src/main.rs b/crates/weft-runtime/src/main.rs index d5b5097..b94f5a4 100644 --- a/crates/weft-runtime/src/main.rs +++ b/crates/weft-runtime/src/main.rs @@ -14,7 +14,11 @@ const DEFAULT_MAX_MEMORY_MIB: usize = 256; use grants::{Grants, Preopen}; fn main() -> anyhow::Result<()> { + // Standard output carries the readiness line to weft-appd, which keeps + // standard error as the process's log. tracing_subscriber::fmt() + .with_writer(std::io::stderr) + .with_ansi(std::io::IsTerminal::is_terminal(&std::io::stderr())) .with_env_filter( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")), diff --git a/tests/runtime/check_grants.py b/tests/runtime/check_grants.py index fcee9a3..34ca8b7 100644 --- a/tests/runtime/check_grants.py +++ b/tests/runtime/check_grants.py @@ -207,8 +207,9 @@ def check_hangup(runtime, work): process.wait(timeout=10) except subprocess.TimeoutExpired: raise AssertionError("the runtime kept running after its IPC connection closed") - output = process.stdout.read() + process.stderr.read() - if process.returncode != 0 or "IPC connection ended" not in output: + log = process.stderr.read() + process.stdout.read() + if process.returncode != 0 or "IPC connection ended" not in log: raise AssertionError(f"the runtime did not end with the connection: " f"exit {process.returncode}") finally: From 8e910c507cdf8dc0630929b3cd99f2ca19f926ca Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 21:22:29 +0000 Subject: [PATCH 3/3] docs(architecture): say where weft-appd's children log Following the move of weft-runtime and weft-app-shell logging to standard error: their stdout carries only the readiness line, and appd records stderr at info level and other stdout output at debug level. --- docs/architecture.md | 2 +- tests/runtime/check_grants.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index 93af5ec..0c4d5eb 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). Before a session exists, appd resolves the package once (see [Package resolution](#package-resolution)) and derives its grants from that package's manifest; the runtime and the app shell receive the resolved component and UI document and do not search the stores themselves. 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. +Session supervisor. Listens on a Unix socket (MessagePack protocol) and a WebSocket port (JSON). Before a session exists, appd resolves the package once (see [Package resolution](#package-resolution)) and derives its grants from that package's manifest; the runtime and the app shell receive the resolved component and UI document and do not search the stores themselves. 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. The two children log to stderr, which appd records at info level; their stdout carries the readiness line, and anything else printed there is recorded only at debug level. 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 diff --git a/tests/runtime/check_grants.py b/tests/runtime/check_grants.py index 34ca8b7..7413453 100644 --- a/tests/runtime/check_grants.py +++ b/tests/runtime/check_grants.py @@ -208,7 +208,7 @@ def check_hangup(runtime, work): except subprocess.TimeoutExpired: raise AssertionError("the runtime kept running after its IPC connection closed") log = process.stderr.read() - process.stdout.read() + process.stdout.read() # drained; the log is on standard error if process.returncode != 0 or "IPC connection ended" not in log: raise AssertionError(f"the runtime did not end with the connection: " f"exit {process.returncode}")