Skip to content

Commit 631db89

Browse files
TonybynMp4claude
andcommitted
fix(desktop): wait for exit through a pipe in the Linux relauncher
Polling the PID could wait on an unrelated process that reused it. The relauncher now waits for EOF on a stdin pipe that only this process writes to, which arrives when it exits however it exits. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8edca6c commit 631db89

2 files changed

Lines changed: 13 additions & 13 deletions

File tree

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as Effect from "effect/Effect";
55
import * as FileSystem from "effect/FileSystem";
66
import * as Layer from "effect/Layer";
77
import * as Path from "effect/Path";
8+
import * as Stream from "effect/Stream";
89
import * as ChildProcess from "effect/process/ChildProcess";
910
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1011
import { beforeEach, vi } from "vite-plus/test";
@@ -279,21 +280,17 @@ describe("ElectronApp", () => {
279280
const [relauncher] = spawnedCommands;
280281
assert.isDefined(relauncher);
281282

282-
// Run the relauncher against a stand-in for this process.
283-
const running = yield* spawner.spawn(ChildProcess.make("sleep", ["30"]));
284-
const [shell, script, , ...relaunchArgs] = relauncher!.args;
283+
// Ending the relauncher's stdin stands in for this process exiting.
285284
const waiting = yield* spawner.spawn(
286-
ChildProcess.make(relauncher!.command, [
287-
shell!,
288-
script!,
289-
String(running.pid),
290-
...relaunchArgs,
291-
]),
285+
ChildProcess.make(relauncher!.command, relauncher!.args, {
286+
...relauncher!.options,
287+
detached: false,
288+
}),
292289
);
293290
assert.isTrue(yield* waiting.isRunning);
294291
assert.isFalse(yield* fileSystem.exists(output));
295292

296-
yield* running.kill();
293+
yield* Stream.run(Stream.empty, waiting.stdin);
297294
assert.equal(yield* waiting.exitCode, 0);
298295
assert.equal(yield* fileSystem.readFileString(output), "two words\n$HOME\n(Nightly)\n");
299296
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ export const make = Effect.gen(function* () {
223223
// never gain root through pkexec and every .deb update it installs fails.
224224
// A plain spawn doesn't set it. Like app.relaunch(), the new instance
225225
// starts once this process exits, so it can take the single-instance lock.
226+
// The shell waits for EOF on a stdin pipe that only this process can write
227+
// to, which arrives when this process exits however it exits. Polling the
228+
// PID instead could wait on an unrelated process that reused it.
226229
relaunch: (options) =>
227230
Effect.scoped(
228231
Effect.gen(function* () {
@@ -231,12 +234,12 @@ export const make = Effect.gen(function* () {
231234
"/bin/sh",
232235
[
233236
"-c",
234-
'while kill -0 "$0" 2>/dev/null; do sleep 0.1; done; exec "$@"',
235-
String(process.pid),
237+
'while read -r _; do :; done; exec "$@" </dev/null',
238+
"t3code-relaunch",
236239
options.execPath ?? process.execPath,
237240
...(options.args ?? process.argv.slice(1)),
238241
],
239-
{ detached: true, stdin: "ignore", stdout: "ignore", stderr: "ignore" },
242+
{ detached: true, stdin: "pipe", stdout: "ignore", stderr: "ignore" },
240243
),
241244
);
242245
// Keeps the waiting shell alive when this scope closes.

0 commit comments

Comments
 (0)