Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions apps/desktop/src/app/DesktopCliShim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const fakeAppImage = (canMount = true, app = REPORTER) => {
// Stays up after a signal until the test lets it finish, like a server shutting down.
const SLOW_SERVER = [
"#!/bin/sh",
"trap 'echo stopping >&3; read go; echo stopped >&3; exit 0' TERM HUP",
"trap 'echo stopping >&3; read go; echo \"stopped $go\" >&3; exit 7' TERM HUP",
"echo ready",
"while :; do sleep 0.05; done",
"",
Expand Down Expand Up @@ -101,9 +101,16 @@ const lines = (stream: NodeJS.ReadableStream) => {
};

describe("renderCliShim", () => {
it.skipIf(!hasSetsid).each(["SIGTERM", "SIGHUP"] as const)(
"keeps the AppImage mounted until the server exits after %s to the process group",
async (signal) => {
// Ctrl-C, an SSH hangup, or a supervisor signals the process group. systemd's default KillMode
// also signals the mounter, and KillMode=mixed signals only the launcher.
it.skipIf(!hasSetsid).each([
["SIGTERM", "the process group"],
["SIGHUP", "the process group"],
["SIGTERM", "every process"],
["SIGTERM", "the launcher"],
] as const)(
"keeps the AppImage mounted until the server exits after %s to %s",
async (signal, target) => {
const { root, shim } = fakeAppImage(true, SLOW_SERVER);
const child = NodeChildProcess.spawn(shim, [], {
detached: true,
Expand All @@ -114,12 +121,12 @@ describe("renderCliShim", () => {
try {
const stdout = lines(child.stdout!);
const events = lines(child.stdio[3] as NodeJS.ReadableStream);
const exited = new Promise((resolve) => child.on("close", resolve));
const exited = new Promise<number | null>((resolve) => child.on("close", resolve));
await stdout.until((line) => line === "ready");
mounter = Number(NodeFS.readFileSync(NodePath.join(root, "mounter.pid"), "utf8"));

// Ctrl-C in a terminal, a closed SSH session, or a service manager signals the whole group.
process.kill(-child.pid!, signal);
process.kill(target === "the launcher" ? child.pid! : -child.pid!, signal);
if (target === "every process") process.kill(mounter, signal);
// The shell runs pending traps in ascending signal number, and WINCH outnumbers TERM and
// HUP, so a mounter that also got the group signal unmounts before it answers this probe.
process.kill(mounter, "SIGWINCH");
Expand All @@ -128,8 +135,8 @@ describe("renderCliShim", () => {

await events.until((line) => line === "stopping");
child.stdin!.end("go\n");
await exited;
expect(events.seen.slice(-2)).toEqual(["stopped", "unmounted"]);
expect(await exited).toBe(7);
expect(events.seen.slice(-2)).toEqual(["stopped go", "unmounted"]);
} finally {
for (const pid of [-child.pid!, mounter]) {
if (pid === undefined) continue;
Expand All @@ -156,6 +163,8 @@ describe("renderCliShim", () => {
const result = run(shim, ["--version"]);
expect(result.status).toBe(3);
expect(result.stdout).toContain(`appimage=${appImage}`);
const closedStdin = NodeChildProcess.spawnSync("sh", ["-c", '"$0" --version <&-', shim]);
expect(closedStdin.status).toBe(3);
});

it("keeps the install's T3 home under sudo, unless one is set", () => {
Expand Down
27 changes: 24 additions & 3 deletions apps/desktop/src/app/DesktopCliShim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ export const renderCliShim = (input: {
// Extract-and-run would make the runtime start the app instead of mounting it.
"unset APPIMAGE_EXTRACT_AND_RUN",
// Its own session keeps a group signal (Ctrl-C, SSH hangup) from unmounting it under the
// running server; the traps unmount it after. Without setsid the extract fallback runs.
// running server, and ignoring TERM survives a stop sent to every process (systemd's default).
// Without setsid the extract fallback runs.
"work=$(mktemp -d) || exit 1",
"trap '' TERM",
'setsid "$app" --appimage-mount >"$work/mount" 2>/dev/null &',
"mounter=$!",
`trap 'kill "$mounter" 2>/dev/null; rm -rf "$work"' EXIT`,
// It unmounts itself on TERM, which it now ignores, so the trap unmounts it. Lazily, like the
// runtime: the server's own children may still be exiting.
`trap '[ -z "$mount" ] || fusermount3 -uz "$mount" 2>/dev/null || fusermount -uz "$mount" 2>/dev/null; kill -HUP "$mounter" 2>/dev/null; rm -rf "$work"' EXIT`,
"trap 'exit 130' INT TERM HUP",
"mount=",
'while kill -0 "$mounter" 2>/dev/null; do',
Expand All @@ -111,7 +115,24 @@ export const renderCliShim = (input: {
"fi",
// The runtime sets this when it launches the app; the CLI skips its path-keyed compile cache on it.
'export APPIMAGE="$app"',
run("$mount"),
// The shell must outlive the CLI to unmount, so it forwards TERM and HUP; the terminal sends
// Ctrl-C to the CLI directly. A background child would read /dev/null, so pass stdin on fd 9.
// With stdin closed, `exec 9<&0` would end the shell; `command` makes it a normal error.
"{ command exec 9<&0; } 2>/dev/null || exec 9</dev/null",
`${run("$mount")} <&9 9<&- &`,
"cli=$!",
"exec 9<&-",
`trap 'kill -TERM "$cli" 2>/dev/null' TERM`,
`trap 'kill -HUP "$cli" 2>/dev/null' HUP`,
"trap '' INT",
"status=0",
'wait "$cli" || status=$?',
// A trapped signal interrupts wait; wait again for the CLI's own exit status.
'while kill -0 "$cli" 2>/dev/null; do',
" status=0",
' wait "$cli" || status=$?',
"done",
'exit "$status"',
"",
].join("\n");
};
Expand Down
Loading