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.
This commit is contained in:
Marco Allegretti 2026-10-09 21:06:58 +00:00
parent 36ae676d83
commit 57f364890f
No known key found for this signature in database
6 changed files with 133 additions and 3 deletions

View file

@ -91,6 +91,8 @@ jobs:
--all-targets -- -D warnings --all-targets -- -D warnings
- name: cargo build (weft-runtime with wasmtime-runtime and net-fetch) - name: cargo build (weft-runtime with wasmtime-runtime and net-fetch)
run: cargo build -p weft-runtime --features wasmtime-runtime,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 - name: grants check
run: python3 -B tests/runtime/check_grants.py run: python3 -B tests/runtime/check_grants.py

View file

@ -62,6 +62,10 @@ pub(crate) fn spawn_ipc_relay(
// One component per session: no further connections are accepted. // One component per session: no further connections are accepted.
drop(listener); drop(listener);
tracing::debug!(session_id, "IPC relay: component connected"); 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 (reader, writer) = tokio::io::split(stream);
let mut reader = BufReader::new(reader); let mut reader = BufReader::new(reader);
let mut writer = BufWriter::new(writer); let mut writer = BufWriter::new(writer);

View file

@ -10,7 +10,7 @@ path = "src/main.rs"
[features] [features]
default = [] 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"] seccomp = ["dep:seccompiler", "dep:libc"]
net-fetch = ["dep:ureq", "dep:url"] net-fetch = ["dep:ureq", "dep:url"]

View file

@ -5,8 +5,14 @@
//! which would split it into several messages. Received data is buffered //! 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 //! up to one message; a peer that sends a longer line, or closes the
//! connection, ends it for good. //! 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::io::{Read, Write};
use std::os::fd::AsRawFd;
use std::os::unix::net::UnixStream; use std::os::unix::net::UnixStream;
/// The longest message in either direction, in bytes. /// The longest message in either direction, in bytes.
@ -39,6 +45,19 @@ impl IpcState {
Self::new(socket) 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> { fn new(socket: UnixStream) -> Option<Self> {
socket.set_nonblocking(true).ok()?; socket.set_nonblocking(true).ok()?;
Some(Self { 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; 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) { fn pair() -> (IpcState, UnixStream) {
let (ours, theirs) = UnixStream::pair().unwrap(); let (ours, theirs) = UnixStream::pair().unwrap();
(IpcState::new(ours).unwrap(), theirs) (IpcState::new(ours).unwrap(), theirs)

View file

@ -295,9 +295,12 @@ fn run_module(
if let Some(socket_path) = ipc_socket { if let Some(socket_path) = ipc_socket {
ctx_builder.env("WEFT_IPC_SOCKET", socket_path); ctx_builder.env("WEFT_IPC_SOCKET", socket_path);
if let Some(ipc) = ipc::IpcState::connect(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); *ipc_state.lock().unwrap_or_else(|p| p.into_inner()) = Some(ipc);
} else { } 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

@ -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 It also checks that the runtime refuses, with a specific error, filesystem
capabilities passed as `--grant`, unknown capabilities and preopens 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. Requires cargo with the wasm32-wasip2 target.
""" """
@ -33,6 +35,7 @@ Requires cargo with the wasm32-wasip2 target.
import argparse import argparse
import http.server import http.server
import shutil import shutil
import socket
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
@ -40,6 +43,7 @@ import threading
from pathlib import Path from pathlib import Path
ROOT = Path(__file__).resolve().parents[2] ROOT = Path(__file__).resolve().parents[2]
COUNTER = ROOT / "examples/org.weft.demo.counter/app.wasm"
PROBE_DIR = ROOT / "tests/components/grants-probe" PROBE_DIR = ROOT / "tests/components/grants-probe"
APP_ID = "org.weft.test.grants" APP_ID = "org.weft.test.grants"
NOT_GRANTED = "is not granted" NOT_GRANTED = "is not granted"
@ -169,6 +173,51 @@ def check(runner, data):
f"{message!r}:\n{result.stderr}") 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): def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__, parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter) formatter_class=argparse.RawDescriptionHelpFormatter)
@ -190,6 +239,7 @@ def main(argv=None):
data = work / "data" data = work / "data"
data.mkdir() data.mkdir()
check(runner, data) check(runner, data)
check_hangup(args.runtime, work)
except AssertionError as failure: except AssertionError as failure:
print(f"grants check failed: {failure}", file=sys.stderr) print(f"grants check failed: {failure}", file=sys.stderr)
return 1 return 1