Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/link/route-rounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export const ROUTE_PROBE_MS = 10_000;
* A candidate's budget, twice: from its spawn to the grant of its remote listener, and then from that
* grant to its proven readiness (discovery, health and every trigger). A slow route that spends most
* of the first budget on the ssh handshake still has the whole second one for its readiness execs.
* Not measured on a real slow route; the bound for one candidate is 2 x READY_PROOF_MS.
* Not measured on a real slow route. A candidate's readiness deadline is at most 2 x READY_PROOF_MS
* after its spawn; the supervisor tick after that deadline stops it (SIGTERM, then SIGKILL after
* CANDIDATE_KILL_GRACE_MS).
*/
export const READY_PROOF_MS = 30_000;
/** One correlated trigger at most. */
Expand Down Expand Up @@ -279,9 +281,13 @@ export function createRouteRounds(host: RouteRoundHost): RouteRounds {
try { phase.child.kill("SIGTERM"); } catch { /* already gone */ }
};

/** Until the grant: READY_PROOF_MS from spawn. After it: READY_PROOF_MS from the grant. */
/**
* Until the grant: READY_PROOF_MS from spawn. After it: READY_PROOF_MS from the grant, and a grant
* seen after that first deadline (before the next tick stopped the candidate) counts from it, so the
* deadline is never later than spawn + 2 x READY_PROOF_MS.
*/
const readinessDeadline = (phase: Extract<Phase, { kind: "candidate" }>): number =>
(phase.grantedAt ?? phase.spawnedAt) + READY_PROOF_MS;
(phase.grantedAt === undefined ? phase.spawnedAt : Math.min(phase.grantedAt, phase.spawnedAt + READY_PROOF_MS)) + READY_PROOF_MS;

const proven = (linkId: string, link: LinkRoutes, round: Round, phase: Extract<Phase, { kind: "candidate" }>): void => {
// A child whose exit was already observed is never promoted, however late its proof settles.
Expand Down
5 changes: 3 additions & 2 deletions src/link/route-trust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ export type ConfirmedLineTrust = "trusted" | "trust_file_unexpected" | "key_chan
* Does the file still trust exactly the key a confirmation wrote for alias? It must pass the same
* whole-file check as a route edit (`trust_file_unexpected` otherwise: OpenCodex writes this file
* only with plain one-alias lines, so another shape is a hand edit this check cannot rule out), and
* its one line for the alias must carry the confirmed key (`key_changed` otherwise). The probe
* computed the confirmed fingerprint from that very line (ssh-keygen -l), so the line carries it.
* every line for the alias, which may be several (a legacy hashed line beside a later plain one),
* must carry the confirmed key type and key (`key_changed` otherwise). The probe computed the
* confirmed fingerprint from the line it wrote (ssh-keygen -l), so that line carries it.
*/
export function confirmedLineTrust(text: string, alias: string, confirmedLine: string): ConfirmedLineTrust {
const found = aliasTrustLine(text, alias);
Expand Down
25 changes: 21 additions & 4 deletions src/link/supervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export interface LinkSupervisorDeps {
/** false when the pid does not exist, true when it does, null when that cannot be told. */
processAlive?: (pid: number) => boolean | null;
pidfileDir?: string;
/** The link's pidfile, null only when there is none; throws when one exists but cannot be read or parsed. */
readPidfile?: (path: string) => Pidfile | null;
writePidfile?: (path: string, pidfile: Pidfile) => void;
removePidfile?: (path: string) => void;
Expand Down Expand Up @@ -206,9 +207,20 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
const readProcessStartTime = deps.readProcessStartTime ?? (platform === "linux" ? linuxProcessStartTime : () => null);
const killProcess = deps.killProcess ?? defaultKillProcess;
const processAlive = deps.processAlive ?? defaultProcessAlive;
const readPidfile = deps.readPidfile ?? ((path: string) => {
try { return parsePidfile(readFileSync(path, "utf8")); } catch { return null; }
const loadPidfile = deps.readPidfile ?? ((path: string): Pidfile | null => {
let text: string;
try { text = readFileSync(path, "utf8"); } catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
throw error;
}
const pidfile = parsePidfile(text);
if (pidfile === null) throw new Error("link pidfile unreadable");
return pidfile;
});
/** Every reader except the orphan-flag lift treats an unreadable pidfile as none, as before. */
const readPidfile = (path: string): Pidfile | null => {
try { return loadPidfile(path); } catch { return null; }
};
const writePidfile = deps.writePidfile ?? ((path: string, pidfile: Pidfile) => {
mkdirSync(storeDir, { recursive: true, mode: 0o700 });
atomicWriteFile(path, `${JSON.stringify(pidfile)}\n`);
Expand Down Expand Up @@ -712,10 +724,15 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
store = nextStore;
// A one-route link flagged at start whose spawn never cleared the flag (ssh missing, say) now
// blocks its rounds: watch that pidfile as a multi-route orphan, or lift the flag when there is
// no earlier process left to watch.
// no earlier process left to watch. A pidfile that exists but cannot be read may still name the
// ssh holding the port, so it keeps the flag; only a missing one (ENOENT) lifts it.
for (const record of gainedRoutes) {
if (!orphanUnverified.has(record.id) || orphanWatches.has(record.id)) continue;
const file = readPidfile(pidfilePath(record.id));
let file: Pidfile | null;
try { file = loadPidfile(pidfilePath(record.id)); } catch {
warn(`[link] pidfile unreadable; link stays blocked link=${record.id}`);
continue;
}
if (file !== null && file.linkId === record.id) reapMultiRouteOrphan(record, file);
if (!orphanWatches.has(record.id)) orphanUnverified.delete(record.id);
}
Expand Down
Loading
Loading