The best way to control Claude Code, Codex and more from any computer or phone
fix force closing
Theo Browne committed
Feb 19, 2026 at 02:03 UTC
8cb2b3fcfe451932681e73e38adb25321fdcb4cb
2 files changed
+63
-3
apps/desktop/scripts/dev-electron.mjs
+45
-1
@@ -1,3 +1,5 @@
1
+import { spawnSync } from "node:child_process";
2
+
3
import electronmon from "electronmon";
4
import waitOn from "wait-on";
5
@@ -18,7 +20,7 @@ await waitOn({
20
const childEnv = { ...process.env };
21
delete childEnv.ELECTRON_RUN_AS_NODE;
22
21
-await electronmon({
23
+const monitor = await electronmon({
24
cwd: desktopDir,
25
args: ["dist-electron/main.js"],
26
env: {
@@ -27,3 +29,45 @@ await electronmon({
29
},
30
electronPath: resolveElectronPath(),
31
});
32
+
33
+let shuttingDown = false;
34
+
35
+function killChildTree(signal) {
36
+ if (process.platform === "win32") {
37
+ return;
38
+ }
39
+
40
+ // electronmon may leave Electron orphaned when parent shutdown races; kill direct children as fallback.
41
+ spawnSync("pkill", [`-${signal}`, "-P", String(process.pid)], { stdio: "ignore" });
42
+}
43
+
44
+async function shutdown(exitCode) {
45
+ if (shuttingDown) return;
46
+ shuttingDown = true;
47
+
48
+ try {
49
+ await Promise.race([
50
+ monitor.destroy(),
51
+ new Promise((resolve) => setTimeout(resolve, 1_500)),
52
+ ]);
53
+ } catch {
54
+ // Best effort only; fallback process kill below handles stubborn children.
55
+ }
56
+
57
+ killChildTree("TERM");
58
+ setTimeout(() => {
59
+ killChildTree("KILL");
60
+ }, 1_200).unref();
61
+
62
+ process.exit(exitCode);
63
+}
64
+
65
+process.once("SIGINT", () => {
66
+ void shutdown(130);
67
+});
68
+process.once("SIGTERM", () => {
69
+ void shutdown(143);
70
+});
71
+process.once("SIGHUP", () => {
72
+ void shutdown(129);
73
+});
apps/desktop/src/main.ts
+18
-2
@@ -164,10 +164,10 @@ function stopBackend(): void {
164
backendProcess = null;
165
if (!child) return;
166
167
- if (!child.killed) {
167
+ if (child.exitCode === null && child.signalCode === null) {
168
child.kill("SIGTERM");
169
setTimeout(() => {
170
- if (!child.killed) {
170
+ if (child.exitCode === null && child.signalCode === null) {
171
child.kill("SIGKILL");
172
}
173
}, 2_000).unref();
@@ -336,3 +336,19 @@ app.on("window-all-closed", () => {
336
app.quit();
337
}
338
});
339
+
340
+if (process.platform !== "win32") {
341
+ process.on("SIGINT", () => {
342
+ if (isQuitting) return;
343
+ isQuitting = true;
344
+ stopBackend();
345
+ app.quit();
346
+ });
347
+
348
+ process.on("SIGTERM", () => {
349
+ if (isQuitting) return;
350
+ isQuitting = true;
351
+ stopBackend();
352
+ app.quit();
353
+ });
354
+}