Reload panel modules from outside the session: control socket, chandra-ctl, SIGUSR1
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
This commit is contained in:
Claude committed 2026-11-25 20:57:40 +00:00
1 parent 0fc2598d27
commit 68164ff75e
21 files changed
+844 -31

No files matched your search

+127
View File
@@ -0,0 +1,127 @@
#!/usr/bin/env python3
"""chandra-ctl: talk to a running Chandra compositor over its control socket.
chandra-ctl reload load every panel module again from disk
chandra-ctl reload NAME only the module(s) whose name, slot or path is NAME
chandra-ctl list the loaded panel modules: slot, module, path
chandra-ctl ping check the compositor answers
The socket is $XDG_RUNTIME_DIR/chandra/<wayland socket name>.sock (the session's is
wayland-moon-0.sock). It is found in this order: --socket PATH, $CHANDRA_CONTROL, the one for
$WAYLAND_DISPLAY, else the only socket in $XDG_RUNTIME_DIR/chandra. Prints the compositor's reply;
exits 0 when it ends in "ok", 1 on "error", 2 when no compositor could be reached.
See docs/reloading.md.
"""
import glob
import os
import socket
import sys
USAGE = "usage: chandra-ctl [--socket PATH] [--quiet] {reload [NAME] | list | ping | help}"
def runtime_dir():
d = os.environ.get("XDG_RUNTIME_DIR", "")
if d:
return d
d = "/run/user/%d" % os.getuid()
return d if os.path.isdir(d) else ""
def find_socket(explicit):
if explicit:
return explicit
env = os.environ.get("CHANDRA_CONTROL", "")
if env:
return env
rt = runtime_dir()
if not rt:
sys.exit("chandra-ctl: XDG_RUNTIME_DIR is not set; pass --socket PATH")
base = os.path.join(rt, "chandra")
wd = os.environ.get("WAYLAND_DISPLAY", "")
if wd:
p = os.path.join(base, os.path.basename(wd) + ".sock")
if os.path.exists(p):
return p
socks = sorted(glob.glob(os.path.join(base, "*.sock")))
if len(socks) == 1:
return socks[0]
if not socks:
print("chandra-ctl: no control socket in %s (is a Chandra session running a build "
"with the control socket? it needs one restart after installing it)" % base,
file=sys.stderr)
sys.exit(2)
print("chandra-ctl: several compositors are running; pick one with --socket:", file=sys.stderr)
for s in socks:
print(" " + s, file=sys.stderr)
sys.exit(2)
def main(argv):
explicit = ""
quiet = False
args = []
i = 0
while i < len(argv):
a = argv[i]
if a == "--socket" and i + 1 < len(argv):
explicit = argv[i + 1]
i += 2
continue
if a.startswith("--socket="):
explicit = a.split("=", 1)[1]
elif a in ("-q", "--quiet"):
quiet = True
elif a in ("-h", "--help"):
print(__doc__.strip())
return 0
else:
args.append(a)
i += 1
if not args:
print(USAGE, file=sys.stderr)
return 2
command = " ".join(args)
if "\n" in command:
print("chandra-ctl: one command at a time", file=sys.stderr)
return 2
path = find_socket(explicit)
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.settimeout(30) # reloading every module takes about a second
try:
s.connect(path)
except OSError as e:
print("chandra-ctl: can't connect to %s: %s" % (path, e.strerror or e), file=sys.stderr)
return 2
s.sendall((command + "\n").encode())
s.shutdown(socket.SHUT_WR)
data = b""
try:
while True:
chunk = s.recv(4096)
if not chunk:
break
data += chunk
# The reply ends with its "ok ..." / "error ..." line.
lines = data.decode("utf-8", "replace").splitlines()
if data.endswith(b"\n") and lines and (lines[-1].startswith("ok") or lines[-1].startswith("error")):
break
except socket.timeout:
print("chandra-ctl: no reply from %s" % path, file=sys.stderr)
return 2
finally:
s.close()
lines = data.decode("utf-8", "replace").splitlines()
if not lines:
print("chandra-ctl: the compositor closed the connection without a reply", file=sys.stderr)
return 2
last = lines[-1]
for l in lines:
if quiet and l is not last:
continue
print(l, file=sys.stderr if l.startswith("error") or l.startswith("failed") else sys.stdout)
return 0 if last.startswith("ok") else 1
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
+7
View File
@@ -21,6 +21,8 @@ Installs:
/usr/local/bin/chandra (--native) release native compositor binary
/usr/local/bin/chandra-session session launcher (starts the host if installed)
/usr/local/bin/chandra-root run one graphical app as root in the session (run0)
/usr/local/bin/chandra-ctl talk to the running session: `chandra-ctl reload` loads
the panel modules again (docs/reloading.md)
/usr/local/share/chandra/{config,wit,assets}/ files the compositor reads at runtime
/usr/local/share/chandra/modules/*.wasm the panel's wasm modules (build them first: just modules)
/usr/share/wayland-sessions/chandra.desktop display-manager entry
@@ -45,6 +47,7 @@ targets = [
"/usr/local/bin/chandra-host",
"/usr/local/bin/chandra-session",
"/usr/local/bin/chandra-root",
"/usr/local/bin/chandra-ctl",
"/usr/share/wayland-sessions/chandra.desktop",
"/usr/share/xdg-desktop-portal/portals/chandra.portal",
"/usr/share/xdg-desktop-portal/chandra-portals.conf",
@@ -106,6 +109,7 @@ if not native:
install(component, os.path.join(share, "compositor.wasm"))
install(os.path.join(root, "session/chandra-session"), "/usr/local/bin/chandra-session")
install(os.path.join(root, "session/chandra-root"), "/usr/local/bin/chandra-root")
install(os.path.join(root, "session/chandra-ctl"), "/usr/local/bin/chandra-ctl")
# The display-manager entry stays a real file: some managers don't follow symlinks there.
put(os.path.join(root, "session/chandra.desktop"), "/usr/share/wayland-sessions/chandra.desktop", 0o644)
# xdg-desktop-portal reads backends only from /usr/share (or $XDG_DESKTOP_PORTAL_DIR), not
@@ -130,6 +134,7 @@ dst = os.path.join(share, "modules")
if linked:
link(mods, dst)
print("done: pick 'Chandra' in the session menu; rebuild with `just compositor-wasm %s modules`" % host_build.split()[1])
print("(in a running session, `just modules reload` picks up rebuilt panel modules)")
sys.exit(0)
if os.path.lexists(dst):
shutil.rmtree(dst) if os.path.isdir(dst) and not os.path.islink(dst) else os.remove(dst)
@@ -143,3 +148,5 @@ if n == 0:
sys.exit("no modules in build/components - run `just modules` first")
print("installed", n, "modules in", dst)
print("done: pick 'Chandra' in the session menu of your login screen")
print("already in a Chandra session? `just reload` (chandra-ctl reload) picks up the new panel modules;")
print("the compositor and host themselves need a new session (docs/reloading.md)")