Skip to content

Commit 559f8e7

Browse files
refactor(desktop): AppImage zygote stop reuses the exit-children /proc helpers
Main added DesktopExitChildren with the same /proc child listing, liveness check, and bounded wait. Move stopZygotesBeforeExit there instead of keeping a second copy, and fold its test into the existing file, now asserting the zygote is gone when the call returns. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 156944b commit 559f8e7

5 files changed

Lines changed: 40 additions & 100 deletions

File tree

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as NodeChildProcess from "node:child_process";
33
import * as NodeFS from "node:fs";
44
import { assert, describe, it } from "@effect/vitest";
55

6-
import { stopChildProcessesBeforeExit } from "./DesktopExitChildren.ts";
6+
import { stopChildProcessesBeforeExit, stopZygotesBeforeExit } from "./DesktopExitChildren.ts";
77

88
const isRunning = (pid: number) => {
99
try {
@@ -39,4 +39,24 @@ describe.runIf(NodeFS.existsSync("/proc/self/task"))("stopChildProcessesBeforeEx
3939
parent.kill("SIGKILL");
4040
}
4141
});
42+
43+
it("kills zygote children before returning and leaves other helpers running", async () => {
44+
const spawnTitled = async (title: string) => {
45+
const child = NodeChildProcess.spawn("sleep", ["60"], { argv0: title, stdio: "ignore" });
46+
await new Promise((resolve) => child.once("spawn", resolve));
47+
return child;
48+
};
49+
// Chromium rewrites a zygote's argv into one space-separated title.
50+
const zygote = await spawnTitled("/opt/T3 Code/t3code --type=zygote --no-sandbox");
51+
const utility = await spawnTitled("/opt/T3 Code/t3code --type=utility");
52+
try {
53+
stopZygotesBeforeExit(process.pid);
54+
55+
assert.isFalse(isRunning(zygote.pid!));
56+
assert.isTrue(isRunning(utility.pid!));
57+
} finally {
58+
zygote.kill("SIGKILL");
59+
utility.kill("SIGKILL");
60+
}
61+
});
4262
});

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ const childPids = (pid: number): ReadonlyArray<number> => {
2929
);
3030
};
3131

32+
// A zygote rewrites its argv into one space-separated title.
33+
const isZygote = (pid: number): boolean =>
34+
(readProc(`/proc/${pid}/cmdline`) ?? "")
35+
.replaceAll("\0", " ")
36+
.split(" ")
37+
.includes("--type=zygote");
38+
3239
// A zombie has already released its memory mappings.
3340
const isRunning = (pid: number): boolean => {
3441
const stat = readProc(`/proc/${pid}/stat`);
@@ -68,3 +75,13 @@ export const stopChildProcessesBeforeExit = (pid: number): ReadonlyArray<number>
6875
waitUntilStopped(survivors, KILL_WAIT_MS);
6976
return survivors;
7077
};
78+
79+
/**
80+
* Chromium's zygotes exit only after main does, so on an AppImage they outlive the mount even on
81+
* an orderly quit. Kills them while main still holds the image mounted. Linux only (/proc).
82+
*/
83+
export const stopZygotesBeforeExit = (pid: number): void => {
84+
const zygotes = childPids(pid).filter(isZygote);
85+
signal(zygotes, "SIGKILL");
86+
waitUntilStopped(zygotes, KILL_WAIT_MS);
87+
};

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import type * as Electron from "electron";
1313
import * as HostProcess from "@t3tools/shared/HostProcess";
1414

1515
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
16-
import { stopChildProcessesBeforeExit } from "./DesktopExitChildren.ts";
16+
import { stopChildProcessesBeforeExit, stopZygotesBeforeExit } from "./DesktopExitChildren.ts";
1717
import { makeComponentLogger } from "./DesktopObservability.ts";
1818
import * as DesktopShutdown from "./DesktopShutdown.ts";
19-
import * as DesktopZygoteExit from "./DesktopZygoteExit.ts";
2019
import * as ElectronApp from "../electron/ElectronApp.ts";
2120
import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
2221
import * as ElectronTheme from "../electron/ElectronTheme.ts";
@@ -346,7 +345,7 @@ export const make = DesktopLifecycle.of({
346345
// Unscoped: the app scope closes before Electron exits, and every exit path must run this.
347346
if (environment.platform === "linux" && Option.isSome(environment.appImagePath)) {
348347
yield* Effect.sync(() => {
349-
process.once("exit", () => DesktopZygoteExit.stopZygotesBeforeExit(process.pid));
348+
process.once("exit", () => stopZygotesBeforeExit(process.pid));
350349
});
351350
}
352351
}).pipe(Effect.withSpan("desktop.lifecycle.register")),

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

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

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)