Skip to content

Commit 61b57e7

Browse files
authored
fix(dev): Wait for old Electron to exit on mprocs restart (#3291)
1 parent 9c63752 commit 61b57e7

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

mprocs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
procs:
22
1-code:
3-
shell: 'node scripts/pnpm-run.mjs --filter code run start'
3+
shell: 'node scripts/wait-for-electron-exit.mjs && node scripts/pnpm-run.mjs --filter code run start'
44
env:
55
DEBUG: "false"
66
depends_on:

scripts/wait-for-electron-exit.mjs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { execFileSync } from "node:child_process";
2+
import path from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
5+
const WAIT_TIMEOUT_MS = 15_000;
6+
const POLL_INTERVAL_MS = 200;
7+
8+
const repoRoot = path.resolve(
9+
path.dirname(fileURLToPath(import.meta.url)),
10+
"..",
11+
);
12+
const electronDist = path.join(repoRoot, "node_modules", "electron", "dist");
13+
const pattern = electronDist.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
14+
15+
function runningElectronPids() {
16+
try {
17+
const out = execFileSync("pgrep", ["-f", pattern], { encoding: "utf8" });
18+
return out.split("\n").filter(Boolean);
19+
} catch (error) {
20+
if (error.status === 1) return [];
21+
throw error;
22+
}
23+
}
24+
25+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
26+
27+
if (process.platform !== "win32") {
28+
try {
29+
const initialPids = runningElectronPids();
30+
if (initialPids.length > 0) {
31+
console.log(
32+
`Waiting for previous Electron instance to exit (pids: ${initialPids.join(", ")})`,
33+
);
34+
const startedAt = Date.now();
35+
while (Date.now() - startedAt < WAIT_TIMEOUT_MS) {
36+
await sleep(POLL_INTERVAL_MS);
37+
if (runningElectronPids().length === 0) break;
38+
}
39+
const remaining = runningElectronPids();
40+
if (remaining.length > 0) {
41+
console.warn(
42+
`Previous Electron instance still running after ${WAIT_TIMEOUT_MS}ms (pids: ${remaining.join(", ")}), starting anyway`,
43+
);
44+
} else {
45+
console.log(
46+
`Previous Electron instance exited after ${((Date.now() - startedAt) / 1000).toFixed(1)}s`,
47+
);
48+
}
49+
}
50+
} catch (error) {
51+
console.warn(
52+
`Skipping wait for previous Electron instance, pgrep failed: ${error.code ?? error.status ?? error.message}`,
53+
);
54+
}
55+
}

0 commit comments

Comments
 (0)