Files
chandra/docs/reloading.md
T
Claude 68164ff75e
CI / test (push) Failing after 16s
Reload panel modules from outside the session: control socket, chandra-ctl, SIGUSR1
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
2026-11-25 20:57:40 +00:00

121 lines
6.1 KiB
Markdown

# Picking up a new build in a running session
After `just install-session` the files under `/usr/local` are new, but the running session keeps
what it loaded at startup. What can be swapped without logging out:
| Part | Reload without logout? | How |
|---|---|---|
| Panel modules (`/usr/local/share/chandra/modules/*.wasm`) | yes | `just reload` / `chandra-ctl reload`, `kill -USR1`, or Settings → Panel (remove, add again) |
| `compositor.wasm` (shell, WM, portal backend) | no | restart the session |
| `chandra-host` (`wasmhost`) | no | restart the session |
| `config/`, themes, assets | not checked | restart the session to be sure |
The control socket and the SIGUSR1 handler are part of the compositor and host, so a session
started before they existed has neither. Log out and back in once after installing a build that
has them; from then on module reloads need no logout. Don't send SIGUSR1 to a session older than
that: without the handler the signal's default action ends the session.
## Reloading panel modules
```
just install-session # or: just modules, with install-session-dev
just reload # reload every panel module from disk
just reload clock-digital # just one
```
`just reload` runs `session/chandra-ctl reload`; `just install-session` also installs it as
`/usr/local/bin/chandra-ctl`:
```
chandra-ctl reload # every loaded module
chandra-ctl reload NAME # modules whose name (clock-digital), slot (w3) or path is NAME
chandra-ctl list # loaded modules in panel order: slot, module, path
chandra-ctl ping # is the compositor answering?
chandra-ctl help
```
It prints one line per module and exits 0 when everything reloaded, 1 when the compositor
reported an error (a module that failed to load, an unknown name), 2 when it could not reach a
compositor:
```
$ chandra-ctl reload
reloaded w10 launcher
...
failed w8 my-widget: Can't load module: not a wasm file
error 1 of 9 failed to reload
```
What a reload does, for each module: load the `.wasm` again from the path it was loaded from
(for an installed session, `/usr/local/share/chandra/modules`), apply its saved settings
(`:module-config` in `~/.config/chandra/state.edn`, the same values a login gives it), put the new
instance in the old one's place in the panel, then drop the old instance. A module that fails to
load is left loaded as it was, and the reply says why. The new instance gets a new slot id
(`w3` becomes `w12`) and starts fresh: an open popup closes. The module catalog is scanned again,
so a module installed since login shows up under Settings → Panel → Add. A notice above the panel
says what happened ("Reloaded 9 modules", or the error).
The reload goes through the same shell code as Settings → Panel (`DesktopShell::reload_modules`
and `replace_slot` in `compositor/shell/widgets.mbt`), so a module that imports no new host
capability needs nothing else. A module that needs a host interface the running host lacks fails
to load and stays as it was: that needs a new session.
### SIGUSR1
Sending SIGUSR1 to the host process reloads every module, like `chandra-ctl reload`:
```
pkill -USR1 -x chandra-host
```
The result goes to the session log (the host's stderr) and the notice above the panel; there is
nobody to answer. The handler is installed by the host at startup (`sys.take-sigusr1`, which the
compositor polls every pass). The signal interrupts the idle loop's wait, so it is handled
promptly.
## The control socket
The compositor listens on `$XDG_RUNTIME_DIR/chandra/<wayland socket name>.sock`. The login
session's Wayland socket is `wayland-moon-0`, so its control socket is
`/run/user/$UID/chandra/wayland-moon-0.sock`. `$XDG_RUNTIME_DIR` is private to the user (mode
0700), so no other user can connect. Without `XDG_RUNTIME_DIR` there is no control socket (the log
says so). When the path is already answered by another running compositor (a nested `just run`
with the same socket name), the newcomer leaves it alone and has no control socket.
`chandra-ctl` finds the socket from `--socket PATH`, then `$CHANDRA_CONTROL`, then
`$WAYLAND_DISPLAY` (inside the session), and otherwise takes the only socket in
`$XDG_RUNTIME_DIR/chandra` (from a text console or ssh as the same user, with
`XDG_RUNTIME_DIR` set). With several compositors running it lists them and asks for `--socket`.
The protocol is lines of text, so anything that can talk to a unix socket can drive it, for
example `echo reload | socat - UNIX-CONNECT:$XDG_RUNTIME_DIR/chandra/wayland-moon-0.sock`. A client
writes one command per line; each reply is zero or more lines of output and then one line
starting with `ok` or `error`. Several commands may be sent on one connection.
| Command | Reply |
|---|---|
| `ping` | `ok pong` |
| `list` | `<slot> <module> <path>` per module, then `ok <n> modules` |
| `reload [NAME]` | `reloaded <new slot> <module>` or `failed <slot> <module>: <why>` per module, then `ok <n> reloaded` or `error ...` |
| `help` | the commands, then `ok` |
The compositor handles the socket in its main loop (`compositor/host/control.mbt`, from
`ChandraCompositor::step`), and the listening socket and its clients are among the fds the idle
loop waits on, so an idle desktop still sleeps (about 0.4% CPU, unchanged) and wakes for a
command. It is the same code on the MoonBit host, the Rust host and the native build.
## Not reloadable
`chandra-host` and `compositor.wasm` are read once when `chandra-session` starts them. There is no
reload request or file watcher for them. `docs/ARCHITECTURE.md` says so ("no hot reload on file
change"), and the WM hot-swap in `docs/compositor-design.md` (replaying `list<window-state>` into a
new WM component's `sync-windows`) is a design: nothing triggers it yet. Changes to the compositor,
WM, portal backend or host need a new session: log out and back in.
## Status
Tested headless on both hosts (`build/wasmhost` and `host-wasm`) with the modules from
`build/components`: `ping`, `list`, `reload`, `reload NAME`, a module replaced by a broken file
(reported, old instance kept), SIGUSR1, and idle CPU before and after. Tests:
`compositor/host/control_test.mbt`. Not yet run against an installed login session.