@devries-cameron20 / t3code-1 / commits / 85f0419d5e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(desktop): dev supervisor stops when its parent process dies (#23571)

Julius Marminge committed Nov 11, 2026 at 15:31 UTC 85f0419d5e3c9ab8fb2a61c1a888ce223a8aa9ba
3 files changed +129 -8
apps/desktop/scripts/dev-electron.mjs
+13
@@ -52,6 +52,8 @@ const childTreeGracePeriodMs = 1_200;
52 const remoteDebuggingPort = process.env.T3CODE_DESKTOP_REMOTE_DEBUGGING_PORT?.trim();
53 // oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone dev script has no Effect runtime.
54 const hostPlatform = NodeOS.platform();
55 +const parentPid = process.ppid;
56 +const parentWatchIntervalMs = 1_000;
57
58 NodeChildProcess.execFileSync(
59 process.execPath,
@@ -271,3 +273,14 @@ process.once("SIGTERM", () => {
273 process.once("SIGHUP", () => {
274 void shutdown(129);
275 });
276 +// A parent killed without forwarding a signal leaves us reparented (to a subreaper on Linux, not
277 +// necessarily PID 1), and nothing else would ever stop the restart loop. This only notices the
278 +// death of our direct parent, so vite.config.ts launches us without a shell in between. Windows
279 +// never reparents, and reading process.ppid there snapshots the process table.
280 +if (hostPlatform !== "win32") {
281 + setInterval(() => {
282 + if (process.ppid !== parentPid) {
283 + void shutdown(129);
284 + }
285 + }, parentWatchIntervalMs).unref();
286 +}
apps/desktop/scripts/electron-launcher.test.mjs
+106 -7
@@ -118,19 +118,21 @@ describe("electron development launcher", () => {
118 });
119 });
120
121 -async function startupCleanupPattern(desktopDir) {
121 +// Supply inert imports so the real startup runs without launching Electron or signalling processes.
122 +async function runDevElectronStartup({
123 + desktopDir = "/repo/apps/desktop",
124 + spawnSync,
125 + spawn,
126 + ...context
127 +}) {
128 const source = NodeFS.readFileSync(new URL("./dev-electron.mjs", import.meta.url), "utf8");
123 - let pattern;
124 - // Supply inert imports so the real startup runs without launching Electron or signalling processes.
129 await NodeVM.runInNewContext(
130 `(async () => { ${source.replace(/^import\s[\s\S]*?;\n/gm, "")} })()`,
131 {
132 NodeChildProcess: {
133 execFileSync: () => undefined,
130 - spawnSync: (command, args) => {
131 - if (command === "pkill" && args.includes("-f")) pattern = args.at(-1);
132 - },
133 - spawn: () => new NodeEvents.EventEmitter(),
134 + spawnSync: spawnSync ?? (() => undefined),
135 + spawn: spawn ?? (() => new NodeEvents.EventEmitter()),
136 },
137 NodeFS: { watch: () => ({ close: () => undefined }) },
138 NodeOS: { platform: () => "linux" },
@@ -140,17 +142,114 @@ async function startupCleanupPattern(desktopDir) {
142 resolveElectronLaunchCommand: (args) => ({ electronPath: "electron", args }),
143 waitForResources: async () => undefined,
144 URL,
145 + ...makeFakeClock(),
146 process: {
147 env: { VITE_DEV_SERVER_URL: "http://localhost:1234" },
148 execPath: "node",
149 + ppid: 1,
150 once: () => undefined,
151 },
152 + ...context,
153 },
154 );
155 +}
156 +
157 +async function startupCleanupPattern(desktopDir) {
158 + let pattern;
159 + await runDevElectronStartup({
160 + desktopDir,
161 + spawnSync: (command, args) => {
162 + if (command === "pkill" && args.includes("-f")) pattern = args.at(-1);
163 + },
164 + });
165 assert.isString(pattern);
166 return pattern;
167 }
168
169 +function makeFakeClock() {
170 + let now = 0;
171 + let timers = [];
172 + const schedule = (callback, delay, repeat) => {
173 + const timer = { callback, due: now + (delay ?? 0), repeat: repeat ? (delay ?? 0) : null };
174 + timer.handle = { unref: () => timer.handle, ref: () => timer.handle };
175 + timers.push(timer);
176 + return timer.handle;
177 + };
178 + const clear = (handle) => {
179 + timers = timers.filter((timer) => timer.handle !== handle);
180 + };
181 + const flushMicrotasks = async () => {
182 + for (let index = 0; index < 50; index += 1) await Promise.resolve();
183 + };
184 + return {
185 + setTimeout: (callback, delay) => schedule(callback, delay, false),
186 + setInterval: (callback, delay) => schedule(callback, delay, true),
187 + clearTimeout: clear,
188 + clearInterval: clear,
189 + async advance(ms) {
190 + const end = now + ms;
191 + for (;;) {
192 + await flushMicrotasks();
193 + const next = timers
194 + .filter((timer) => timer.due <= end)
195 + .toSorted((a, b) => a.due - b.due)[0];
196 + if (!next) break;
197 + now = next.due;
198 + if (next.repeat === null) clear(next.handle);
199 + else next.due = now + Math.max(next.repeat, 1);
200 + next.callback();
201 + }
202 + now = end;
203 + await flushMicrotasks();
204 + },
205 + };
206 +}
207 +
208 +it("dev supervisor stops Electron and exits once its parent process goes away", async () => {
209 + const clock = makeFakeClock();
210 + let ppid = 4242;
211 + const exitCodes = [];
212 + const appSignals = [];
213 + await runDevElectronStartup({
214 + ...clock,
215 + spawn: () => {
216 + const app = new NodeEvents.EventEmitter();
217 + app.pid = 5151;
218 + app.kill = (signal) => {
219 + appSignals.push(signal);
220 + app.emit("exit", null, signal);
221 + };
222 + return app;
223 + },
224 + process: {
225 + env: { VITE_DEV_SERVER_URL: "http://localhost:1234" },
226 + execPath: "node",
227 + pid: 4343,
228 + get ppid() {
229 + return ppid;
230 + },
231 + once: () => undefined,
232 + exit: (code) => {
233 + exitCodes.push(code);
234 + },
235 + },
236 + });
237 +
238 + await clock.advance(5_000);
239 + assert.deepEqual(appSignals, [], "supervisor stopped Electron while its parent was alive");
240 +
241 + // The launching process died without signalling us; the OS reparented the supervisor.
242 + ppid = 1;
243 + await clock.advance(5_000);
244 +
245 + assert.include(
246 + appSignals,
247 + "SIGTERM",
248 + "supervisor kept Electron running after its parent process went away",
249 + );
250 + assert.lengthOf(exitCodes, 1, "supervisor did not exit after its parent process went away");
251 +});
252 +
253 it
254 // oxlint-disable-next-line t3code/no-global-process-runtime -- Native pgrep is unavailable on Windows.
255 .skipIf(NodeOS.platform() === "win32")
apps/desktop/vite.config.ts
+10 -1
@@ -1,4 +1,6 @@
1 import "vite-plus/test/config";
2 +import * as NodeOS from "node:os";
3 +
4 import { defaultInclude, defineConfig } from "vite-plus";
5
6 import { isDesktopRuntimeExternalDependency } from "../../scripts/lib/desktop-external-packages.ts";
@@ -11,6 +13,13 @@ const domTests = ["src/preview/PickPreload.test.ts"];
13 const isMainProcessExternal = (id: string) =>
14 id === "electron" || id.startsWith("electron/") || isDesktopRuntimeExternalDependency(id);
15 const shouldLaunchElectronAfterPack = process.env.T3CODE_DESKTOP_DEV === "1";
16 +// tsdown runs onSuccess through a shell. dash forks instead of exec'ing the last command, which
17 +// would leave an `sh` between `vp pack` and the supervisor and hide `vp pack` dying from it.
18 +const devElectronCommand =
19 + // oxlint-disable-next-line t3code/no-global-process-runtime -- Build config has no Effect runtime.
20 + NodeOS.platform() === "win32"
21 + ? "node scripts/dev-electron.mjs"
22 + : "exec node scripts/dev-electron.mjs";
23 const publicConfigDefine = {
24 __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: JSON.stringify(
25 repoEnv.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() ?? "",
@@ -71,7 +80,7 @@ export default defineConfig({
80 neverBundle: isMainProcessExternal,
81 onlyBundle: false,
82 },
74 - ...(shouldLaunchElectronAfterPack ? { onSuccess: "node scripts/dev-electron.mjs" } : {}),
83 + ...(shouldLaunchElectronAfterPack ? { onSuccess: devElectronCommand } : {}),
84 },
85 {
86 format: "cjs",