From 2752b81e3613b9bdfc597d9c2947997661804b70 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 06:29:52 +0000 Subject: [PATCH] Session: wait for the document portal's mount before reopening apps; say when it's broken V's session.log had kwi (a Flatpak) failing with "bwrap: Can't find source path /run/user/1000/doc/by-app/org.kde.kwi". At login chandra-session restarts xdg-desktop-portal, and xdg-document-portal stopped and started with it in the same second (V's journal, 21:36:12); a Flatpak started in that gap, such as one session restore reopens, finds no FUSE mount to bind into its sandbox. When flatpak is installed, chandra-session now asks the document portal for its mount point (which also starts it), waits up to 5 s for the FUSE mount and checks it answers. If it doesn't, the reason goes to the log and into CHANDRA_SESSION_ERRORS, which the shell shows as an error notice once the desktop is up (DesktopShell::show_session_errors). A missing dbus-send is reported too. Tested: the snippet against a real xdg-document-portal on a dbus-run-session bus (mounted after 0 waits) and with an unreachable bus (error text); the notice in compositor/host/launch_test.mbt and headless with build/wasmhost. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DPL5hiGJWeuHiTgtoyB4kb --- compositor/host/launch_test.mbt | 12 ++++++++++++ compositor/shell/widgets.mbt | 17 +++++++++++++++++ session/chandra-session | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/compositor/host/launch_test.mbt b/compositor/host/launch_test.mbt index eb45e66..c241f2f 100644 --- a/compositor/host/launch_test.mbt +++ b/compositor/host/launch_test.mbt @@ -84,3 +84,15 @@ test "an app that keeps running is not reported, and its output still reaches th assert_eq(lines, []) assert_eq(comp.shell.launch_fds().length(), 1) } + +///| +test "problems chandra-session found at login show as an error notice" { + let comp = new_comp("session-errors") + comp.shell.show_session_errors("") + assert_eq(comp.shell.notice_lines, []) + comp.shell.show_session_errors( + "Flatpak apps can't start: the document portal answered but nothing is mounted at /run/user/1000/doc\n", + ) + assert_eq(comp.shell.notice_lines[0], "Flatpak apps can't start: the document portal answered but nothing is mounted at /run/user/1000/doc") + assert_eq(comp.shell.notice_lines.length(), 2) +} diff --git a/compositor/shell/widgets.mbt b/compositor/shell/widgets.mbt index ef1933e..387b952 100644 --- a/compositor/shell/widgets.mbt +++ b/compositor/shell/widgets.mbt @@ -368,6 +368,23 @@ fn DesktopShell::ensure_booted(self : DesktopShell) -> Unit { ignore(self.load_path(path)) } } + self.show_session_errors(@sys.getenv("CHANDRA_SESSION_ERRORS")) +} + +///| Show what chandra-session found broken before the desktop started (CHANDRA_SESSION_ERRORS, +/// one problem per line; the document portal Flatpak apps need, for one). +pub fn DesktopShell::show_session_errors(self : DesktopShell, errors : String) -> Unit { + let lines : Array[String] = [] + for l in errors.split("\n") { + let t = l.trim(chars=" \t").to_string() + if t != "" { + lines.push(t) + } + } + if lines.length() > 0 { + lines.push("Found at login by chandra-session; details in ~/.local/state/chandra/session.log") + self.say_error(lines) + } } ///| Load the module file at `path` into the panel (any number of instances may be loaded). diff --git a/session/chandra-session b/session/chandra-session index e860b2a..91e2519 100755 --- a/session/chandra-session +++ b/session/chandra-session @@ -149,6 +149,39 @@ if command -v systemctl >/dev/null 2>&1; then systemctl --user try-restart xdg-desktop-portal.service >>"$log" 2>&1 || true fi +# Flatpak apps need the document portal's FUSE mount: `flatpak run` binds +# $XDG_RUNTIME_DIR/doc/by-app/APP into the sandbox, and bwrap dies ("Can't find source path +# .../doc/by-app/org.kde.kwi") while nothing is mounted there. The portal restart above stops and +# starts xdg-document-portal too, so wait (up to 5 s) for its mount before the compositor reopens +# the last session's apps. A portal that doesn't come up is shown on screen when the desktop +# starts (CHANDRA_SESSION_ERRORS) and logged, not left for each Flatpak launch to hit. +session_errors= +doc_mounted() { # PATH -> success when a FUSE file system is mounted at PATH + awk -v m="$1" '$5 == m && / - fuse/ { f = 1 } END { exit !f }' /proc/self/mountinfo +} +if command -v flatpak >/dev/null 2>&1; then + doc="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/doc" + if ! command -v dbus-send >/dev/null 2>&1; then + session_errors="Flatpak apps can't be checked: dbus-send is not installed (package dbus-bin or dbus)" + elif ! out=$(dbus-send --session --print-reply=literal --dest=org.freedesktop.portal.Documents \ + /org/freedesktop/portal/documents org.freedesktop.portal.Documents.GetMountPoint 2>&1); then + # Asking also starts xdg-document-portal when it isn't running. + session_errors="Flatpak apps can't start: the document portal (xdg-document-portal) did not answer: $out" + else + i=0 + while [ $i -lt 50 ] && ! doc_mounted "$doc"; do sleep 0.1; i=$((i + 1)); done + if ! doc_mounted "$doc"; then + session_errors="Flatpak apps can't start: the document portal answered but nothing is mounted at $doc (see: journalctl --user -b -u xdg-document-portal)" + elif ! ls "$doc/by-app" >/dev/null 2>&1; then + session_errors="Flatpak apps can't start: the document portal's mount at $doc does not answer ($(ls "$doc/by-app" 2>&1 | head -1))" + else + echo "chandra-session: document portal mounted at $doc after $i waits" >>"$log" + fi + fi + [ -n "$session_errors" ] && echo "chandra-session: $session_errors" >>"$log" +fi +export CHANDRA_SESSION_ERRORS="$session_errors" + # Hand the PAM-started gnome-keyring daemon (already unlocked by the login password) its session # components, so it owns org.freedesktop.secrets on our bus. (oo7-daemon needs none of this: it # is started by systemd and unlocked over its PAM socket; see docs/keyring.md.) Harmless if it isn't installed,