CI / test (push) Failing after 16s
Until now a module reload was only reachable by clicking Settings > Panel inside the session. Now: - The compositor listens on $XDG_RUNTIME_DIR/chandra/<wayland socket name>.sock (the login session's is wayland-moon-0.sock) with a line protocol: ping, list, reload [NAME], help. Replies end with an "ok ..." or "error ..." line. It lives in compositor/host/control.mbt, runs from ChandraCompositor::step, and its fds join the idle loop's wait set, so an idle desktop still sleeps (measured ~0.4% CPU on wasmhost, ~0.1% on host-wasm) and wakes for a command. A path another live compositor answers on is left alone. - DesktopShell::reload_modules(name) reloads every loaded module (or those whose module id, stem, slot or path is NAME) in place via replace_slot: same panel position, saved :module-config applied, a module that fails to load stays as it was and the error is reported to the caller and shown as a notice. The catalog is rescanned. - SIGUSR1 to the host reloads every module (result in the log and the notice): new WIT function sys.take-sigusr1, implemented in compositor/sys (native build and wasmhost) and host-wasm; the handler has no SA_RESTART so it interrupts the idle poll. - session/chandra-ctl (installed to /usr/local/bin), `just reload [NAME]`, docs/reloading.md, README. - Tests: compositor/host/control_test.mbt (protocol, reload all / by name / by slot, order and settings kept, failing module kept, busy socket). Checked headless on both hosts with chandra-ctl and kill -USR1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hjLJtvYcgW48okL6qQ
162 lines
6.4 KiB
MoonBit
162 lines
6.4 KiB
MoonBit
///| The control socket (control.mbt): chandra-ctl's commands and module reloads.
|
|
/// Needs the modules: run scripts/build-modules.sh (just test does).
|
|
|
|
///| A compositor with its control socket open in a fresh directory.
|
|
fn control_comp(tag : String) -> (@host.ChandraCompositor, String) raise {
|
|
let comp = new_comp("control-" + tag)
|
|
let dir = "/tmp/chandra-control-" + tag + "-" + @sys.getpid().to_string()
|
|
let path = @host.control_socket_path(dir, "/run/user/1000/wayland-moon-0")
|
|
assert_eq(path, dir + "/chandra/wayland-moon-0.sock")
|
|
assert_true(comp.start_control(path))
|
|
(comp, path)
|
|
}
|
|
|
|
///| Send `line` over a new connection, let the compositor run a pass, and read its reply lines.
|
|
fn ask(comp : @host.ChandraCompositor, path : String, line : String) -> Array[String] raise {
|
|
let fd = @sys.unix_connect(path)
|
|
assert_true(fd >= 0)
|
|
assert_true(@sys.sendmsg_fd(fd, @utf8.encode(line + "\n"), -1) > 0)
|
|
let raw : Array[Byte] = []
|
|
let buf : FixedArray[Byte] = FixedArray::make(4096, b'\x00')
|
|
let mut tries = 0
|
|
let mut done = false
|
|
while !done && tries < 50 {
|
|
comp.step()
|
|
while @sys.poll_read(fd, 5) > 0 {
|
|
let (n, _) = @sys.recvmsg_fd(fd, buf, 4096)
|
|
if n <= 0 {
|
|
break
|
|
}
|
|
for i = 0; i < n; i = i + 1 {
|
|
raw.push(buf[i])
|
|
}
|
|
}
|
|
let text = @utf8.decode_lossy(Bytes::from_array(raw))
|
|
let lines = text.split("\n").map(s => s.to_string()).filter(s => s != "").collect()
|
|
if text.has_suffix("\n") && lines.length() > 0 {
|
|
let last = lines[lines.length() - 1]
|
|
done = last.has_prefix("ok") || last.has_prefix("error")
|
|
}
|
|
tries = tries + 1
|
|
}
|
|
@sys.close_fd(fd)
|
|
let text = @utf8.decode_lossy(Bytes::from_array(raw))
|
|
text.split("\n").map(s => s.to_string()).filter(s => s != "").collect()
|
|
}
|
|
|
|
///| The slot ids of the panel, in order.
|
|
fn panel_order(comp : @host.ChandraCompositor) -> Array[String] {
|
|
comp.shell.loaded_modules().map(m => m.1)
|
|
}
|
|
|
|
///|
|
|
test "control socket path follows the Wayland socket's name" {
|
|
assert_eq(@host.control_socket_path("/run/user/1000", "/run/user/1000/wayland-moon-0"), "/run/user/1000/chandra/wayland-moon-0.sock")
|
|
assert_eq(@host.control_socket_path("/run/user/1000", "wayland-1"), "/run/user/1000/chandra/wayland-1.sock")
|
|
assert_eq(@host.control_socket_path("", "/tmp/wayland-moon-0"), "")
|
|
}
|
|
|
|
///|
|
|
test "ping, list and errors over the control socket" {
|
|
let (comp, path) = control_comp("basic")
|
|
assert_eq(ask(comp, path, "ping"), ["ok pong"])
|
|
let list = ask(comp, path, "list")
|
|
let n = comp.shell.loaded_modules().length()
|
|
assert_true(n > 3)
|
|
assert_eq(list.length(), n + 1)
|
|
assert_eq(list[n], "ok " + n.to_string() + " modules")
|
|
assert_true(list.iter().any(l => l.contains(" clock-digital ") && l.has_suffix("clock-digital.wasm")))
|
|
assert_eq(ask(comp, path, "bogus"), ["error unknown command 'bogus' (try: help)"])
|
|
assert_eq(ask(comp, path, "reload nope"), ["error no loaded module named 'nope'"])
|
|
assert_eq(ask(comp, path, " "), ["error empty command (try: help)"])
|
|
assert_eq(ask(comp, path, "help").last(), Some("ok"))
|
|
// Several commands on one connection get one reply each, in order.
|
|
let both = ask(comp, path, "ping\nping")
|
|
assert_eq(both, ["ok pong", "ok pong"])
|
|
comp.close()
|
|
}
|
|
|
|
///|
|
|
test "reload every module keeps the panel's order" {
|
|
let (comp, path) = control_comp("all")
|
|
let before = panel_order(comp)
|
|
let slots_before = comp.shell.loaded_modules().map(m => m.0)
|
|
let reply = ask(comp, path, "reload")
|
|
assert_eq(reply.length(), before.length() + 1)
|
|
assert_eq(reply.last(), Some("ok " + before.length().to_string() + " reloaded"))
|
|
assert_eq(panel_order(comp), before)
|
|
// Every instance is new.
|
|
for s in comp.shell.loaded_modules().map(m => m.0) {
|
|
assert_false(slots_before.contains(s))
|
|
}
|
|
// And still draws.
|
|
ignore(comp.compose_frame())
|
|
assert_true(rect_of(comp, "clock-digital").w > 0)
|
|
comp.close()
|
|
}
|
|
|
|
///|
|
|
test "reload one module by name keeps its place and its settings" {
|
|
let (comp, path) = control_comp("one")
|
|
let slot = comp.shell.slot_of("clock-digital")
|
|
comp.shell.set_module_setting(slot, "week-numbers", "on")
|
|
let before = panel_order(comp)
|
|
let others = comp.shell.loaded_modules().filter(m => m.1 != "clock-digital").map(m => m.0)
|
|
let reply = ask(comp, path, "reload clock-digital")
|
|
assert_eq(reply.length(), 2)
|
|
assert_true(reply[0].has_prefix("reloaded w"))
|
|
assert_eq(reply[1], "ok 1 reloaded")
|
|
assert_eq(panel_order(comp), before)
|
|
assert_true(comp.shell.slot_of("clock-digital") != slot)
|
|
// Only the clock was replaced.
|
|
assert_eq(comp.shell.loaded_modules().filter(m => m.1 != "clock-digital").map(m => m.0), others)
|
|
// The new instance got the saved setting: its calendar shows week numbers.
|
|
ignore(comp.compose_frame())
|
|
click_module(comp, "clock-digital", 1U)
|
|
finish_slide(comp)
|
|
assert_true(cal_texts(comp, "clock-digital").contains("Wk"))
|
|
// By slot works too.
|
|
let s2 = comp.shell.slot_of("logout")
|
|
assert_eq(ask(comp, path, "reload " + s2).last(), Some("ok 1 reloaded"))
|
|
comp.close()
|
|
}
|
|
|
|
///|
|
|
test "a module that fails to reload stays loaded and the error is reported" {
|
|
let (comp, path) = control_comp("fail")
|
|
let dir = "/tmp/chandra-control-badmod-" + @sys.getpid().to_string()
|
|
ignore(@sys.mkdir_p(dir))
|
|
let good = @sys.read_file_bytes("build/components/logout.wasm")
|
|
assert_true(good.length() > 0)
|
|
let copy = dir + "/logout.wasm"
|
|
ignore(@sys.write_file_bytes(copy, good))
|
|
assert_true(comp.shell.load_module(copy))
|
|
let before = panel_order(comp)
|
|
ignore(@sys.write_file_bytes(copy, @utf8.encode("not wasm")))
|
|
let reply = ask(comp, path, "reload " + copy)
|
|
assert_eq(reply.length(), 2)
|
|
assert_true(reply[0].has_prefix("failed w"))
|
|
assert_true(reply[0].contains("Can't load module"))
|
|
assert_eq(reply[1], "error 1 of 1 failed to reload")
|
|
assert_eq(panel_order(comp), before)
|
|
// The other modules still reload; the result says one failed.
|
|
let all = ask(comp, path, "reload")
|
|
assert_eq(all.last(), Some("error 1 of " + before.length().to_string() + " failed to reload"))
|
|
assert_eq(panel_order(comp), before)
|
|
comp.close()
|
|
}
|
|
|
|
///|
|
|
test "a second compositor leaves a live control socket alone" {
|
|
let (comp, path) = control_comp("busy")
|
|
let other = new_comp("control-busy2")
|
|
assert_false(other.start_control(path))
|
|
assert_eq(ask(comp, path, "ping"), ["ok pong"])
|
|
other.close()
|
|
comp.close()
|
|
// Closing removes the socket; a new compositor can take the path again.
|
|
let third = new_comp("control-busy3")
|
|
assert_true(third.start_control(path))
|
|
third.close()
|
|
}
|