GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(desktop): keep AppImage mounted while t3 shuts down (#24421) · Pipyakas/t3code@4a075c9

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/desktop/src/app
    • DesktopCliShim.test.ts
    • DesktopCliShim.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: apps/desktop/src/app/DesktopCliShim.test.ts
+80-3Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ const run = (shim: string, args: ReadonlyArray<string>, env: NodeJS.ProcessEnv =
4141
const REPORTER =
4242
'#!/bin/sh\necho "node=$ELECTRON_RUN_AS_NODE cli=$T3CODE_CLI_PATH home=$T3CODE_HOME"\nprintf "%s\\n" "$@"\nexit 3\n';
4343

44-
const fakeAppImage = (canMount = true) => {
44+
const fakeAppImage = (canMount = true, app = REPORTER) => {
4545
const root = tempRoot();
4646
const image = NodePath.join(root, "image");
47-
writeExecutable(NodePath.join(image, "t3code"), REPORTER);
47+
writeExecutable(NodePath.join(image, "t3code"), app);
4848
const appImage = NodePath.join(root, "T3 Code.AppImage");
4949
writeExecutable(
5050
appImage,
@@ -53,8 +53,11 @@ const fakeAppImage = (canMount = true) => {
5353
`if [ "$1" = --appimage-extract ]; then cp -R '${image}' squashfs-root; exit 0; fi`,
5454
'[ "$1" = --appimage-mount ] || exit 9',
5555
'[ -n "$APPIMAGE_EXTRACT_AND_RUN" ] && exit 8',
56+
`echo $$ >'${root}/mounter.pid'`,
5657
canMount ? `echo '${image}'` : 'echo "Cannot mount AppImage"; exit 1',
57-
`trap 'touch "${root}/unmounted"; exit 0' TERM`,
58+
// Like the FUSE runtime, any of these unmounts. fd 3 reports to the test when it is open.
59+
`trap 'echo unmounted >&3 2>/dev/null; touch "${root}/unmounted"; exit 0' TERM HUP INT`,
60+
"trap 'echo mounted >&3 2>/dev/null' WINCH",
5861
"while :; do sleep 0.05; done",
5962
"",
6063
].join("\n"),
@@ -63,7 +66,81 @@ const fakeAppImage = (canMount = true) => {
6366
return { root, image, appImage, shim };
6467
};
6568

69+
// Stays up after a signal until the test lets it finish, like a server shutting down.
70+
const SLOW_SERVER = [
71+
"#!/bin/sh",
72+
"trap 'echo stopping >&3; read go; echo stopped >&3; exit 0' TERM HUP",
73+
"echo ready",
74+
"while :; do sleep 0.05; done",
75+
"",
76+
].join("\n");
77+
78+
// The launcher mounts only under setsid, which stock macOS and Windows lack; there it extracts instead.
79+
const hasSetsid = NodeChildProcess.spawnSync("setsid", ["true"]).status === 0;
80+
81+
const lines = (stream: NodeJS.ReadableStream) => {
82+
const seen: Array<string> = [];
83+
const waiters: Array<() => void> = [];
84+
let buffer = "";
85+
stream.setEncoding("utf8");
86+
stream.on("data", (chunk: string) => {
87+
buffer += chunk;
88+
const parts = buffer.split("\n");
89+
buffer = parts.pop()!;
90+
seen.push(...parts);
91+
for (const wake of waiters.splice(0)) wake();
92+
});
93+
const until = async (match: (line: string) => boolean) => {
94+
for (;;) {
95+
const line = seen.find(match);
96+
if (line !== undefined) return line;
97+
await new Promise<void>((resolve) => waiters.push(resolve));
98+
}
99+
};
100+
return { seen, until };
101+
};
102+
66103
describe("renderCliShim", () => {
104+
it.skipIf(!hasSetsid).each(["SIGTERM", "SIGHUP"] as const)(
105+
"keeps the AppImage mounted until the server exits after %s to the process group",
106+
async (signal) => {
107+
const { root, shim } = fakeAppImage(true, SLOW_SERVER);
108+
const child = NodeChildProcess.spawn(shim, [], {
109+
detached: true,
110+
env: { PATH: process.env.PATH },
111+
stdio: ["pipe", "pipe", "ignore", "pipe"],
112+
});
113+
let mounter: number | undefined;
114+
try {
115+
const stdout = lines(child.stdout!);
116+
const events = lines(child.stdio[3] as NodeJS.ReadableStream);
117+
const exited = new Promise((resolve) => child.on("close", resolve));
118+
await stdout.until((line) => line === "ready");
119+
mounter = Number(NodeFS.readFileSync(NodePath.join(root, "mounter.pid"), "utf8"));
120+
121+
// Ctrl-C in a terminal, a closed SSH session, or a service manager signals the whole group.
122+
process.kill(-child.pid!, signal);
123+
// The shell runs pending traps in ascending signal number, and WINCH outnumbers TERM and
124+
// HUP, so a mounter that also got the group signal unmounts before it answers this probe.
125+
process.kill(mounter, "SIGWINCH");
126+
const mount = await events.until((line) => line === "mounted" || line === "unmounted");
127+
expect(mount).toBe("mounted");
128+
129+
await events.until((line) => line === "stopping");
130+
child.stdin!.end("go\n");
131+
await exited;
132+
expect(events.seen.slice(-2)).toEqual(["stopped", "unmounted"]);
133+
} finally {
134+
for (const pid of [-child.pid!, mounter]) {
135+
if (pid === undefined) continue;
136+
try {
137+
process.kill(pid, "SIGKILL");
138+
} catch {}
139+
}
140+
}
141+
},
142+
);
143+
67144
it("extracts the AppImage where it cannot be mounted", () => {
68145
const { shim } = fakeAppImage(false);
69146
const result = run(shim, ["--version"], { APPIMAGE_EXTRACT_AND_RUN: "1" });
Collapse file

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

Copy file name to clipboardExpand all lines: apps/desktop/src/app/DesktopCliShim.ts
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ export const renderCliShim = (input: {
9090
...header,
9191
// Extract-and-run would make the runtime start the app instead of mounting it.
9292
"unset APPIMAGE_EXTRACT_AND_RUN",
93-
// The runtime stays mounted until killed; traps unmount it because background jobs ignore Ctrl-C.
93+
// Its own session keeps a group signal (Ctrl-C, SSH hangup) from unmounting it under the
94+
// running server; the traps unmount it after. Without setsid the extract fallback runs.
9495
"work=$(mktemp -d) || exit 1",
95-
'"$app" --appimage-mount >"$work/mount" 2>/dev/null &',
96+
'setsid "$app" --appimage-mount >"$work/mount" 2>/dev/null &',
9697
"mounter=$!",
9798
`trap 'kill "$mounter" 2>/dev/null; rm -rf "$work"' EXIT`,
98-
"trap 'exit 130' INT TERM",
99+
"trap 'exit 130' INT TERM HUP",
99100
"mount=",
100101
'while kill -0 "$mounter" 2>/dev/null; do',
101102
' mount=$(head -n 1 "$work/mount")',

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.