Files
chandra/scripts/build-wasmhost.sh
nandiandClaude Sonnet 5.5 ada9850b8d Fix the memory leak that filled RAM and swap; launch monitor, memory log
wasmhost leaked the arguments of every host call. wt_stub.c handed them to
the MoonBit dispatcher expecting it to own them, but the compiler borrowed
the parameter, so nothing released them (about 600 kB/s in a test with one
busy terminal; a long session reached over 16 GB resident plus swap).
MoonBit now allocates the buffer and C copies into it.

Also:
- Unannotated pointer FFI parameters default to #owned, and none of these C
  functions released theirs, including the native KMS present (a whole frame
  each). Annotated #borrow; the wasmhost build now fails on that warning,
  which --warn-list -a had hidden.
- Watched launches forked the whole compositor, and each monitor kept a
  snapshot of its heap for the app's lifetime. It now posix_spawns a fresh
  copy of the binary that takes over before main.
- The host logs memory, window buffers and images once a minute.
- chandra-session keeps the previous log as session.log.1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hj5m35Lj6Rt9vwwLGZ
2026-11-26 17:10:47 -08:00

30 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Build the MoonBit host for the compositor component: build/wasmhost.
# It links the prebuilt wasmtime C API (scripts/setup-toolchain.sh puts it in .tools/wasmtime-c-api)
# instead of compiling wasmtime, so a build takes seconds. Statically linked: nothing to install.
# scripts/build-wasmhost.sh
# build/wasmhost build/compositor.wasm --socket PATH [--window | --drm | --gfx-out PNG]
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"; cd "$ROOT"
export PATH="$ROOT/.tools/bin:$HOME/.moon/bin:$PATH"
CAPI="$ROOT/.tools/wasmtime-c-api"
[ -f "$CAPI/lib/libwasmtime.a" ] || scripts/setup-toolchain.sh
[ -f "$CAPI/lib/libwasmtime.a" ] || { echo "error: $CAPI missing; run scripts/setup-toolchain.sh" >&2; exit 1; }
command -v moon >/dev/null || { echo "error: moon not found" >&2; exit 1; }
export C_INCLUDE_PATH="$CAPI/include${C_INCLUDE_PATH:+:$C_INCLUDE_PATH}"
export LIBRARY_PATH="$CAPI/lib${LIBRARY_PATH:+:$LIBRARY_PATH}"
# libseat (and its headers) from the pixi env when there is one (see pixi.toml).
PIXI="$ROOT/.pixi/envs/default"
if [ -d "$PIXI/lib" ]; then
export C_INCLUDE_PATH="$C_INCLUDE_PATH:$PIXI/include"
export LIBRARY_PATH="$LIBRARY_PATH:$PIXI/lib"
fi
# unannotated_ffi fails the build: an unannotated pointer parameter is owned by the C function, and
# one that doesn't release it leaks it on every call.
moon build --target native --release --warn-list -a+unannotated_ffi --deny-warn wasmhost/bin
mkdir -p build
# Replace it by renaming, so a running session (`just install --link` links to it) keeps its copy.
cp _build/native/release/build/wasmhost/bin/bin.exe build/wasmhost.new
mv -f build/wasmhost.new build/wasmhost
echo "build/wasmhost $(stat -c %s build/wasmhost) bytes"