fix(runtime): close the IPC socket when the connection ends, and do not count refused growth

When the runtime ended an IPC connection, it only marked it closed.
weft-appd therefore kept a session whose messages went nowhere. The
socket is now shut down, so weft-appd ends the session as it does when a
component closes the connection.

A growth past a memory's or table's own maximum is refused without
being counted against the store's limit, since Wasmtime would reject it
anyway.
This commit is contained in:
Marco Allegretti 2026-10-09 18:42:18 +00:00
parent cbc984eef6
commit 36ae676d83
No known key found for this signature in database
3 changed files with 35 additions and 7 deletions

View file

@ -11,8 +11,8 @@ use std::os::unix::net::UnixStream;
/// The longest message in either direction, in bytes. /// The longest message in either direction, in bytes.
pub const MAX_MESSAGE: usize = 64 * 1024; pub const MAX_MESSAGE: usize = 64 * 1024;
/// How long sending one message may block before the connection is given /// How long one write of a message may block before the connection is
/// up. /// given up.
const SEND_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); const SEND_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
/// Checks a message the component wants to send. /// Checks a message the component wants to send.
@ -108,9 +108,12 @@ impl IpcState {
self.take_line() self.take_line()
} }
/// Ends the connection for both sides: weft-appd sees it close and
/// ends the session, as it does when the component closes it.
fn close(&mut self, reason: &'static str) { fn close(&mut self, reason: &'static str) {
tracing::warn!(reason, "IPC connection ended"); tracing::warn!(reason, "IPC connection ended");
self.closed = Some(reason); self.closed = Some(reason);
let _ = self.socket.shutdown(std::net::Shutdown::Both);
} }
fn overlong(&mut self) { fn overlong(&mut self) {
@ -179,6 +182,9 @@ mod tests {
} }
assert!(ipc.recv_buf.len() <= MAX_MESSAGE + 4096); assert!(ipc.recv_buf.len() <= MAX_MESSAGE + 4096);
assert!(ipc.send("after").is_err()); assert!(ipc.send("after").is_err());
// The peer sees the connection end.
let mut rest = Vec::new();
assert_eq!(peer.read_to_end(&mut rest).unwrap(), 0);
} }
#[test] #[test]

View file

@ -30,7 +30,18 @@ impl Limits {
} }
/// Accounts a growth from `current` to `desired` against `used` of `max`. /// Accounts a growth from `current` to `desired` against `used` of `max`.
fn grow(used: &mut usize, max: usize, current: usize, desired: usize) -> bool { /// A growth past the memory's or table's own `maximum` fails anyway, so it
/// is refused without being counted.
fn grow(
used: &mut usize,
max: usize,
current: usize,
desired: usize,
maximum: Option<usize>,
) -> bool {
if maximum.is_some_and(|m| desired > m) {
return false;
}
let added = desired.saturating_sub(current); let added = desired.saturating_sub(current);
match used.checked_add(added) { match used.checked_add(added) {
Some(total) if total <= max => { Some(total) if total <= max => {
@ -46,22 +57,29 @@ impl wasmtime::ResourceLimiter for Limits {
&mut self, &mut self,
current: usize, current: usize,
desired: usize, desired: usize,
_maximum: Option<usize>, maximum: Option<usize>,
) -> anyhow::Result<bool> { ) -> anyhow::Result<bool> {
Ok(grow(&mut self.memory, self.max_memory, current, desired)) Ok(grow(
&mut self.memory,
self.max_memory,
current,
desired,
maximum,
))
} }
fn table_growing( fn table_growing(
&mut self, &mut self,
current: usize, current: usize,
desired: usize, desired: usize,
_maximum: Option<usize>, maximum: Option<usize>,
) -> anyhow::Result<bool> { ) -> anyhow::Result<bool> {
Ok(grow( Ok(grow(
&mut self.table_elements, &mut self.table_elements,
MAX_TABLE_ELEMENTS, MAX_TABLE_ELEMENTS,
current, current,
desired, desired,
maximum,
)) ))
} }
@ -101,5 +119,9 @@ mod tests {
assert!(!limits.memory_growing(0, 101, None).unwrap()); assert!(!limits.memory_growing(0, 101, None).unwrap());
assert!(limits.memory_growing(0, 100, None).unwrap()); assert!(limits.memory_growing(0, 100, None).unwrap());
assert!(!limits.memory_growing(0, usize::MAX, None).unwrap()); assert!(!limits.memory_growing(0, usize::MAX, None).unwrap());
// Past a memory's own maximum: refused and not counted.
let mut limits = Limits::new(100);
assert!(!limits.memory_growing(0, 50, Some(40)).unwrap());
assert!(limits.memory_growing(0, 100, None).unwrap());
} }
} }

View file

@ -12,7 +12,7 @@ Installing a package does not yet ask the user to approve its capabilities; ever
## Process Isolation ## Process Isolation
Each app session runs as a separate OS process (`weft-runtime`). When systemd is available, the process is wrapped in a systemd scope (`weft-apps.slice`) with `CPUQuota=200%`, `MemoryMax=512M` and `TasksMax=512` (threads included; the runtime runs a few per CPU). Independently of systemd, the runtime limits the component's linear memory to 256 MiB in total, across all its memories (`--max-memory-mib` changes it), and bounds its tables, memories and core instances: a memory or table that would grow past its limit does not grow, and the component sees an allocation failure. App messages over `weft:app/ipc` are single lines of at most 64 KiB, the limit weft-appd applies; a component's message that is longer or contains a line break is refused, a longer line from weft-appd, or a failed or timed-out send (10 seconds), ends the connection, after which sending reports that it is closed. CPU time within the quota is not limited per component. Each app session runs as a separate OS process (`weft-runtime`). When systemd is available, the process is wrapped in a systemd scope (`weft-apps.slice`) with `CPUQuota=200%`, `MemoryMax=512M` and `TasksMax=512` (threads included; the runtime runs a few per CPU). Independently of systemd, the runtime limits the component's linear memory to 256 MiB in total, across all its memories (`--max-memory-mib` changes it), and bounds its tables, memories and core instances: a memory or table that would grow past its limit does not grow, and the component sees an allocation failure. App messages over `weft:app/ipc` are single lines of at most 64 KiB, the limit weft-appd applies; a component's message that is longer or contains a line break is refused, a longer line from weft-appd, or a failed send, or a write that blocks for 10 seconds, ends the connection for both sides, so weft-appd ends the session as it does when the component closes the connection. CPU time within the quota is not limited per component.
## Filesystem Isolation ## Filesystem Isolation