|
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. |
2 | 2 | import * as NodeChildProcess from "node:child_process"; |
3 | 3 | import * as NodeCrypto from "node:crypto"; |
4 | 4 | import * as NodeFS from "node:fs"; |
@@ -12,8 +12,14 @@ import * as NodeProcess from "node:process"; |
12 | 12 | import { assert, describe, expect, it } from "@effect/vitest"; |
13 | 13 |
|
14 | 14 | import { make } from "./DesktopBackendRecovery.ts"; |
| 15 | +import { linuxProcessIdentity } from "./LinuxProcessIdentity.ts"; |
15 | 16 | import { loadMacProcessAuditToken } from "./MacProcessAuditToken.ts"; |
16 | 17 |
|
| 18 | +const loadNative = () => |
| 19 | + NodeProcess.platform === "darwin" |
| 20 | + ? loadMacProcessAuditToken() |
| 21 | + : Promise.resolve(linuxProcessIdentity); |
| 22 | + |
17 | 23 | const childCode = ` |
18 | 24 | import { createServer } from "node:net"; |
19 | 25 | process.once("message", () => { |
@@ -55,12 +61,13 @@ async function spawnTrackedBackend( |
55 | 61 | appDataDirectory: string, |
56 | 62 | stateDir: string, |
57 | 63 | cleanup: AsyncDisposableStack, |
| 64 | + options: { readonly execPath?: string; readonly env?: NodeJS.ProcessEnv } = {}, |
58 | 65 | ) { |
59 | 66 | const moduleUrl = new URL("./DesktopBackendRecovery.ts", import.meta.url).href; |
60 | 67 | const launcher = NodeChildProcess.spawn( |
61 | | - NodeProcess.execPath, |
| 68 | + options.execPath ?? NodeProcess.execPath, |
62 | 69 | ["--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 }, |
64 | 71 | ); |
65 | 72 | const exited = NodeEvents.EventEmitter.once(launcher, "exit"); |
66 | 73 | const stopLauncher = async () => { |
@@ -76,7 +83,7 @@ async function spawnTrackedBackend( |
76 | 83 | throw new Error("Fixture launcher exited before its backend was ready"); |
77 | 84 | }), |
78 | 85 | ]); |
79 | | - const native = await loadMacProcessAuditToken(); |
| 86 | + const native = await loadNative(); |
80 | 87 | const token = native.read(backend.pid); |
81 | 88 | assert.isDefined(token); |
82 | 89 | cleanup.defer(() => native.signal(token, "SIGKILL")); |
@@ -93,109 +100,162 @@ async function checkPortIsFree(port: number) { |
93 | 100 | } |
94 | 101 | } |
95 | 102 |
|
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 | + }, |
101 | 154 | ); |
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-"), |
130 | 160 | ); |
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); |
132 | 200 | 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); |
143 | 205 |
|
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); |
164 | 212 | }); |
165 | 213 |
|
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 | +); |