Merge fix/end-runtime-on-ipc-hangup: end the runtime when its session's IPC connection closes (#36)

This commit is contained in:
Marco Allegretti 2026-10-10 09:27:22 +02:00 committed by GitHub
commit 7e9df63d87
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 143 additions and 4 deletions

View file

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

View file

@ -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")),

View file

@ -67,6 +67,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);

View file

@ -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"]

View file

@ -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<Self> {
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)

View file

@ -16,7 +16,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")),
@ -301,9 +305,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}");
}
}

View file

@ -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 <token>` 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. `TERMINATE_APP` on a running session closes it as a user close would: appd asks the compositor to close the session's windows and gives the app shell 5 seconds to exit by itself, for example after saving, before terminating it. The session stays `running` while the application answers (appd replies with that state), and enters `stopping` only once it is ending. An application that declares the `ask` close policy may decline the close (see [Closing an application](#closing-an-application)); its window is brought to the front and the session keeps running. A repeated request while the application answers changes nothing, and a session being asked to close is still restored at the next start if appd stops meanwhile. `TERMINATE_APP` with `"force": true` terminates the session without asking; the taskbar offers it, as Force quit, for an app still running once it has had time to answer. The stop reason records what happened: a close on request with a clean exit, a failure while closing, the timeout, or a forced stop; `stopped` is broadcast once the session has settled. A session still starting is stopped at once, also when the request races its readiness, and when appd itself shuts down its sessions are terminated without a close. appd keeps the final state of at most 32 stopped sessions, forgetting those launched earliest. 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 <token>` 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. `TERMINATE_APP` on a running session closes it as a user close would: appd asks the compositor to close the session's windows and gives the app shell 5 seconds to exit by itself, for example after saving, before terminating it. The session stays `running` while the application answers (appd replies with that state), and enters `stopping` only once it is ending. An application that declares the `ask` close policy may decline the close (see [Closing an application](#closing-an-application)); its window is brought to the front and the session keeps running. A repeated request while the application answers changes nothing, and a session being asked to close is still restored at the next start if appd stops meanwhile. `TERMINATE_APP` with `"force": true` terminates the session without asking; the taskbar offers it, as Force quit, for an app still running once it has had time to answer. The stop reason records what happened: a close on request with a clean exit, a failure while closing, the timeout, or a forced stop; `stopped` is broadcast once the session has settled. A session still starting is stopped at once, also when the request races its readiness, and when appd itself shuts down its sessions are terminated without a close. appd keeps the final state of at most 32 stopped sessions, forgetting those launched earliest. 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

View file

@ -28,7 +28,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.
"""
@ -36,6 +38,7 @@ Requires cargo with the wasm32-wasip2 target.
import argparse
import http.server
import shutil
import socket
import subprocess
import sys
import tempfile
@ -43,6 +46,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"
@ -186,6 +190,52 @@ 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")
log = process.stderr.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}")
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)
@ -207,6 +257,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