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(server): a desktop backend whose launcher died exits even if shut… · pingdotgg/t3code@17dd10e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps
    • desktop/src/backend
      • DesktopBackendPool.ts
      • DesktopBackendRecovery.test.ts
      • DesktopBackendRecovery.ts
      • LinuxProcessIdentity.ts
    • server/src
      • cli
        • server.ts
      • launcherWatch.test.ts
      • launcherWatch.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/desktop/src/backend/DesktopBackendPool.ts‎

Copy file name to clipboardExpand all lines: apps/desktop/src/backend/DesktopBackendPool.ts
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ const { logInfo: logBackendPoolInfo, logWarning: logBackendPoolWarning } =
4343

4444
const makeRecovery = Effect.gen(function* () {
4545
const environment = yield* DesktopEnvironment.DesktopEnvironment;
46-
if (environment.platform !== "darwin" || !environment.isPackaged) return undefined;
46+
const supported =
47+
environment.platform === "linux" ||
48+
(environment.platform === "darwin" && environment.isPackaged);
49+
if (!supported) return undefined;
4750
const runFork = Effect.runForkWith(yield* Effect.context<never>());
4851
return DesktopBackendRecoveryProcess.make({
4952
appDataDirectory: environment.appDataDirectory,
Collapse file

‎apps/desktop/src/backend/DesktopBackendRecovery.test.ts‎

Copy file name to clipboard
+164-104Lines changed: 164 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @effect-diagnostics nodeBuiltinImport:off - Drive actual macOS subprocesses and their file permissions.
1+
// @effect-diagnostics nodeBuiltinImport:off - Drive actual subprocesses and their file permissions.
22
import * as NodeChildProcess from "node:child_process";
33
import * as NodeCrypto from "node:crypto";
44
import * as NodeFS from "node:fs";
@@ -12,8 +12,14 @@ import * as NodeProcess from "node:process";
1212
import { assert, describe, expect, it } from "@effect/vitest";
1313

1414
import { make } from "./DesktopBackendRecovery.ts";
15+
import { linuxProcessIdentity } from "./LinuxProcessIdentity.ts";
1516
import { loadMacProcessAuditToken } from "./MacProcessAuditToken.ts";
1617

18+
const loadNative = () =>
19+
NodeProcess.platform === "darwin"
20+
? loadMacProcessAuditToken()
21+
: Promise.resolve(linuxProcessIdentity);
22+
1723
const childCode = `
1824
import { createServer } from "node:net";
1925
process.once("message", () => {
@@ -55,12 +61,13 @@ async function spawnTrackedBackend(
5561
appDataDirectory: string,
5662
stateDir: string,
5763
cleanup: AsyncDisposableStack,
64+
options: { readonly execPath?: string; readonly env?: NodeJS.ProcessEnv } = {},
5865
) {
5966
const moduleUrl = new URL("./DesktopBackendRecovery.ts", import.meta.url).href;
6067
const launcher = NodeChildProcess.spawn(
61-
NodeProcess.execPath,
68+
options.execPath ?? NodeProcess.execPath,
6269
["--input-type=module", "-e", launcherCode, moduleUrl, appDataDirectory, stateDir, childCode],
63-
{ stdio: ["ignore", "ignore", "inherit", "ipc"] },
70+
{ stdio: ["ignore", "ignore", "inherit", "ipc"], env: options.env ?? NodeProcess.env },
6471
);
6572
const exited = NodeEvents.EventEmitter.once(launcher, "exit");
6673
const stopLauncher = async () => {
@@ -76,7 +83,7 @@ async function spawnTrackedBackend(
7683
throw new Error("Fixture launcher exited before its backend was ready");
7784
}),
7885
]);
79-
const native = await loadMacProcessAuditToken();
86+
const native = await loadNative();
8087
const token = native.read(backend.pid);
8188
assert.isDefined(token);
8289
cleanup.defer(() => native.signal(token, "SIGKILL"));
@@ -93,109 +100,162 @@ async function checkPortIsFree(port: number) {
93100
}
94101
}
95102

96-
describe.runIf(NodeProcess.platform === "darwin")("macOS desktop backend recovery", () => {
97-
it("cleans up a truncated record only after the writer releases it", async () => {
98-
await using cleanup = new AsyncDisposableStack();
99-
const stateDir = await NodeFSP.realpath(
100-
await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-record-")),
103+
describe.runIf(NodeProcess.platform === "darwin" || NodeProcess.platform === "linux")(
104+
"desktop backend recovery",
105+
() => {
106+
it.runIf(NodeProcess.platform === "darwin")(
107+
"cleans up a truncated record only after the writer releases it",
108+
async () => {
109+
await using cleanup = new AsyncDisposableStack();
110+
const stateDir = await NodeFSP.realpath(
111+
await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-record-")),
112+
);
113+
cleanup.defer(() => NodeFSP.rm(stateDir, { recursive: true, force: true }));
114+
const directory = NodePath.join(stateDir, "t3code-backend-recovery");
115+
await NodeFSP.mkdir(directory, { mode: 0o700 });
116+
const scope = NodeCrypto.createHash("sha256")
117+
.update(JSON.stringify([stateDir, await NodeFSP.realpath(NodeProcess.execPath)]))
118+
.digest("hex");
119+
const path = () => NodePath.join(directory, `${scope}.${NodeCrypto.randomUUID()}.json`);
120+
const partial = path();
121+
const future = path();
122+
const unsafe = path();
123+
await NodeFSP.writeFile(future, '{"version":2}', { mode: 0o600 });
124+
await NodeFSP.writeFile(unsafe, "{", { mode: 0o600 });
125+
await NodeFSP.chmod(unsafe, 0o644);
126+
const errors: unknown[] = [];
127+
const recovered: number[] = [];
128+
const recovery = make({
129+
appDataDirectory: stateDir,
130+
stateDir,
131+
launcherPid: NodeProcess.pid,
132+
onRecovery: (pid) => recovered.push(pid),
133+
onError: (_, error) => errors.push(error),
134+
});
135+
{
136+
// Darwin O_EXLOCK keeps the record private to the writer until close or exit.
137+
await using writer = await NodeFSP.open(
138+
partial,
139+
NodeFS.constants.O_WRONLY | NodeFS.constants.O_CREAT | NodeFS.constants.O_EXCL | 0x20,
140+
0o600,
141+
);
142+
await writer.writeFile('{"version":');
143+
await recovery.recover();
144+
assert.equal(await NodeFSP.readFile(partial, "utf8"), '{"version":');
145+
}
146+
await recovery.recover();
147+
await recovery.recover();
148+
await expect(NodeFSP.stat(partial)).rejects.toMatchObject({ code: "ENOENT" });
149+
assert.equal(await NodeFSP.readFile(future, "utf8"), '{"version":2}');
150+
assert.equal(await NodeFSP.readFile(unsafe, "utf8"), "{");
151+
assert.deepEqual(recovered, []);
152+
assert.deepEqual(errors, []);
153+
},
101154
);
102-
cleanup.defer(() => NodeFSP.rm(stateDir, { recursive: true, force: true }));
103-
const directory = NodePath.join(stateDir, "t3code-backend-recovery");
104-
await NodeFSP.mkdir(directory, { mode: 0o700 });
105-
const scope = NodeCrypto.createHash("sha256")
106-
.update(JSON.stringify([stateDir, await NodeFSP.realpath(NodeProcess.execPath)]))
107-
.digest("hex");
108-
const path = () => NodePath.join(directory, `${scope}.${NodeCrypto.randomUUID()}.json`);
109-
const partial = path();
110-
const future = path();
111-
const unsafe = path();
112-
await NodeFSP.writeFile(future, '{"version":2}', { mode: 0o600 });
113-
await NodeFSP.writeFile(unsafe, "{", { mode: 0o600 });
114-
await NodeFSP.chmod(unsafe, 0o644);
115-
const errors: unknown[] = [];
116-
const recovered: number[] = [];
117-
const recovery = make({
118-
appDataDirectory: stateDir,
119-
stateDir,
120-
launcherPid: NodeProcess.pid,
121-
onRecovery: (pid) => recovered.push(pid),
122-
onError: (_, error) => errors.push(error),
123-
});
124-
{
125-
// Darwin O_EXLOCK keeps the record private to the writer until close or exit.
126-
await using writer = await NodeFSP.open(
127-
partial,
128-
NodeFS.constants.O_WRONLY | NodeFS.constants.O_CREAT | NodeFS.constants.O_EXCL | 0x20,
129-
0o600,
155+
156+
it("releases a frozen orphan's port and leaves a backend with a live launcher running", async () => {
157+
await using cleanup = new AsyncDisposableStack();
158+
const appDataDirectory = await NodeFSP.mkdtemp(
159+
NodePath.join(NodeOS.tmpdir(), "t3-backend-recovery-"),
130160
);
131-
await writer.writeFile('{"version":');
161+
cleanup.defer(() => NodeFSP.rm(appDataDirectory, { recursive: true, force: true }));
162+
// The first launcher records ownership before the home exists.
163+
const stateDir = NodePath.join(appDataDirectory, "home", "userdata");
164+
const orphan = await spawnTrackedBackend(appDataDirectory, stateDir, cleanup);
165+
await NodeFSP.mkdir(stateDir, { recursive: true });
166+
const live = await spawnTrackedBackend(appDataDirectory, stateDir, cleanup);
167+
const native = await loadNative();
168+
const recovered: number[] = [];
169+
const errors: unknown[] = [];
170+
const recovery = make({
171+
appDataDirectory,
172+
stateDir,
173+
launcherPid: NodeProcess.pid,
174+
onRecovery: (pid) => recovered.push(pid),
175+
onError: (_, error) => errors.push(error),
176+
});
177+
178+
NodeProcess.kill(orphan.pid, "SIGSTOP");
179+
await orphan.stopLauncher();
180+
await expect(checkPortIsFree(orphan.port)).rejects.toMatchObject({ code: "EADDRINUSE" });
181+
182+
const wrongVersion = Buffer.from(live.token, "hex");
183+
wrongVersion[28] = wrongVersion[28]! ^ 1;
184+
native.signal(wrongVersion.toString("hex"), "SIGKILL");
185+
assert.equal(native.read(live.pid), live.token);
186+
187+
const otherHome = make({
188+
appDataDirectory,
189+
stateDir: NodePath.join(stateDir, "other"),
190+
launcherPid: NodeProcess.pid,
191+
onRecovery: (pid) => recovered.push(pid),
192+
onError: (_, error) => errors.push(error),
193+
});
194+
await otherHome.recover();
195+
assert.deepEqual(recovered, []);
196+
197+
const records = NodePath.join(appDataDirectory, "t3code-backend-recovery");
198+
for (const name of await NodeFSP.readdir(records))
199+
await NodeFSP.chmod(NodePath.join(records, name), 0o644);
132200
await recovery.recover();
133-
assert.equal(await NodeFSP.readFile(partial, "utf8"), '{"version":');
134-
}
135-
await recovery.recover();
136-
await recovery.recover();
137-
await expect(NodeFSP.stat(partial)).rejects.toMatchObject({ code: "ENOENT" });
138-
assert.equal(await NodeFSP.readFile(future, "utf8"), '{"version":2}');
139-
assert.equal(await NodeFSP.readFile(unsafe, "utf8"), "{");
140-
assert.deepEqual(recovered, []);
141-
assert.deepEqual(errors, []);
142-
});
201+
assert.deepEqual(recovered, []);
202+
assert.equal(native.read(orphan.pid), orphan.token);
203+
for (const name of await NodeFSP.readdir(records))
204+
await NodeFSP.chmod(NodePath.join(records, name), 0o600);
143205

144-
it("releases a frozen orphan's port and leaves a backend with a live launcher running", async () => {
145-
await using cleanup = new AsyncDisposableStack();
146-
const appDataDirectory = await NodeFSP.mkdtemp(
147-
NodePath.join(NodeOS.tmpdir(), "t3-backend-recovery-"),
148-
);
149-
cleanup.defer(() => NodeFSP.rm(appDataDirectory, { recursive: true, force: true }));
150-
// The first launcher records ownership before the home exists.
151-
const stateDir = NodePath.join(appDataDirectory, "home", "userdata");
152-
const orphan = await spawnTrackedBackend(appDataDirectory, stateDir, cleanup);
153-
await NodeFSP.mkdir(stateDir, { recursive: true });
154-
const live = await spawnTrackedBackend(appDataDirectory, stateDir, cleanup);
155-
const native = await loadMacProcessAuditToken();
156-
const recovered: number[] = [];
157-
const errors: unknown[] = [];
158-
const recovery = make({
159-
appDataDirectory,
160-
stateDir,
161-
launcherPid: NodeProcess.pid,
162-
onRecovery: (pid) => recovered.push(pid),
163-
onError: (_, error) => errors.push(error),
206+
await recovery.recover();
207+
assert.deepEqual(errors, []);
208+
assert.deepEqual(recovered, [orphan.pid]);
209+
assert.isUndefined(native.read(orphan.pid));
210+
await checkPortIsFree(orphan.port);
211+
assert.equal(native.read(live.pid), live.token);
164212
});
165213

166-
NodeProcess.kill(orphan.pid, "SIGSTOP");
167-
await orphan.stopLauncher();
168-
await expect(checkPortIsFree(orphan.port)).rejects.toMatchObject({ code: "EADDRINUSE" });
169-
170-
const wrongVersion = Buffer.from(live.token, "hex");
171-
wrongVersion[28] = wrongVersion[28]! ^ 1;
172-
native.signal(wrongVersion.toString("hex"), "SIGKILL");
173-
assert.equal(native.read(live.pid), live.token);
174-
175-
const otherHome = make({
176-
appDataDirectory,
177-
stateDir: NodePath.join(stateDir, "other"),
178-
launcherPid: NodeProcess.pid,
179-
onRecovery: (pid) => recovered.push(pid),
180-
onError: (_, error) => errors.push(error),
181-
});
182-
await otherHome.recover();
183-
assert.deepEqual(recovered, []);
184-
185-
const records = NodePath.join(appDataDirectory, "t3code-backend-recovery");
186-
for (const name of await NodeFSP.readdir(records))
187-
await NodeFSP.chmod(NodePath.join(records, name), 0o644);
188-
await recovery.recover();
189-
assert.deepEqual(recovered, []);
190-
assert.equal(native.read(orphan.pid), orphan.token);
191-
for (const name of await NodeFSP.readdir(records))
192-
await NodeFSP.chmod(NodePath.join(records, name), 0o600);
193-
194-
await recovery.recover();
195-
assert.deepEqual(errors, []);
196-
assert.deepEqual(recovered, [orphan.pid]);
197-
assert.isUndefined(native.read(orphan.pid));
198-
await checkPortIsFree(orphan.port);
199-
assert.equal(native.read(live.pid), live.token);
200-
});
201-
});
214+
it.runIf(NodeProcess.platform === "linux")(
215+
"recovers an AppImage orphan after a relaunch mounts the app at a new path",
216+
async () => {
217+
await using cleanup = new AsyncDisposableStack();
218+
const appDataDirectory = await NodeFSP.realpath(
219+
await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-appimage-recovery-")),
220+
);
221+
cleanup.defer(() => NodeFSP.rm(appDataDirectory, { recursive: true, force: true }));
222+
const stateDir = NodePath.join(appDataDirectory, "home");
223+
const appImage = NodePath.join(appDataDirectory, "T3-Code.AppImage");
224+
await NodeFSP.writeFile(appImage, "");
225+
// The previous launch ran from another mount, so its executable path differs from ours.
226+
const previousMount = NodePath.join(appDataDirectory, "mount-previous");
227+
await NodeFSP.mkdir(previousMount);
228+
const previousExecutable = NodePath.join(previousMount, "node");
229+
await NodeFSP.link(NodeProcess.execPath, previousExecutable).catch(() =>
230+
NodeFSP.copyFile(NodeProcess.execPath, previousExecutable),
231+
);
232+
const orphan = await spawnTrackedBackend(appDataDirectory, stateDir, cleanup, {
233+
execPath: previousExecutable,
234+
env: { ...NodeProcess.env, APPIMAGE: appImage },
235+
});
236+
const native = await loadNative();
237+
NodeProcess.kill(orphan.pid, "SIGSTOP");
238+
await orphan.stopLauncher();
239+
240+
const recovered: number[] = [];
241+
const errors: unknown[] = [];
242+
const previousAppImage = NodeProcess.env.APPIMAGE;
243+
NodeProcess.env.APPIMAGE = appImage;
244+
cleanup.defer(() => {
245+
if (previousAppImage === undefined) delete NodeProcess.env.APPIMAGE;
246+
else NodeProcess.env.APPIMAGE = previousAppImage;
247+
});
248+
await make({
249+
appDataDirectory,
250+
stateDir,
251+
launcherPid: NodeProcess.pid,
252+
onRecovery: (pid) => recovered.push(pid),
253+
onError: (_, error) => errors.push(error),
254+
}).recover();
255+
assert.deepEqual(errors, []);
256+
assert.deepEqual(recovered, [orphan.pid]);
257+
assert.isUndefined(native.read(orphan.pid));
258+
},
259+
);
260+
},
261+
);

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.