@devries-cameron20 / t3code-1 / commits / 9a4ec171fc
Fork 0

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

fix(cli): npm launcher no longer leaves a runtime running after a stop signal (#30487)

Julius Marminge committed Nov 13, 2026 at 17:03 UTC 9a4ec171fc27c16ddbf68b229a7321579ab7ffcf
2 files changed +107 -1
scripts/build-npm-launcher.test.ts
+76
@@ -412,4 +412,80 @@ syncBuiltinESMExports();
412 }
413 }),
414 );
415 +
416 + it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
417 + "kills a runtime that ignores the forwarded SIGTERM, and its providers, so the launcher exits",
418 + () =>
419 + Effect.gen(function* () {
420 + const fs = yield* FileSystem.FileSystem;
421 + const path = yield* Path.Path;
422 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-npm-launcher-test-" });
423 + const output = yield* buildNpmLauncher({
424 + version: VERSION,
425 + outputDir: path.join(root, "out"),
426 + });
427 + const hostPath = (yield* HostProcess.Environment).PATH;
428 + const installed = path.join(root, "installed");
429 + yield* fs.makeDirectory(installed);
430 + const unpack = yield* run("tar", ["-xzf", output.tarball, "-C", installed], {
431 + cwd: root,
432 + env: { PATH: hostPath },
433 + });
434 + assert.equal(unpack.exitCode, 0, unpack.stderr);
435 +
436 + const home = path.join(root, "home");
437 + const versionDir = path.join(home, "runtime/versions", VERSION);
438 + yield* fs.makeDirectory(versionDir, { recursive: true });
439 + yield* fs.writeFileString(path.join(versionDir, ".install-complete"), `${VERSION}\n`);
440 + // Stands in for a runtime whose exit is stuck behind blocked filesystem calls, with a
441 + // provider it never gets to stop. Both give up on their own well inside the project
442 + // timeout so an unescalated stop fails descriptively.
443 + const stuck = "runtime ignored SIGTERM and was still running 30s later";
444 + const orphaned = "provider outlived the runtime by 30s";
445 + const provider = `process.on("SIGTERM", () => {}); setTimeout(() => { console.log(${JSON.stringify(orphaned)}); process.exit(0); }, 30_000);`;
446 + yield* fs.writeFileString(
447 + path.join(versionDir, "t3"),
448 + `#!${process.execPath}
449 +const provider = require("node:child_process").spawn(process.execPath, ["-e", ${JSON.stringify(provider)}], { stdio: "inherit" });
450 +process.on("SIGTERM", () => setTimeout(() => { console.log(${JSON.stringify(stuck)}); process.exit(0); }, 30_000));
451 +setInterval(() => {}, 1 << 30);
452 +console.log(process.pid, provider.pid);
453 +`,
454 + );
455 + yield* fs.chmod(path.join(versionDir, "t3"), 0o755);
456 +
457 + const launcher = NodeChildProcess.spawn(
458 + process.execPath,
459 + [path.join(installed, "package/bin/t3.mjs"), "serve"],
460 + { cwd: root, env: { PATH: hostPath, T3CODE_HOME: home }, stdio: "pipe" },
461 + );
462 + let pids: Array<number> = [];
463 + yield* Effect.addFinalizer(() =>
464 + Effect.sync(() => {
465 + for (const pid of [...pids, launcher.pid]) {
466 + try {
467 + if (pid !== undefined) process.kill(pid, "SIGKILL");
468 + } catch {}
469 + }
470 + }),
471 + );
472 + let stdout = "";
473 + const started = Promise.withResolvers<void>();
474 + launcher.stdout.setEncoding("utf8");
475 + launcher.stdout.on("data", (chunk: string) => {
476 + stdout += chunk;
477 + if (stdout.includes("\n")) started.resolve();
478 + });
479 + // The runtime and provider inherit this pipe, so it closes only once all three are gone.
480 + const closed = NodeEvents.EventEmitter.once(launcher.stdout, "close");
481 + yield* Effect.promise(() => started.promise);
482 + pids = stdout.trim().split(" ").map(Number);
483 + assert.isTrue(pids.length === 2 && pids.every(Number.isInteger), stdout);
484 +
485 + launcher.kill("SIGTERM");
486 + yield* Effect.promise(() => closed);
487 + assert.notInclude(stdout, stuck, "the launcher waited on a runtime that ignored SIGTERM");
488 + assert.notInclude(stdout, orphaned, "the runtime's provider outlived the launcher");
489 + }),
490 + );
491 });
scripts/npm-launcher/t3.mjs
+31 -1
@@ -11,6 +11,8 @@ const { version } = JSON.parse(
11 );
12
13 const SUPPORTED = ["darwin-arm64", "linux-arm64", "linux-x64", "win32-arm64", "win32-x64"];
14 +// Well inside launchd's 20 s ExitTimeOut, which SIGKILLs only the launcher and orphans the runtime.
15 +const TERMINATE_GRACE_MS = 5_000;
16
17 function t3Home() {
18 const raw = (process.env.T3CODE_HOME || "").trim();
@@ -199,8 +201,13 @@ async function main() {
201 child.on("message", (message) => process.connected && process.send(message));
202 process.on("disconnect", () => child.connected && child.disconnect());
203 }
204 + let force;
205 for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
203 - process.on(signal, () => (ipc || signal !== "SIGINT") && child.kill(signal));
206 + process.on(signal, () => {
207 + if (!ipc && signal === "SIGINT") return;
208 + child.kill(signal);
209 + force ??= setTimeout(() => killTree(child), TERMINATE_GRACE_MS).unref();
210 + });
211 }
212 child.on("error", (error) => fail(executableError(key, error.message)));
213 child.on("exit", (code, signal) =>
@@ -208,6 +215,29 @@ async function main() {
215 );
216 }
217
218 +/** SIGKILL the runtime and its providers, which a runtime stuck on shutdown never stops. */
219 +function killTree(child) {
220 + const pids = [child.pid];
221 + if (process.platform !== "win32") {
222 + const table =
223 + spawnSync("ps", ["-A", "-o", "pid=,ppid="], { encoding: "utf8", timeout: 2_000 }).stdout ||
224 + "";
225 + const rows = table
226 + .trim()
227 + .split("\n")
228 + .map((line) => line.trim().split(/\s+/).map(Number));
229 + for (let i = 0; i < pids.length; i++) {
230 + for (const [pid, ppid] of rows) if (ppid === pids[i]) pids.push(pid);
231 + }
232 + }
233 + for (const pid of pids.slice(1)) {
234 + try {
235 + process.kill(pid, "SIGKILL");
236 + } catch {}
237 + }
238 + child.kill("SIGKILL");
239 +}
240 +
241 function executableError(key, detail) {
242 return new Error(
243 `could not run T3 Code ${version} for ${key}: ${detail}` +