Launches that fail say why: command, exit status and what it printed
CI / test (push) Canceled after 17m11s
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:
18 files changed
+686
-30
No files matched your search
@@ -0,0 +1,303 @@
|
||||
///| Watched app launches. Every app the launcher, the dock, Super+1..9 or session restore starts
|
||||
/// runs under a small monitor (`@sys.spawn_watch`) that reports how it exited. An app that fails
|
||||
/// to start never fails quietly: when it exits with an error (or a signal) within
|
||||
/// `launch_watch_ms`, or exits cleanly without ever showing a window, the shell shows a notice
|
||||
/// with the command, the exit status and the last lines it printed, and logs the same.
|
||||
|
||||
///| How long after starting an app its exit counts as a failed launch (later exits are the user
|
||||
/// quitting it, or a crash the session log keeps).
|
||||
let launch_watch_ms : UInt64 = 30000UL
|
||||
|
||||
///| After an app exits 0 without a window, how long to wait for one (an app that hands the launch
|
||||
/// to its running instance opens the window from there, a moment later).
|
||||
let launch_grace_ms : UInt64 = 2000UL
|
||||
|
||||
///|
|
||||
struct LaunchWatch {
|
||||
/// Report socket from the monitor (-1 once closed).
|
||||
mut fd : Int
|
||||
id : String
|
||||
name : String
|
||||
command : String
|
||||
started : UInt64
|
||||
/// Whether the app already had a window here when it was started.
|
||||
had_window : Bool
|
||||
buf : Array[Byte]
|
||||
/// Parsed report, once the pipe hit EOF.
|
||||
mut report : LaunchReport?
|
||||
/// When to check (exited 0 without a window yet) whether a window has appeared since.
|
||||
mut check_at : UInt64
|
||||
}
|
||||
|
||||
///| How a launched command ended, as the monitor reported it.
|
||||
pub(all) struct LaunchReport {
|
||||
/// "exit" or "signal" ("" when the report was unreadable).
|
||||
how : String
|
||||
/// Exit status or signal number.
|
||||
code : Int
|
||||
/// Milliseconds it ran.
|
||||
ran_ms : Int
|
||||
/// The end of what it printed (stdout and stderr).
|
||||
output : String
|
||||
} derive(Eq, Debug)
|
||||
|
||||
///| Parse the monitor's report: "exit CODE MS\n" or "signal NUM MS\n", then the output tail.
|
||||
pub fn parse_launch_report(data : Bytes) -> LaunchReport {
|
||||
let text = @utf8.decode_lossy(data)
|
||||
let (head, rest) = match text.find("\n") {
|
||||
Some(i) => (text[:i].to_string(), text[i + 1:].to_string())
|
||||
None => (text, "")
|
||||
}
|
||||
let parts : Array[String] = head.split(" ").map(x => x.to_string()).collect()
|
||||
if parts.length() != 3 || (parts[0] != "exit" && parts[0] != "signal") {
|
||||
return { how: "", code: -1, ran_ms: 0, output: text }
|
||||
}
|
||||
let num = fn(s : String) {
|
||||
let mut n = 0
|
||||
for c in s {
|
||||
if c < '0' || c > '9' {
|
||||
return -1
|
||||
}
|
||||
n = n * 10 + (c.to_int() - '0'.to_int())
|
||||
}
|
||||
n
|
||||
}
|
||||
{ how: parts[0], code: num(parts[1]), ran_ms: num(parts[2]), output: rest }
|
||||
}
|
||||
|
||||
///| The last `max` meaningful lines of `output`: blank lines dropped, a line repeated right after
|
||||
/// itself shown once with "(N times)".
|
||||
pub fn output_tail(output : String, max : Int) -> Array[String] {
|
||||
let lines : Array[(String, Int)] = []
|
||||
for raw in output.split("\n") {
|
||||
let l = raw.trim(chars=" \t\r").to_string()
|
||||
if l.length() == 0 {
|
||||
continue
|
||||
}
|
||||
match lines.last() {
|
||||
Some((prev, n)) if prev == l => lines[lines.length() - 1] = (prev, n + 1)
|
||||
_ => lines.push((l, 1))
|
||||
}
|
||||
}
|
||||
let start = if lines.length() > max { lines.length() - max } else { 0 }
|
||||
let out : Array[String] = []
|
||||
for i = start; i < lines.length(); i = i + 1 {
|
||||
let (l, n) = lines[i]
|
||||
out.push(if n > 1 { l + " (" + n.to_string() + " times)" } else { l })
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
///| Signal names for the ones apps die of.
|
||||
fn signal_name(n : Int) -> String {
|
||||
match n {
|
||||
1 => "SIGHUP"
|
||||
2 => "SIGINT"
|
||||
4 => "SIGILL"
|
||||
5 => "SIGTRAP"
|
||||
6 => "SIGABRT"
|
||||
7 => "SIGBUS"
|
||||
8 => "SIGFPE"
|
||||
9 => "SIGKILL"
|
||||
11 => "SIGSEGV"
|
||||
13 => "SIGPIPE"
|
||||
15 => "SIGTERM"
|
||||
_ => "signal " + n.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
///| The notice for a failed launch of `name` (`command`), or None when `r` is not a failure.
|
||||
/// `window` says whether the app has a window here now.
|
||||
pub fn launch_failure(name : String, command : String, r : LaunchReport, window : Bool) -> Array[String]? {
|
||||
let secs = fn(ms : Int) {
|
||||
if ms < 1000 {
|
||||
ms.to_string() + " ms"
|
||||
} else {
|
||||
(ms / 1000).to_string() + "." + (ms % 1000 / 100).to_string() + " s"
|
||||
}
|
||||
}
|
||||
let what = match r.how {
|
||||
"exit" if r.code == 0 =>
|
||||
if window {
|
||||
return None
|
||||
} else {
|
||||
"exited (status 0) after " + secs(r.ran_ms) + " without opening a window"
|
||||
}
|
||||
"exit" if r.code == 127 => "failed: command not found (status 127)"
|
||||
"exit" if r.code == 126 => "failed: command not executable (status 126)"
|
||||
"exit" => "exited with status " + r.code.to_string() + " after " + secs(r.ran_ms)
|
||||
"signal" => "was killed by " + signal_name(r.code) + " after " + secs(r.ran_ms)
|
||||
_ => "ended without a report from its monitor"
|
||||
}
|
||||
let lines = ["Could not start " + name + ": " + what, "Command: " + command]
|
||||
let tail = output_tail(r.output, 4)
|
||||
if tail.length() == 0 {
|
||||
lines.push("It printed nothing. More in ~/.local/state/chandra/session.log")
|
||||
} else {
|
||||
lines.append(tail)
|
||||
}
|
||||
Some(lines)
|
||||
}
|
||||
|
||||
///| Whether some window here belongs to desktop app `id`.
|
||||
fn DesktopShell::has_window_for(self : DesktopShell, id : String) -> Bool {
|
||||
self.win_list.iter().any(e => resolve_app_id(e.app_id, self.exec, self.wm_classes) == Some(id))
|
||||
}
|
||||
|
||||
///| The catalog name of app `id` (its id when it has none).
|
||||
fn DesktopShell::app_name(self : DesktopShell, id : String) -> String {
|
||||
for a in self.apps {
|
||||
if a.id == id {
|
||||
return a.name
|
||||
}
|
||||
}
|
||||
id
|
||||
}
|
||||
|
||||
///| Start desktop app `id` with `command`, watched. False (with a notice) when nothing started.
|
||||
pub fn DesktopShell::start_app(self : DesktopShell, id : String, command : String) -> Bool {
|
||||
let name = self.app_name(id)
|
||||
let fd = @sys.spawn_watch(command, self.display)
|
||||
if fd < 0 {
|
||||
self.say_error(["Could not start " + name + ": the process could not be created", "Command: " + command])
|
||||
@sys.log("chandra: launch failed: " + name + " (" + id + "): could not create a process for `" + command + "`")
|
||||
return false
|
||||
}
|
||||
@sys.log("chandra: launching " + name + " (" + id + "): " + command)
|
||||
self.launches.push({
|
||||
fd,
|
||||
id,
|
||||
name,
|
||||
command,
|
||||
started: @sys.monotonic_ms(),
|
||||
had_window: self.has_window_for(id),
|
||||
buf: [],
|
||||
report: None,
|
||||
check_at: 0UL,
|
||||
})
|
||||
true
|
||||
}
|
||||
|
||||
///| Report sockets still open, for the main loop to wake on.
|
||||
pub fn DesktopShell::launch_fds(self : DesktopShell) -> Array[Int] {
|
||||
self.launches.filter(l => l.fd >= 0).map(l => l.fd)
|
||||
}
|
||||
|
||||
///| Milliseconds until a launch needs looking at again (-1: nothing pending).
|
||||
pub fn DesktopShell::launch_wake_ms(self : DesktopShell, now : UInt64) -> Int {
|
||||
let mut best = -1
|
||||
for l in self.launches {
|
||||
let at = if l.check_at > 0UL { l.check_at } else if l.fd >= 0 { l.started + launch_watch_ms } else { now }
|
||||
let d = if at <= now { 0 } else { (at - now).to_int() }
|
||||
if best < 0 || d < best {
|
||||
best = d
|
||||
}
|
||||
}
|
||||
best
|
||||
}
|
||||
|
||||
///| Read the monitors' reports and act on them: a notice for each failed launch.
|
||||
pub fn DesktopShell::launch_tick(self : DesktopShell) -> Unit {
|
||||
if self.launches.length() == 0 {
|
||||
return
|
||||
}
|
||||
let now = @sys.monotonic_ms()
|
||||
let buf : FixedArray[Byte] = FixedArray::make(4096, b'\x00')
|
||||
for l in self.launches {
|
||||
if l.fd < 0 {
|
||||
continue
|
||||
}
|
||||
let mut eof = false
|
||||
while @sys.poll_read(l.fd, 0) > 0 {
|
||||
let (n, extra) = @sys.recvmsg_fd(l.fd, buf, buf.length())
|
||||
if extra >= 0 {
|
||||
@sys.close_fd(extra)
|
||||
}
|
||||
if n == -1 {
|
||||
break
|
||||
}
|
||||
if n <= 0 {
|
||||
eof = true
|
||||
break
|
||||
}
|
||||
for i = 0; i < n; i = i + 1 {
|
||||
l.buf.push(buf[i])
|
||||
}
|
||||
}
|
||||
if eof {
|
||||
@sys.close_fd(l.fd)
|
||||
l.fd = -1
|
||||
let r = parse_launch_report(Bytes::from_array(l.buf))
|
||||
l.report = Some(r)
|
||||
let ran : UInt64 = if r.ran_ms > 0 { r.ran_ms.to_uint64() } else { 0UL }
|
||||
if ran > launch_watch_ms {
|
||||
// Ran a while: the user quit it, or it crashed; the monitor logged how.
|
||||
l.check_at = 0UL
|
||||
} else if r.how == "exit" && r.code == 0 && !l.had_window {
|
||||
l.check_at = now + launch_grace_ms
|
||||
} else {
|
||||
l.check_at = now
|
||||
}
|
||||
} else if now - l.started > launch_watch_ms {
|
||||
// Running past the watch window: it started. Stop listening (the monitor keeps logging).
|
||||
@sys.close_fd(l.fd)
|
||||
l.fd = -1
|
||||
l.check_at = 0UL
|
||||
}
|
||||
}
|
||||
let keep : Array[LaunchWatch] = []
|
||||
for l in self.launches {
|
||||
if l.fd >= 0 {
|
||||
keep.push(l)
|
||||
continue
|
||||
}
|
||||
match l.report {
|
||||
Some(r) if l.check_at > 0UL =>
|
||||
if l.check_at > now {
|
||||
keep.push(l)
|
||||
} else {
|
||||
let window = l.had_window || self.has_window_for(l.id)
|
||||
match launch_failure(l.name, l.command, r, window) {
|
||||
Some(lines) => {
|
||||
self.say_error(lines)
|
||||
@sys.log("chandra: launch failed: " + l.id + ": " + lines.join("\n "))
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
self.launches.clear()
|
||||
self.launches.append(keep)
|
||||
}
|
||||
|
||||
///|
|
||||
test "parse_launch_report reads the monitor's report" {
|
||||
let r = parse_launch_report(b"exit 1 230\nbwrap: no\n")
|
||||
assert_eq(r, { how: "exit", code: 1, ran_ms: 230, output: "bwrap: no\n" })
|
||||
let s = parse_launch_report(b"signal 11 5\n")
|
||||
assert_eq(s.how, "signal")
|
||||
assert_eq(s.code, 11)
|
||||
assert_eq(parse_launch_report(b"").how, "")
|
||||
}
|
||||
|
||||
///|
|
||||
test "output_tail keeps the last lines and folds repeats" {
|
||||
assert_eq(output_tail("a\n\nb\nb\nb\nc\n", 2), ["b (3 times)", "c"])
|
||||
assert_eq(output_tail("", 4), [])
|
||||
}
|
||||
|
||||
///|
|
||||
test "launch_failure: a clean exit is fine once the app has a window" {
|
||||
let r = { how: "exit", code: 0, ran_ms: 40, output: "" }
|
||||
assert_eq(launch_failure("Kwi", "kwi", r, true), None)
|
||||
match launch_failure("Kwi", "kwi", r, false) {
|
||||
Some(l) => assert_eq(l[0], "Could not start Kwi: exited (status 0) after 40 ms without opening a window")
|
||||
None => fail("expected a notice")
|
||||
}
|
||||
match launch_failure("Kwi", "kwi", { how: "exit", code: 2, ran_ms: 1500, output: "x" }, true) {
|
||||
Some(l) => assert_eq(l, ["Could not start Kwi: exited with status 2 after 1.5 s", "Command: kwi", "x"])
|
||||
None => fail("expected a notice")
|
||||
}
|
||||
}
|
||||
@@ -347,6 +347,11 @@ pub(all) struct DesktopShell {
|
||||
mut notice : String
|
||||
mut notice_frames : Int
|
||||
mut notice_start : Int
|
||||
/// A multi-line error notice (`say_error`): its lines, shown for `notice_secs`.
|
||||
mut notice_lines : Array[String]
|
||||
mut notice_secs : Int
|
||||
/// Apps started and still watched for a failed launch (launch.mbt).
|
||||
launches : Array[LaunchWatch]
|
||||
/// Right-click menu.
|
||||
mut menu_open : Bool
|
||||
mut menu_rect : @scene.Rect
|
||||
@@ -504,6 +509,9 @@ pub fn DesktopShell::new(display : String) -> DesktopShell {
|
||||
notice: "",
|
||||
notice_frames: 0,
|
||||
notice_start: 0,
|
||||
notice_lines: [],
|
||||
notice_secs: 4,
|
||||
launches: [],
|
||||
menu_open: false,
|
||||
menu_rect: zero,
|
||||
menu_title: "",
|
||||
@@ -1131,11 +1139,35 @@ pub fn DesktopShell::render(
|
||||
self.menu_rect = { x: 0, y: 0, w: 0, h: 0 }
|
||||
}
|
||||
if self.notice_frames > 0 {
|
||||
if (wall_secs() - self.notice_start + 86400) % 86400 >= 4 {
|
||||
if (wall_secs() - self.notice_start + 86400) % 86400 >= self.notice_secs {
|
||||
self.notice_frames = 0
|
||||
}
|
||||
}
|
||||
if self.notice_frames > 0 {
|
||||
if self.notice_frames > 0 && self.notice_lines.length() > 0 {
|
||||
// An error: its lines in a box above the panel's right end, a red edge on the left.
|
||||
let rows = notice_rows(self.notice_lines, 110)
|
||||
let fs = tokens.font_size.reinterpret_as_int() - 3
|
||||
let lh = fs + 6
|
||||
let mut longest = 0
|
||||
for r in rows {
|
||||
longest = if r.length() > longest { r.length() } else { longest }
|
||||
}
|
||||
let nw = longest * 7 + 32
|
||||
let nw = if nw > w - 24 { w - 24 } else { nw }
|
||||
let nh = rows.length() * lh + 16
|
||||
let red = @ui.color(229, 72, 77, 255)
|
||||
let ops = [
|
||||
@ui.fill_rect(@ui.rect(0, 0, nw, nh), tokens.palette.surface_popup, radius=tokens.radius.reinterpret_as_int()),
|
||||
@ui.stroke_rect(@ui.rect(0, 0, nw, nh), red, radius=tokens.radius.reinterpret_as_int()),
|
||||
@ui.fill_rect(@ui.rect(0, 0, 4, nh), red),
|
||||
]
|
||||
for i, r in rows {
|
||||
ops.push(
|
||||
@ui.text(16, 8 + i * lh + lh / 2, r, fs, if i == 0 { red } else { tokens.palette.foreground }),
|
||||
)
|
||||
}
|
||||
translate(ops, w - nw - 12, self.panel_rect.y - nh - 12, panel_ops)
|
||||
} else if self.notice_frames > 0 {
|
||||
let nw = self.notice.length() * 7 + 24
|
||||
translate(
|
||||
[
|
||||
|
||||
@@ -583,7 +583,7 @@ pub fn DesktopShell::launch_app_id(self : DesktopShell, app_id : String) -> Bool
|
||||
match resolve_app_id(app_id, self.exec, self.wm_classes) {
|
||||
Some(id) =>
|
||||
match self.command_for(id) {
|
||||
Some(cmd) => @sys.spawn(cmd, self.display)
|
||||
Some(cmd) => self.start_app(id, cmd)
|
||||
None => false
|
||||
}
|
||||
None => false
|
||||
|
||||
@@ -215,10 +215,44 @@ fn DesktopShell::panel_fits(self : DesktopShell) -> Bool {
|
||||
///| Show a short message above the panel for a few seconds.
|
||||
pub fn DesktopShell::say(self : DesktopShell, msg : String) -> Unit {
|
||||
self.notice = msg
|
||||
self.notice_lines = []
|
||||
self.notice_secs = 4
|
||||
self.notice_frames = 1
|
||||
self.notice_start = wall_secs()
|
||||
}
|
||||
|
||||
///| Show an error notice: the first line is what went wrong, the rest the details. It stays 15
|
||||
/// seconds, and the same lines go to the session log.
|
||||
pub fn DesktopShell::say_error(self : DesktopShell, lines : Array[String]) -> Unit {
|
||||
self.notice = lines.get(0).unwrap_or("")
|
||||
self.notice_lines = lines
|
||||
self.notice_secs = 15
|
||||
self.notice_frames = 1
|
||||
self.notice_start = wall_secs()
|
||||
}
|
||||
|
||||
///| The notice's lines as drawn: long ones wrapped at `max` characters, at most 10.
|
||||
fn notice_rows(lines : Array[String], max : Int) -> Array[String] {
|
||||
let out : Array[String] = []
|
||||
for l in lines {
|
||||
let chars = l.to_array()
|
||||
let mut i = 0
|
||||
while i < chars.length() || i == 0 {
|
||||
let end = if i + max < chars.length() { i + max } else { chars.length() }
|
||||
out.push(String::from_array(chars[i:end].to_array()))
|
||||
i = end
|
||||
if end == chars.length() {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if out.length() > 10 {
|
||||
out[out.length() - 10:].to_array()
|
||||
} else {
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
///| Seconds since local midnight (wall clock).
|
||||
fn wall_secs() -> Int {
|
||||
@sys.wall_time_field(3) * 3600 + @sys.wall_time_field(4) * 60 + @sys.wall_time_field(5)
|
||||
@@ -829,14 +863,15 @@ pub fn DesktopShell::set_power_timeouts(self : DesktopShell, screen_off : Int, s
|
||||
///| Start the catalog app `id` (a desktop file id) and remember it as the latest launch.
|
||||
fn DesktopShell::launch_catalog_app(self : DesktopShell, id : String) -> Unit {
|
||||
match self.command_for(id) {
|
||||
Some(bin) =>
|
||||
if @sys.spawn(bin, self.display) {
|
||||
Some(cmd) =>
|
||||
if self.start_app(id, cmd) {
|
||||
self.note_recent(id)
|
||||
self.apps_dirty = true
|
||||
} else {
|
||||
self.say("Could not start " + bin)
|
||||
}
|
||||
None => self.say("No executable for " + id)
|
||||
None => {
|
||||
self.say_error(["Could not start " + id + ": no installed .desktop file has that id"])
|
||||
@sys.log("chandra: launch failed: no desktop file for " + id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user