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