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:
1 parent
44688453d7
commit
ada9850b8d
11 files changed
+211
-75
No files matched your search
+9
-3
@@ -4,7 +4,11 @@
|
||||
/// component reach the one handler given to `set_handler` as (id, arguments).
|
||||
|
||||
///|
|
||||
extern "C" fn c_set_dispatch(f : FuncRef[(Int, Bytes) -> Bytes]) = "wt_set_dispatch"
|
||||
extern "C" fn c_set_dispatch(f : FuncRef[(Int, Int) -> Bytes]) = "wt_set_dispatch"
|
||||
|
||||
///|
|
||||
#borrow(out)
|
||||
extern "C" fn c_take_args(out : Bytes) = "wt_take_args"
|
||||
|
||||
///|
|
||||
extern "C" fn c_last_error() -> Bytes = "wt_last_error"
|
||||
@@ -33,7 +37,9 @@ let handler : Ref[(Int, Array[Val]) -> Array[Val]] = Ref(fn(id, _) {
|
||||
})
|
||||
|
||||
///|
|
||||
fn dispatch(id : Int, args : Bytes) -> Bytes {
|
||||
fn dispatch(id : Int, len : Int) -> Bytes {
|
||||
let args = Bytes::make(len, 0)
|
||||
c_take_args(args)
|
||||
encode((handler.val)(id, decode(args)))
|
||||
}
|
||||
|
||||
@@ -52,7 +58,7 @@ pub fn load(path : String) -> Result[Unit, String] {
|
||||
if c_init() != 0 {
|
||||
return Err(last_error())
|
||||
}
|
||||
c_set_dispatch(fn(id, args) { dispatch(id, args) })
|
||||
c_set_dispatch(fn(id, len) { dispatch(id, len) })
|
||||
if c_load(cstr(path)) != 0 {
|
||||
return Err(last_error())
|
||||
}
|
||||
|
||||
+29
-6
@@ -27,7 +27,11 @@
|
||||
|
||||
#define KIND_BLOB 0x80
|
||||
|
||||
typedef moonbit_bytes_t (*wt_dispatch_t)(int32_t id, moonbit_bytes_t args);
|
||||
/* The dispatcher takes only the arguments' length and fetches them with wt_take_args, so no heap
|
||||
* object passes from C into MoonBit: whether a MoonBit function owns or borrows a parameter is
|
||||
* the compiler's choice (it borrowed this one, and every call's arguments leaked). What it
|
||||
* returns is ours to release. */
|
||||
typedef moonbit_bytes_t (*wt_dispatch_t)(int32_t id, int32_t len);
|
||||
|
||||
static wasm_engine_t *engine;
|
||||
static wasmtime_store_t *store;
|
||||
@@ -37,6 +41,8 @@ static wasmtime_component_t *component;
|
||||
static wasmtime_component_instance_t instance;
|
||||
static int have_instance;
|
||||
static wt_dispatch_t dispatch;
|
||||
static const uint8_t *pending_args; /* of the host call being dispatched */
|
||||
static size_t pending_len;
|
||||
static char last_error[16384]; /* wasmtime backtraces are long; the cause comes last */
|
||||
|
||||
static void set_error(const char *what, wasmtime_error_t *e) {
|
||||
@@ -139,10 +145,14 @@ static moonbit_bytes_t to_moonbit(const Buf *b) {
|
||||
return out;
|
||||
}
|
||||
|
||||
static void put_message(Buf *b, const wasmtime_component_val_t *vals, size_t n) {
|
||||
put_u32(b, (uint32_t)n);
|
||||
for (size_t i = 0; i < n; i++) enc(b, &vals[i]);
|
||||
}
|
||||
|
||||
static moonbit_bytes_t enc_message(const wasmtime_component_val_t *vals, size_t n) {
|
||||
Buf b = {0};
|
||||
put_u32(&b, (uint32_t)n);
|
||||
for (size_t i = 0; i < n; i++) enc(&b, &vals[i]);
|
||||
put_message(&b, vals, n);
|
||||
moonbit_bytes_t out = to_moonbit(&b);
|
||||
free(b.p);
|
||||
return out;
|
||||
@@ -300,10 +310,16 @@ static wasmtime_error_t *host_call(void *env, wasmtime_context_t *c, const wasmt
|
||||
int32_t id = (int32_t)(intptr_t)env;
|
||||
if (profiling < 0) { profiling = getenv("WASMHOST_PROFILE") != NULL; prof_last = now_ns(); }
|
||||
uint64_t t0 = profiling ? now_ns() : 0;
|
||||
moonbit_bytes_t in = enc_message(args, nargs);
|
||||
uint64_t t1 = 0, inlen = profiling ? (uint64_t)Moonbit_array_length(in) : 0;
|
||||
Buf in = {0};
|
||||
put_message(&in, args, nargs);
|
||||
uint64_t t1 = 0, inlen = in.len;
|
||||
if (profiling) t1 = now_ns();
|
||||
moonbit_bytes_t out = dispatch(id, in); /* takes `in` */
|
||||
pending_args = in.p;
|
||||
pending_len = in.len;
|
||||
moonbit_bytes_t out = dispatch(id, (int32_t)in.len);
|
||||
pending_args = NULL;
|
||||
pending_len = 0;
|
||||
free(in.p);
|
||||
uint64_t t2 = profiling ? now_ns() : 0;
|
||||
int bad = dec_message(out, results, nresults);
|
||||
moonbit_decref(out);
|
||||
@@ -323,6 +339,13 @@ static wasmtime_error_t *host_call(void *env, wasmtime_context_t *c, const wasmt
|
||||
|
||||
void wt_set_dispatch(wt_dispatch_t f) { dispatch = f; }
|
||||
|
||||
/* Copy the arguments of the call being dispatched into `out` (made by MoonBit, borrowed here). */
|
||||
void wt_take_args(moonbit_bytes_t out) {
|
||||
size_t n = (size_t)Moonbit_array_length(out);
|
||||
if (n > pending_len) n = pending_len;
|
||||
if (n) memcpy(out, pending_args, n);
|
||||
}
|
||||
|
||||
moonbit_bytes_t wt_last_error(void) {
|
||||
size_t n = strlen(last_error);
|
||||
moonbit_bytes_t out = moonbit_make_bytes((int32_t)n, 0);
|
||||
|
||||
Reference in new issue
Block a user