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
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-26 17:10:47 -08:00
1 parent 44688453d7
commit ada9850b8d
11 files changed
+211 -75

No files matched your search

+94 -62
View File
@@ -13,6 +13,7 @@
#include <sys/wait.h>
#include <time.h>
#include <signal.h>
#include <spawn.h>
#include <sys/prctl.h>
#include <dirent.h>
#include <unistd.h>
@@ -631,8 +632,82 @@ static int pump(int from, struct tail *t) {
}
}
/* Launch `cmd` like mp_spawn, watched: a small monitor process starts it with its stdout and
* stderr on a pipe, copies that output to our stderr (the session log, as before) and keeps its
/* ---- the launch monitor ----
* A watched launch runs this binary again, as MONITOR_ARG0 (monitor_entry takes over before
* anything else in the new process starts). It must not be a plain fork of the compositor: that
* child lives as long as the app does and keeps a snapshot of the compositor's whole heap, which
* stays allocated (and went to swap) as the compositor's own copy changes. */
#define MONITOR_ARG0 "chandra-launch-monitor"
#define MONITOR_FD 3 /* its end of the report socket */
/* Start `cmd` with its stdout and stderr on a pipe, copy that output to our stderr (the session
* log) and keep its last 2 KiB; report on `report` when it exits (see mp_spawn_watch). `bus` is
* the compositor's private session bus, "" when there is none. */
static void launch_monitor(const char *cmd, const char *display, const char *bus, int report) {
prctl(PR_SET_NAME, "chandra-launch");
signal(SIGCHLD, SIG_DFL);
signal(SIGPIPE, SIG_IGN); /* the compositor may stop listening; keep copying output */
/* Hold nothing of the compositor's (DRM, seat, client sockets) for the app's lifetime. */
for (int fd = 3; fd < 1024; fd++)
if (fd != report) close(fd);
fcntl(report, F_SETFD, FD_CLOEXEC);
snprintf(private_bus, sizeof(private_bus), "%s", bus);
int out[2];
uint64_t t0 = mono_ms();
pid_t app = pipe2(out, O_CLOEXEC) == 0 ? fork() : -1;
if (app < 0) {
char msg[128];
int n = snprintf(msg, sizeof(msg), "exit 127 0\nfork failed: %s\n", strerror(errno));
write_all(report, msg, (size_t)n);
_exit(1);
}
if (app == 0) {
/* The command's stdout and stderr go to the monitor; none of our other fds leak into it. */
dup2(out[1], 1);
dup2(out[1], 2);
for (int fd = 3; fd < 1024; fd++) close(fd);
app_env(cmd, display);
execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
_exit(127);
}
close(out[1]);
fcntl(out[0], F_SETFL, fcntl(out[0], F_GETFL, 0) | O_NONBLOCK);
struct tail t = { .len = 0 };
int open_out = 1, reported = 0;
while (open_out || !reported) {
if (open_out) {
struct pollfd p = { .fd = out[0], .events = POLLIN };
poll(&p, 1, reported ? -1 : 100);
open_out = pump(out[0], &t);
}
if (!reported) {
int st = 0;
pid_t r = waitpid(app, &st, open_out ? WNOHANG : 0);
if (r == app) {
if (open_out) open_out = pump(out[0], &t); /* what it wrote just before exiting */
char head[64];
int n = WIFSIGNALED(st)
? snprintf(head, sizeof(head), "signal %d %llu\n", WTERMSIG(st), (unsigned long long)(mono_ms() - t0))
: snprintf(head, sizeof(head), "exit %d %llu\n", WEXITSTATUS(st), (unsigned long long)(mono_ms() - t0));
write_all(report, head, (size_t)n);
fprintf(stderr, "chandra-launch: `%s` %s", cmd, head); /* "... exit 1 230\n" in the session log */
write_all(report, t.buf, t.len);
close(report);
reported = 1;
}
}
}
_exit(0);
}
/* glibc passes constructors main's arguments. */
__attribute__((constructor)) static void monitor_entry(int argc, char **argv) {
if (argc == 4 && argv && strcmp(argv[0], MONITOR_ARG0) == 0)
launch_monitor(argv[1], argv[2], argv[3], MONITOR_FD);
}
/* Launch `cmd` like mp_spawn, watched: a small monitor process (above) starts it with its stdout
* and stderr on a pipe, copies that output to our stderr (the session log, as before) and keeps its
* last 2 KiB. When `sh -c cmd` exits the monitor reports on the returned socket (non-blocking):
* "exit CODE MS\n" or "signal NUM MS\n" (MS = how long it ran), then the output tail.
* The socket closes after that. The monitor keeps copying output from anything the command left
@@ -648,67 +723,24 @@ int32_t mp_spawn_watch(moonbit_bytes_t cmd_bytes, moonbit_bytes_t display_bytes)
int rep[2];
/* A socket, not a pipe: the compositor reads every fd it watches with recvmsg. */
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, rep) != 0) return -1;
int out[2];
if (pipe2(out, O_CLOEXEC) != 0) { close(rep[0]); close(rep[1]); return -1; }
pid_t mon = fork();
if (mon < 0) { close(rep[0]); close(rep[1]); close(out[0]); close(out[1]); return -1; }
if (mon == 0) {
setsid();
signal(SIGCHLD, SIG_DFL);
signal(SIGPIPE, SIG_IGN); /* the compositor may stop listening; keep copying output */
/* Hold nothing of the compositor's (DRM, seat, client sockets) for the app's lifetime. */
for (int fd = 3; fd < 1024; fd++) {
if (fd != rep[1] && fd != out[0] && fd != out[1]) close(fd);
}
uint64_t t0 = mono_ms();
pid_t app = fork();
if (app < 0) {
char msg[128];
int n = snprintf(msg, sizeof(msg), "exit 127 0\nfork failed: %s\n", strerror(errno));
write_all(rep[1], msg, (size_t)n);
_exit(1);
}
if (app == 0) {
/* The command's stdout and stderr go to the monitor; none of our other fds leak into it. */
dup2(out[1], 1);
dup2(out[1], 2);
for (int fd = 3; fd < 1024; fd++) close(fd);
app_env(cmd, display);
execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
_exit(127);
}
close(out[1]);
fcntl(out[0], F_SETFL, fcntl(out[0], F_GETFL, 0) | O_NONBLOCK);
struct tail t = { .len = 0 };
int open_out = 1, reported = 0;
while (open_out || !reported) {
if (open_out) {
struct pollfd p = { .fd = out[0], .events = POLLIN };
poll(&p, 1, reported ? -1 : 100);
open_out = pump(out[0], &t);
}
if (!reported) {
int st = 0;
pid_t r = waitpid(app, &st, open_out ? WNOHANG : 0);
if (r == app) {
if (open_out) open_out = pump(out[0], &t); /* what it wrote just before exiting */
char head[64];
int n = WIFSIGNALED(st)
? snprintf(head, sizeof(head), "signal %d %llu\n", WTERMSIG(st), (unsigned long long)(mono_ms() - t0))
: snprintf(head, sizeof(head), "exit %d %llu\n", WEXITSTATUS(st), (unsigned long long)(mono_ms() - t0));
write_all(rep[1], head, (size_t)n);
fprintf(stderr, "chandra-launch: `%s` %s", cmd, head); /* "... exit 1 230\n" in the session log */
write_all(rep[1], t.buf, t.len);
close(rep[1]);
reported = 1;
}
}
}
_exit(0);
}
posix_spawn_file_actions_t actions;
posix_spawnattr_t attr;
posix_spawn_file_actions_init(&actions);
posix_spawnattr_init(&attr);
/* (Onto itself, when it already is MONITOR_FD, this clears close-on-exec.) */
posix_spawn_file_actions_adddup2(&actions, rep[1], MONITOR_FD);
posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSID);
char *argv[] = { MONITOR_ARG0, cmd, display, private_bus, NULL };
pid_t mon;
/* /proc/self/exe: this very build, even after an update replaced the file. */
int err = posix_spawn(&mon, "/proc/self/exe", &actions, &attr, argv, environ);
posix_spawn_file_actions_destroy(&actions);
posix_spawnattr_destroy(&attr);
close(rep[1]);
close(out[0]);
close(out[1]);
if (err != 0) {
close(rep[0]);
return -1;
}
set_nonblock_cloexec(rep[0]);
return rep[0];
}