Files
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

109 lines
4.6 KiB
MoonBit

///| Watched launches (compositor/shell/launch.mbt): an app that fails to start shows an error
/// notice with the command, its exit status and the last lines it printed.
///| Step `comp` until the shell shows an error notice or `ms` pass.
fn wait_notice(comp : @host.ChandraCompositor, ms : Int) -> Array[String] {
let t0 = @sys.monotonic_ms()
while (@sys.monotonic_ms() - t0).to_int() < ms {
comp.step()
if comp.shell.notice_frames > 0 && comp.shell.notice_lines.length() > 0 {
return comp.shell.notice_lines
}
@sys.sleep_ms(20)
}
[]
}
///|
test "a launch that fails shows the command, the exit status and what it printed" {
let comp = new_comp("launch-fail")
// What V's Flatpak kwi did: bwrap can't set up the sandbox, prints why and exits 1.
let cmd = "echo starting; for i in 1 2 3; do echo \"bwrap: Can't find source path /run/user/1000/doc/by-app/org.kde.kwi: No such file or directory\" >&2; done; exit 1"
assert_true(comp.shell.start_app("org.kde.kwi", cmd))
assert_eq(comp.shell.launch_fds().length(), 1)
let lines = wait_notice(comp, 5000)
assert_true(lines.length() >= 3)
assert_true(lines[0].has_prefix("Could not start org.kde.kwi: exited with status 1 after"))
assert_eq(lines[1], "Command: " + cmd)
assert_eq(lines[2], "starting")
assert_eq(
lines[3],
"bwrap: Can't find source path /run/user/1000/doc/by-app/org.kde.kwi: No such file or directory (3 times)",
)
// Drawn above the panel: a red edge at the notice's left side.
let f = comp.compose_frame()
let mut red = 0
for y = 0; y < 720; y = y + 1 {
for x = 0; x < 1280; x = x + 1 {
let i = (y * f.width + x) * 4
if f.data[i].to_int() > 200 && f.data[i + 1].to_int() < 100 && f.data[i + 2].to_int() < 100 {
red = red + 1
}
}
}
assert_true(red > 100)
ignore(@sys.write_file_bytes("/tmp/chandra-launch-notice.png", @renderer.encode_png(f)))
assert_eq(comp.shell.launch_fds().length(), 0)
}
///|
test "a command that isn't installed says so" {
let comp = new_comp("launch-127")
assert_true(comp.shell.start_app("nothere", "chandra-no-such-program-xyz --flag"))
let lines = wait_notice(comp, 5000)
assert_eq(lines[0], "Could not start nothere: failed: command not found (status 127)")
assert_true(lines[2].contains("chandra-no-such-program-xyz"))
}
///|
test "an app killed by a signal while starting says which" {
let comp = new_comp("launch-sig")
assert_true(comp.shell.start_app("crashy", "kill -SEGV $$"))
let lines = wait_notice(comp, 5000)
assert_true(lines[0].has_prefix("Could not start crashy: was killed by SIGSEGV after"))
assert_eq(lines[2], "It printed nothing. More in ~/.local/state/chandra/session.log")
}
///|
test "an app that exits 0 without ever opening a window is reported, after a grace period" {
let comp = new_comp("launch-nowin")
assert_true(comp.shell.start_app("handoff", "echo 'handed the launch to the running instance'"))
let t0 = @sys.monotonic_ms()
let lines = wait_notice(comp, 6000)
assert_true((@sys.monotonic_ms() - t0).to_int() >= 1900) // waited for a window first
assert_true(lines[0].has_prefix("Could not start handoff: exited (status 0) after"))
assert_true(lines[0].has_suffix("without opening a window"))
assert_eq(lines[2], "handed the launch to the running instance")
}
///|
test "an app that keeps running is not reported, and its output still reaches the log" {
let comp = new_comp("launch-ok")
assert_true(comp.shell.start_app("stays", "echo up; sleep 3"))
let lines = wait_notice(comp, 1500)
assert_eq(lines, [])
assert_eq(comp.shell.launch_fds().length(), 1)
}
///|
test "the launch monitor is a fresh process, not a copy of the compositor" {
let comp = new_comp("launch-monitor")
// The app's parent is the monitor: it prints that process's command line.
assert_true(comp.shell.start_app("who", "tr '\\0' ' ' < /proc/$PPID/cmdline; echo; exit 3"))
let lines = wait_notice(comp, 5000)
assert_true(lines[0].has_prefix("Could not start who: exited with status 3 after"))
assert_true(lines[2].has_prefix("chandra-launch-monitor "))
}
///|
test "problems chandra-session found at login show as an error notice" {
let comp = new_comp("session-errors")
comp.shell.show_session_errors("")
assert_eq(comp.shell.notice_lines, [])
comp.shell.show_session_errors(
"Flatpak apps can't start: the document portal answered but nothing is mounted at /run/user/1000/doc\n",
)
assert_eq(comp.shell.notice_lines[0], "Flatpak apps can't start: the document portal answered but nothing is mounted at /run/user/1000/doc")
assert_eq(comp.shell.notice_lines.length(), 2)
}