Files
chandra/wasmhost
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
..
2026-11-26 04:19:19 +00:00

wasmhost

The host that runs the compositor component (build/compositor.wasm): just run, just run-drm and just install use it. It is the only host: it replaced the Rust host-wasm/ (on the wasmtime crate), which was removed on 2026-11-26.

It links wasmtime as the prebuilt C API library (libwasmtime.a from the upstream wasmtime-v$WASMTIME_VERSION-x86_64-linux-c-api release, fetched by scripts/setup-toolchain.sh into .tools/wasmtime-c-api) instead of compiling wasmtime, so a cold build takes about 4 s.

scripts/build-wasmhost.sh        # or: just wasmhost  ->  build/wasmhost
build/wasmhost build/compositor.wasm --socket PATH --gfx-out frame.png
  • wt/: the wasmtime C API (wt_stub.c) and the value encoding host calls cross as (val.mbt). wasmtime's C API has no bindgen: every call goes through one callback with dynamic values, so each import is a small handler from values to values. A list<u8> crosses as one dynamic value per byte (about 0.4 s for a 720p frame) and wasmtime caps how much one call may copy (about 2.7 MB of list<u8>), so large files are written as write-file plus append-file chunks, and screenshots, recording and --gfx-out keep the screen's pixels on the host (snapshot-*, *-recording-frame, write-frame-png in wit/host/raster.wit).
  • bin/: the interfaces of wit/host. Most of it is the native compositor's own packages: sys is compositor/sys, glyphs come from compositor/font (stb_truetype). The rasteriser (raster.mbt, pixel work in raster_stub.c) is a port of compositor/renderer; SVG icons use nanosvg (third_party/nanosvg, patched for clip paths and masks). plasmoids.mbt runs panel modules through compositor/wasm (wasm.h, which libwasmtime provides); display.mbt is the login session's DRM/KMS output and input (compositor/kms).

Every interface of wit/host is served here. CPU in use: about 1 s at start to compile the component, then idle.

WASMHOST_TRACE=1 logs every host call; WASMHOST_PROFILE=1 prints the time spent in each host function every 5 s.