Skip to content

Commit 2990b89

Browse files
fix(desktop): AppImage quit no longer leaves zygotes running on an unmounted image
The AppImage runtime unmounts the image as soon as the main process exits, but Chromium's zygotes exit only after it, so a zygote that faults in a code page in that window dies with SIGBUS. Stop the zygotes in the process exit hook and wait for them before main exits. Fixes #24315 AI-assisted; verified with the tests and tools listed above.
1 parent 4e0b528 commit 2990b89

4 files changed

Lines changed: 107 additions & 0 deletions

File tree

‎apps/desktop/src/app/DesktopLifecycle.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { assert, describe, it } from "@effect/vitest";
22
import * as Deferred from "effect/Deferred";
33
import * as Effect from "effect/Effect";
44
import * as Layer from "effect/Layer";
5+
import * as Option from "effect/Option";
56
import * as Ref from "effect/Ref";
67
import * as TestClock from "effect/testing/TestClock";
78

@@ -117,6 +118,7 @@ describe("DesktopLifecycle", () => {
117118
const layerEnvironment = Layer.succeed(DesktopEnvironment.DesktopEnvironment, {
118119
platform,
119120
isDevelopment: false,
121+
appImagePath: Option.none(),
120122
} as DesktopEnvironment.DesktopEnvironment["Service"]);
121123

122124
const layer = DesktopLifecycle.layer.pipe(

‎apps/desktop/src/app/DesktopLifecycle.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Context from "effect/Context";
33
import * as Duration from "effect/Duration";
44
import * as Effect from "effect/Effect";
55
import * as Layer from "effect/Layer";
6+
import * as Option from "effect/Option";
67
import * as Ref from "effect/Ref";
78
import * as Schema from "effect/Schema";
89
import * as Scope from "effect/Scope";
@@ -14,6 +15,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
1415
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
1516
import { makeComponentLogger } from "./DesktopObservability.ts";
1617
import * as DesktopShutdown from "./DesktopShutdown.ts";
18+
import * as DesktopZygoteExit from "./DesktopZygoteExit.ts";
1719
import * as ElectronApp from "../electron/ElectronApp.ts";
1820
import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
1921
import * as ElectronTheme from "../electron/ElectronTheme.ts";
@@ -308,6 +310,13 @@ export const make = DesktopLifecycle.of({
308310
quitFromSignal("SIGTERM", runEffect, quit);
309311
});
310312
}
313+
314+
// Unscoped: the app scope closes before Electron exits, and every exit path must run this.
315+
if (environment.platform === "linux" && Option.isSome(environment.appImagePath)) {
316+
yield* Effect.sync(() => {
317+
process.once("exit", () => DesktopZygoteExit.stopZygotesBeforeExit(process.pid));
318+
});
319+
}
311320
}).pipe(Effect.withSpan("desktop.lifecycle.register")),
312321
});
313322

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Spawns real child processes for /proc to report.
2+
import * as NodeChildProcess from "node:child_process";
3+
4+
import { describe, expect, it } from "vite-plus/test";
5+
6+
import { stopZygotesBeforeExit } from "./DesktopZygoteExit.ts";
7+
8+
// oxlint-disable-next-line t3code/no-global-process-runtime -- The helper reads Linux /proc.
9+
const isLinux = process.platform === "linux";
10+
11+
const spawnTitled = async (title: string) => {
12+
const child = NodeChildProcess.spawn("sleep", ["30"], { argv0: title, stdio: "ignore" });
13+
const exited = new Promise<NodeJS.Signals | null>((resolve) =>
14+
child.on("exit", (_code, signal) => resolve(signal)),
15+
);
16+
await new Promise((resolve) => child.on("spawn", resolve));
17+
return { child, exited };
18+
};
19+
20+
describe("stopZygotesBeforeExit", () => {
21+
it.skipIf(!isLinux)("kills zygote children and leaves other helpers running", async () => {
22+
// Chromium rewrites a zygote's argv into one space-separated title.
23+
const zygote = await spawnTitled("/opt/T3 Code/t3code --type=zygote --no-sandbox");
24+
const utility = await spawnTitled("/opt/T3 Code/t3code --type=utility");
25+
try {
26+
stopZygotesBeforeExit(process.pid);
27+
28+
expect(await zygote.exited).toBe("SIGKILL");
29+
expect(utility.child.exitCode).toBeNull();
30+
expect(utility.child.signalCode).toBeNull();
31+
} finally {
32+
zygote.child.kill("SIGKILL");
33+
utility.child.kill("SIGKILL");
34+
}
35+
});
36+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Runs synchronously in the process "exit" event, after the Effect runtime has finished.
2+
import * as NodeFS from "node:fs";
3+
4+
const MAX_WAIT_MS = 1000;
5+
6+
const readProc = (path: string): string | undefined => {
7+
try {
8+
return NodeFS.readFileSync(path, "utf8");
9+
} catch {
10+
return undefined;
11+
}
12+
};
13+
14+
// Chromium launches helpers from its own threads, so each thread's children file is read.
15+
const childPids = (pid: number): ReadonlyArray<number> => {
16+
let tasks: ReadonlyArray<string>;
17+
try {
18+
tasks = NodeFS.readdirSync(`/proc/${pid}/task`);
19+
} catch {
20+
return [];
21+
}
22+
return tasks.flatMap((task) =>
23+
(readProc(`/proc/${pid}/task/${task}/children`) ?? "")
24+
.split(" ")
25+
.filter((value) => value.trim() !== "")
26+
.map(Number),
27+
);
28+
};
29+
30+
// A zygote rewrites its argv into one space-separated title.
31+
const isZygote = (pid: number): boolean =>
32+
(readProc(`/proc/${pid}/cmdline`) ?? "")
33+
.replaceAll("\0", " ")
34+
.split(" ")
35+
.includes("--type=zygote");
36+
37+
// A zombie has already released its memory mappings.
38+
const isRunning = (pid: number): boolean => {
39+
const stat = readProc(`/proc/${pid}/stat`);
40+
if (stat === undefined) return false;
41+
const state = stat.charAt(stat.lastIndexOf(")") + 2);
42+
return state !== "Z" && state !== "X";
43+
};
44+
45+
/**
46+
* Chromium's zygotes exit only after the main process does, but the AppImage runtime unmounts
47+
* the image as soon as main exits, so a zygote that then faults in a code page dies with SIGBUS.
48+
*/
49+
export const stopZygotesBeforeExit = (pid: number): void => {
50+
const zygotes = childPids(pid).filter(isZygote);
51+
for (const zygote of zygotes) {
52+
try {
53+
process.kill(zygote, "SIGKILL");
54+
} catch {}
55+
}
56+
const tick = new Int32Array(new SharedArrayBuffer(4));
57+
for (let waited = 0; waited < MAX_WAIT_MS && zygotes.some(isRunning); waited++) {
58+
Atomics.wait(tick, 0, 0, 1);
59+
}
60+
};

0 commit comments

Comments
 (0)