Files
hyperframes/packages/cli/src/utils/runningCli.ts
T
Sergey S 24965372db fix(cli): each start sweeps the running markers of CLIs that are gone (#5790)
A CLI killed by a signal never runs its exit listener, so its file in
~/.hyperframes/running stays. Only the background installer swept those
files, and it never runs for an install layout it skips ("Unknown install
layout", e.g. a CLI bundled inside another tool), so the folder only grew:
185 markers since 2026-10-05 on one machine, none of them a live CLI.

registerRunningCli now removes, besides its own file, every file whose pid
is gone, whose name is not a pid, or whose heartbeat is older than
RUNNING_STALE_MS (a reused pid) — the installer's own criterion. The sweep
never throws, so a failure cannot skip the heartbeat or the exit listener.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

The test reads the running folder's listing instead of checking each path, so no
existence check precedes a write to the same path (CodeQL, file system race).
2026-10-24 15:15:25 +00:00

73 lines
2.2 KiB
TypeScript

import { mkdirSync, readdirSync, statSync, unlinkSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
/** One empty file per live CLI process, named by pid; the background installer waits until none is alive. */
export const RUNNING_DIR = join(homedir(), ".hyperframes", "running");
const RUNNING_HEARTBEAT_MS = 30_000;
export const RUNNING_STALE_MS = 10 * 60_000;
function isAlive(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch (error) {
// EPERM: the pid exists but belongs to another user.
return error instanceof Error && "code" in error && error.code === "EPERM";
}
}
function pruneExitedClis(dir: string, own: string): void {
let names: string[] = [];
try {
names = readdirSync(dir);
} catch {
return;
}
for (const name of names) {
if (name === own) continue;
const file = join(dir, name);
try {
const pid = Number(name);
const beating = Date.now() - statSync(file).mtimeMs <= RUNNING_STALE_MS;
// A live pid whose file stopped beating is a reused pid, not a CLI.
if (Number.isInteger(pid) && pid > 0 && isAlive(pid) && beating) continue;
unlinkSync(file);
} catch {
/* another CLI removed it first */
}
}
}
/** Mark this process as running until it exits. Never throws: a failed write only lets an update land sooner. */
export function registerRunningCli(
dir: string = RUNNING_DIR,
heartbeatMs: number = RUNNING_HEARTBEAT_MS,
): void {
try {
mkdirSync(dir, { recursive: true, mode: 0o700 });
const file = join(dir, String(process.pid));
writeFileSync(file, "", { mode: 0o600 });
pruneExitedClis(dir, String(process.pid));
const heartbeat = setInterval(() => {
try {
mkdirSync(dir, { recursive: true, mode: 0o700 });
writeFileSync(file, String(Date.now()), { mode: 0o600 });
} catch {
/* best-effort */
}
}, heartbeatMs);
heartbeat.unref();
process.on("exit", () => {
clearInterval(heartbeat);
try {
unlinkSync(file);
} catch {
/* already gone */
}
});
} catch {
/* best-effort */
}
}