Launches that fail say why: command, exit status and what it printed
CI / test (push) Canceled after 17m11s

V: "kwi not starting from app menu or dock". Kwi is a Flatpak, and bwrap died
before it opened a window ("Can't find source path /run/user/1000/doc/by-app/
org.kde.kwi"); Chandra forked apps and forgot them, so the failure only reached
the session log and the click did nothing visible.

Every app the Applications menu, the dock, Super+1..9 and session restore start
now runs under a small monitor (sys spawn-watch, compositor/sys/sys_stub.c): it
copies the app's output to the session log as before, keeps its last 2 KiB,
and reports how sh -c CMD exited on a socket the main loop wakes on. The shell
(compositor/shell/launch.mbt) shows a red error notice for 15 s, and logs it,
when the app exits non-zero or by a signal within 30 s, or exits 0 without ever
opening a window here (after a 2 s grace for single-instance hand-offs): the
first line says what happened, then the command and the last lines it printed,
repeats folded ("(3 times)"). Status 127/126 say "command not found" / "not
executable". Launched apps no longer inherit the compositor's fds.

chandra-ctl launch ID starts a desktop app exactly as the dock does.

Tests: compositor/host/launch_test.mbt (bwrap-style failure incl. drawn notice,
127, SIGSEGV, exit 0 without a window, an app that keeps running), unit tests
in launch.mbt. Checked end to end with build/wasmhost + compositor.wasm
headless and a Flatpak-style kwi desktop file: chandra-ctl launch org.kde.kwi
shows the notice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiGJWeuHiTgtoyB4kb
This commit is contained in:
Claude committed 2026-11-26 05:48:39 +00:00
1 parent 77fcc4db1c
commit 50149e6b99
18 files changed
+686 -30

No files matched your search

+146 -16
View File
@@ -464,6 +464,27 @@ static void start_private_bus(void) {
}
}
/* The environment of a launched app (in the forked child): our Wayland socket, and the
* private bus when nested (see start_private_bus). */
static void app_env(const char *cmd, const char *display) {
/* SIG_IGN survives exec; apps (e.g. terminals that wait() on a helper) need the default. */
signal(SIGCHLD, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
/* Flatpak apps reach the host (shell lookup, portals) through org.freedesktop.Flatpak
* on the real session bus, so they keep it; the sandbox isolates their instances anyway. */
if (private_bus[0] && strstr(cmd, "flatpak run") == NULL) setenv("DBUS_SESSION_BUS_ADDRESS", private_bus, 1);
setenv("WAYLAND_DISPLAY", display, 1);
unsetenv("DISPLAY");
/* Make toolkits pick our Wayland socket, not X11 or the host session. */
setenv("XDG_SESSION_TYPE", "wayland", 1);
setenv("QT_QPA_PLATFORM", "wayland", 1);
setenv("GDK_BACKEND", "wayland", 1);
setenv("SDL_VIDEODRIVER", "wayland", 1);
setenv("MOZ_ENABLE_WAYLAND", "1", 1);
setenv("ELECTRON_OZONE_PLATFORM_HINT", "wayland", 1);
setenv("CLUTTER_BACKEND", "wayland", 1);
}
/* Launch `cmd` detached, with WAYLAND_DISPLAY pointing at our socket. Returns 0 on fork success. */
MOONBIT_FFI_EXPORT
int32_t mp_spawn(moonbit_bytes_t cmd_bytes, moonbit_bytes_t display_bytes) {
@@ -477,28 +498,137 @@ int32_t mp_spawn(moonbit_bytes_t cmd_bytes, moonbit_bytes_t display_bytes) {
if (pid < 0) return -1;
if (pid == 0) {
setsid();
/* SIG_IGN survives exec; apps (e.g. terminals that wait() on a helper) need the default. */
signal(SIGCHLD, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
/* Flatpak apps reach the host (shell lookup, portals) through org.freedesktop.Flatpak
* on the real session bus, so they keep it; the sandbox isolates their instances anyway. */
if (private_bus[0] && strstr(cmd, "flatpak run") == NULL) setenv("DBUS_SESSION_BUS_ADDRESS", private_bus, 1);
setenv("WAYLAND_DISPLAY", display, 1);
unsetenv("DISPLAY");
/* Make toolkits pick our Wayland socket, not X11 or the host session. */
setenv("XDG_SESSION_TYPE", "wayland", 1);
setenv("QT_QPA_PLATFORM", "wayland", 1);
setenv("GDK_BACKEND", "wayland", 1);
setenv("SDL_VIDEODRIVER", "wayland", 1);
setenv("MOZ_ENABLE_WAYLAND", "1", 1);
setenv("ELECTRON_OZONE_PLATFORM_HINT", "wayland", 1);
setenv("CLUTTER_BACKEND", "wayland", 1);
app_env(cmd, display);
execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
_exit(127);
}
return 0;
}
static uint64_t mono_ms(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}
static void write_all(int fd, const char *p, size_t n) {
while (n > 0) {
ssize_t w = write(fd, p, n);
if (w < 0 && errno == EINTR) continue;
if (w <= 0) return;
p += w;
n -= (size_t)w;
}
}
/* Keep the last LAUNCH_TAIL bytes an app wrote. */
#define LAUNCH_TAIL 2048
struct tail { char buf[LAUNCH_TAIL]; size_t len; };
static void tail_add(struct tail *t, const char *p, size_t n) {
if (n >= LAUNCH_TAIL) { memcpy(t->buf, p + n - LAUNCH_TAIL, LAUNCH_TAIL); t->len = LAUNCH_TAIL; return; }
if (t->len + n > LAUNCH_TAIL) {
size_t drop = t->len + n - LAUNCH_TAIL;
memmove(t->buf, t->buf + drop, t->len - drop);
t->len -= drop;
}
memcpy(t->buf + t->len, p, n);
t->len += n;
}
/* Copy what is readable on `from` to our stderr (the session log) and into `t`. 0 at EOF. */
static int pump(int from, struct tail *t) {
char buf[4096];
for (;;) {
ssize_t n = read(from, buf, sizeof(buf));
if (n > 0) { write_all(2, buf, (size_t)n); tail_add(t, buf, (size_t)n); continue; }
if (n < 0 && errno == EINTR) continue;
if (n < 0 && errno == EAGAIN) return 1;
return 0;
}
}
/* Launch `cmd` like mp_spawn, watched: a small monitor process starts it with its stdout and
* stderr on a pipe, copies that output to our stderr (the session log, as before) and keeps its
* last 2 KiB. When `sh -c cmd` exits the monitor reports on the returned socket (non-blocking):
* "exit CODE MS\n" or "signal NUM MS\n" (MS = how long it ran), then the output tail.
* The socket closes after that. The monitor keeps copying output from anything the command left
* running, until nothing holds the pipe open. Returns our end, or -1 when nothing could start. */
MOONBIT_FFI_EXPORT
int32_t mp_spawn_watch(moonbit_bytes_t cmd_bytes, moonbit_bytes_t display_bytes) {
char cmd[2048];
char display[512];
bytes_to_cstr(cmd_bytes, cmd, sizeof(cmd));
bytes_to_cstr(display_bytes, display, sizeof(display));
signal(SIGCHLD, SIG_IGN); /* the monitor is never our zombie */
start_private_bus();
int rep[2];
/* A socket, not a pipe: the compositor reads every fd it watches with recvmsg. */
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, rep) != 0) return -1;
int out[2];
if (pipe2(out, O_CLOEXEC) != 0) { close(rep[0]); close(rep[1]); return -1; }
pid_t mon = fork();
if (mon < 0) { close(rep[0]); close(rep[1]); close(out[0]); close(out[1]); return -1; }
if (mon == 0) {
setsid();
signal(SIGCHLD, SIG_DFL);
signal(SIGPIPE, SIG_IGN); /* the compositor may stop listening; keep copying output */
/* Hold nothing of the compositor's (DRM, seat, client sockets) for the app's lifetime. */
for (int fd = 3; fd < 1024; fd++) {
if (fd != rep[1] && fd != out[0] && fd != out[1]) close(fd);
}
uint64_t t0 = mono_ms();
pid_t app = fork();
if (app < 0) {
char msg[128];
int n = snprintf(msg, sizeof(msg), "exit 127 0\nfork failed: %s\n", strerror(errno));
write_all(rep[1], msg, (size_t)n);
_exit(1);
}
if (app == 0) {
/* The command's stdout and stderr go to the monitor; none of our other fds leak into it. */
dup2(out[1], 1);
dup2(out[1], 2);
for (int fd = 3; fd < 1024; fd++) close(fd);
app_env(cmd, display);
execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
_exit(127);
}
close(out[1]);
fcntl(out[0], F_SETFL, fcntl(out[0], F_GETFL, 0) | O_NONBLOCK);
struct tail t = { .len = 0 };
int open_out = 1, reported = 0;
while (open_out || !reported) {
if (open_out) {
struct pollfd p = { .fd = out[0], .events = POLLIN };
poll(&p, 1, reported ? -1 : 100);
open_out = pump(out[0], &t);
}
if (!reported) {
int st = 0;
pid_t r = waitpid(app, &st, open_out ? WNOHANG : 0);
if (r == app) {
if (open_out) open_out = pump(out[0], &t); /* what it wrote just before exiting */
char head[64];
int n = WIFSIGNALED(st)
? snprintf(head, sizeof(head), "signal %d %llu\n", WTERMSIG(st), (unsigned long long)(mono_ms() - t0))
: snprintf(head, sizeof(head), "exit %d %llu\n", WEXITSTATUS(st), (unsigned long long)(mono_ms() - t0));
write_all(rep[1], head, (size_t)n);
fprintf(stderr, "chandra-launch: `%s` %s", cmd, head); /* "... exit 1 230\n" in the session log */
write_all(rep[1], t.buf, t.len);
close(rep[1]);
reported = 1;
}
}
}
_exit(0);
}
close(rep[1]);
close(out[0]);
close(out[1]);
set_nonblock_cloexec(rep[0]);
return rep[0];
}
/* Names of "*.desktop" files in `dir`, newline-separated. */
MOONBIT_FFI_EXPORT
moonbit_bytes_t mp_list_desktop(moonbit_bytes_t dir_bytes) {