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): AppImage updates no longer leave old runtimes and mount… · Pipyakas/t3code@98fd5f2

File tree

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

‎apps/desktop/src/electron/ElectronUpdater.test.ts‎

Copy file name to clipboard
+118Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// @effect-diagnostics nodeBuiltinImport:off -- Intercepts the spawn electron-updater requires natively.
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import { assert, describe, it } from "@effect/vitest";
4+
import * as Deferred from "effect/Deferred";
5+
import * as Effect from "effect/Effect";
6+
import * as FileSystem from "effect/FileSystem";
7+
import * as Path from "effect/Path";
8+
import * as HostProcess from "@t3tools/shared/HostProcess";
9+
import { autoUpdater } from "electron-updater";
10+
import type * as NodeChildProcess from "node:child_process";
11+
import * as NodeModule from "node:module";
12+
import { vi } from "vite-plus/test";
13+
14+
// electron-updater requires Electron natively, outside vi.mock's reach, and builds its
15+
// autoUpdater singleton as soon as it is imported.
16+
vi.hoisted(() => {
17+
const NodeModule = process.getBuiltinModule("node:module");
18+
const NodeEvents = process.getBuiltinModule("node:events");
19+
const require = NodeModule.createRequire(import.meta.url);
20+
const electronPath = NodeModule.createRequire(require.resolve("electron-updater")).resolve(
21+
"electron",
22+
);
23+
const electronModule = new NodeModule.Module(electronPath);
24+
electronModule.filename = electronPath;
25+
electronModule.loaded = true;
26+
electronModule.exports = {
27+
app: {
28+
getVersion: () => "1.0.0",
29+
getName: () => "T3 Code",
30+
isPackaged: true,
31+
quit: () => undefined,
32+
once: () => undefined,
33+
},
34+
autoUpdater: new NodeEvents.EventEmitter(),
35+
};
36+
require.cache[electronPath] = electronModule;
37+
});
38+
39+
import * as ElectronUpdater from "./ElectronUpdater.ts";
40+
41+
const childProcess = NodeModule.createRequire(import.meta.url)("node:child_process") as {
42+
spawn: (
43+
command: string,
44+
args: ReadonlyArray<string>,
45+
options: NodeChildProcess.SpawnOptions,
46+
) => NodeChildProcess.ChildProcess;
47+
};
48+
49+
describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")(
50+
"ElectronUpdater AppImage install",
51+
() => {
52+
it.live("relaunches the installed AppImage without the updating process's descriptors", () =>
53+
Effect.gen(function* () {
54+
const fileSystem = yield* FileSystem.FileSystem;
55+
const path = yield* Path.Path;
56+
const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-appimage-install-" });
57+
const appImagePath = path.join(dir, "T3-Code.AppImage");
58+
const stagedPath = path.join(dir, "T3-Code-1.0.1.AppImage");
59+
const fdReportPath = path.join(dir, "relaunched-fds.txt");
60+
yield* fileSystem.writeFileString(appImagePath, "old AppImage");
61+
yield* fileSystem.writeFileString(
62+
stagedPath,
63+
`#!/bin/sh\nls /proc/self/fd > '${fdReportPath}'\n`,
64+
);
65+
yield* fileSystem.chmod(stagedPath, 0o755);
66+
67+
yield* Effect.acquireRelease(
68+
Effect.sync(() => vi.stubEnv("APPIMAGE", appImagePath)),
69+
() => Effect.sync(() => vi.unstubAllEnvs()),
70+
);
71+
72+
autoUpdater.logger = null;
73+
Object.assign(autoUpdater, {
74+
downloadedUpdateHelper: {
75+
file: stagedPath,
76+
downloadedFileInfo: { isAdminRightsRequired: false },
77+
},
78+
});
79+
80+
// Electron's main process holds the AppImage runtime's keepalive pipe and some
81+
// Chromium files without O_CLOEXEC. Node cannot hold such a descriptor, so the
82+
// relaunch runs with fd 7 opened that way to stand in for them.
83+
const relaunched = yield* Deferred.make<void>();
84+
const realSpawn = childProcess.spawn;
85+
yield* Effect.acquireRelease(
86+
Effect.sync(() => {
87+
childProcess.spawn = (command, args, options) => {
88+
const child = realSpawn(
89+
"bash",
90+
["-c", 'exec 7</dev/null; exec "$@"', "leaky-parent", command, ...args],
91+
options,
92+
);
93+
child.once("exit", () => Deferred.doneUnsafe(relaunched, Effect.void));
94+
return child;
95+
};
96+
NodeModule.syncBuiltinESMExports();
97+
}),
98+
() =>
99+
Effect.sync(() => {
100+
childProcess.spawn = realSpawn;
101+
NodeModule.syncBuiltinESMExports();
102+
}),
103+
);
104+
105+
const electronUpdater = yield* ElectronUpdater.ElectronUpdater;
106+
yield* electronUpdater.quitAndInstall({ isSilent: true, isForceRunAfter: true });
107+
yield* Deferred.await(relaunched);
108+
109+
const descriptors = (yield* fileSystem.readFileString(fdReportPath)).trim().split("\n");
110+
assert.notInclude(
111+
descriptors,
112+
"7",
113+
`relaunched AppImage inherited the updating process's fd 7 (open fds: ${descriptors.join(" ")})`,
114+
);
115+
}).pipe(Effect.provide([ElectronUpdater.layer, NodeServices.layer])),
116+
);
117+
},
118+
);
Collapse file

‎apps/desktop/src/electron/ElectronUpdater.ts‎

Copy file name to clipboardExpand all lines: apps/desktop/src/electron/ElectronUpdater.ts
+22-1Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,29 @@ import * as Layer from "effect/Layer";
44
import * as Schema from "effect/Schema";
55
import * as Scope from "effect/Scope";
66

7-
import { autoUpdater } from "electron-updater";
7+
import { AppImageUpdater, autoUpdater } from "electron-updater";
88

99
type AutoUpdater = typeof autoUpdater;
10+
type SpawnLog = (cmd: string, args?: Array<string>, ...rest: Array<unknown>) => Promise<boolean>;
11+
12+
// Electron holds the AppImage runtime's keepalive pipe and some files without O_CLOEXEC; a
13+
// relaunched AppImage inherits them, so old runtimes and FUSE mounts never exit. bash (AppRun
14+
// already needs it; dash cannot close multi-digit fds) closes them before exec'ing the AppImage.
15+
// -p skips BASH_ENV and exported functions in the helper only; the AppImage still receives both.
16+
const CLOSE_INHERITED_FDS_THEN_EXEC =
17+
'for fd in /proc/self/fd/*; do fd=${fd##*/}; case $fd in *[!0-9]*|[012]) ;; *) eval "exec $fd>&-" ;; esac; done; exec "$@"';
18+
19+
const relaunchWithoutInheritedDescriptors = (updater: AppImageUpdater) => {
20+
const spawnLog = (AppImageUpdater.prototype as unknown as { spawnLog: SpawnLog }).spawnLog;
21+
(updater as unknown as { spawnLog: SpawnLog }).spawnLog = function (cmd, args = [], ...rest) {
22+
return spawnLog.call(
23+
this,
24+
"bash",
25+
["-p", "-c", CLOSE_INHERITED_FDS_THEN_EXEC, cmd, cmd, ...args],
26+
...rest,
27+
);
28+
};
29+
};
1030

1131
export type ElectronUpdaterFeedUrl = Parameters<AutoUpdater["setFeedURL"]>[0];
1232

@@ -153,6 +173,7 @@ export const make = ElectronUpdater.of({
153173
}) {
154174
return Effect.suspend(() => {
155175
const channel = autoUpdater.channel;
176+
if (autoUpdater instanceof AppImageUpdater) relaunchWithoutInheritedDescriptors(autoUpdater);
156177
return Effect.try({
157178
try: () => autoUpdater.quitAndInstall(isSilent, isForceRunAfter),
158179
catch: (cause) =>

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.