Skip to content

Commit 84efa01

Browse files
fix(desktop): AppImage quit no longer leaves zygotes running on an unmounted image (pingdotgg#36057)
1 parent 08544ba commit 84efa01

4 files changed

Lines changed: 49 additions & 2 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.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 & 1 deletion
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";
@@ -12,7 +13,7 @@ import type * as Electron from "electron";
1213
import * as HostProcess from "@t3tools/shared/HostProcess";
1314

1415
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
15-
import { stopChildProcessesBeforeExit } from "./DesktopExitChildren.ts";
16+
import { stopChildProcessesBeforeExit, stopZygotesBeforeExit } from "./DesktopExitChildren.ts";
1617
import { makeComponentLogger } from "./DesktopObservability.ts";
1718
import * as DesktopShutdown from "./DesktopShutdown.ts";
1819
import * as ElectronApp from "../electron/ElectronApp.ts";
@@ -340,6 +341,13 @@ export const make = DesktopLifecycle.of({
340341
quitFromSignal("SIGTERM", runEffect, quit);
341342
});
342343
}
344+
345+
// Unscoped: the app scope closes before Electron exits, and every exit path must run this.
346+
if (environment.platform === "linux" && Option.isSome(environment.appImagePath)) {
347+
yield* Effect.sync(() => {
348+
process.once("exit", () => stopZygotesBeforeExit(process.pid));
349+
});
350+
}
343351
}).pipe(Effect.withSpan("desktop.lifecycle.register")),
344352
});
345353

0 commit comments

Comments
 (0)