fix(appd): drop --wait from the systemd-run scope launch

When a systemd user manager was detected, weft-appd launched the runtime
with `systemd-run --user --scope --wait ...`. systemd rejects that
combination while parsing options ("--wait may not be combined with
--scope"), so every app launch on such a host exited immediately and the
session stopped without the runtime ever starting.

A scope already runs the command in the foreground: systemd-run registers
the scope and then executes the command in its own process, so stdout and
the exit status still reach appd without --wait.

The options now live in one constant. A Linux test runs the installed
systemd-run with them against an empty runtime directory and bus address
and requires it to reach the manager connection; option and combination
errors stop it earlier. The test does not validate the -p property values
or start a scope. It requires systemd-run to be installed.

This keeps the existing ownership scope for the runtime only; placing the
app shell and helpers under the same owner is separate lifecycle work.

Validation:
- systemd-run 255: the test passes with the new options and fails with
  the old --wait (and with --pty) using systemd's own message
- cargo test -p weft-appd (38 passed), clippy -D warnings, fmt --check
This commit is contained in:
Marco Allegretti 2026-10-09 00:43:35 +00:00
parent b99cc73a47
commit ef7d105118
No known key found for this signature in database

View file

@ -70,6 +70,22 @@ pub(crate) async fn spawn_ipc_relay(
const READY_TIMEOUT: Duration = Duration::from_secs(30); const READY_TIMEOUT: Duration = Duration::from_secs(30);
/// `systemd-run` options that place the runtime in a transient user scope.
///
/// A scope runs the command in the foreground as systemd-run's own process,
/// so its stdout and exit status reach appd directly. `--wait` only applies
/// to service units; systemd rejects it together with `--scope`.
const SYSTEMD_SCOPE_ARGS: &[&str] = &[
"--user",
"--scope",
"--collect",
"--slice=weft-apps.slice",
"-p",
"CPUQuota=200%",
"-p",
"MemoryMax=512M",
];
fn systemd_cgroup_available() -> bool { fn systemd_cgroup_available() -> bool {
if std::env::var("WEFT_DISABLE_CGROUP").is_ok() { if std::env::var("WEFT_DISABLE_CGROUP").is_ok() {
return false; return false;
@ -240,19 +256,7 @@ pub(crate) async fn supervise(
let mut cmd = if systemd_cgroup_available() { let mut cmd = if systemd_cgroup_available() {
let mut c = tokio::process::Command::new("systemd-run"); let mut c = tokio::process::Command::new("systemd-run");
c.args([ c.args(SYSTEMD_SCOPE_ARGS).arg("--").arg(&bin);
"--user",
"--scope",
"--wait",
"--collect",
"--slice=weft-apps.slice",
"-p",
"CPUQuota=200%",
"-p",
"MemoryMax=512M",
"--",
&bin,
]);
c c
} else { } else {
tokio::process::Command::new(&bin) tokio::process::Command::new(&bin)
@ -437,3 +441,41 @@ async fn drain_stderr(stderr: tokio::process::ChildStderr, session_id: u64) {
tracing::warn!(session_id, stderr = %line, "app stderr"); tracing::warn!(session_id, stderr = %line, "app stderr");
} }
} }
#[cfg(all(test, target_os = "linux"))]
mod tests {
use super::SYSTEMD_SCOPE_ARGS;
/// systemd-run checks its options and their combinations before it
/// connects to the service manager. Run against an empty runtime
/// directory and bus address, it must get as far as the connection
/// attempt; any option error ends earlier with a different message. This
/// does not validate the `-p` property values, which only the manager
/// checks, and never starts a real scope.
#[test]
fn systemd_run_accepts_scope_options() {
let empty = std::env::temp_dir().join(format!("weft-scope-test-{}", std::process::id()));
std::fs::create_dir_all(&empty).unwrap();
let output = std::process::Command::new("systemd-run")
.args(SYSTEMD_SCOPE_ARGS)
.args(["--", "true"])
.env_clear()
.env("PATH", std::env::var_os("PATH").unwrap_or_default())
.env("LC_ALL", "C")
.env("XDG_RUNTIME_DIR", &empty)
.env(
"DBUS_SESSION_BUS_ADDRESS",
format!("unix:path={}/bus", empty.display()),
)
.output();
std::fs::remove_dir_all(&empty).unwrap();
let output = output.expect("systemd-run is required to check the scope options");
let stderr = String::from_utf8_lossy(&output.stderr);
// systemd 255 says "Failed to connect to bus"; later releases name the
// transport, e.g. "Failed to connect to user scope bus via local transport".
assert!(
stderr.starts_with("Failed to connect to"),
"systemd-run did not reach the manager connection: {stderr}"
);
}
}