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
2 changes: 1 addition & 1 deletion docs-site/src/content/docs/guides/remote-link.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ ocx link routes order --link-id <id> --routes <alias,alias,...>
- **Every route reaches the same machine.** The link's own alias is its *pin*: its confirmed host key is the only key any route may present, and it stays in the list. Before `ocx link routes add` stores a route, the Home connects over it with strict host-key checking against that one key; a route that answers with another key, refuses the login or does not answer is not stored (`route_unproven` with the cause `hostkey`, `auth`, `forward` or `transient`). The Home also refuses the add when its private link `known_hosts` file trusts any key for the alias other than the link's own (`pin_mismatch`, `trust_file_unexpected`). Adding a Child checks the same file: a wildcard, host-list or marker line in it, which OpenCodex never writes, makes it fail with `trust_file_unexpected`; remove that line, then probe and confirm the host again. Hashed lines are fine: releases before 2.89 recorded them on Homes whose SSH configuration hashes known hosts, and each one counts for the alias it hashes; several lines for the alias are fine as long as they all carry the link's key. Confirming a different host key for an alias that a Home-initiated link already uses is refused (`link_pin_conflict`); to trust a replaced machine, remove the link and add it again. If the Home stores a route edit but its running tunnels fail to pick it up, the command fails with `routes_stored_reload_failed`; the edit is kept, and restarting OpenCodex on the Home applies it.
- **A new route goes before the alias** unless you pass `--position`, so direct routes come first and the relay alias stays last.
- **How the Home picks a route.** At each connection it tries the routes in order (10 seconds each). A route counts as working only once the Child's sshd accepted the tunnel and the Child fetched the Home's catalog through it with its link key; until then the link reads **Connecting**. Each route has 30 seconds for its sshd to accept the tunnel and then another 30 seconds for that check. A route that fails rests for a while (seconds after a network error, one minute after a forwarding refusal, five minutes after an authentication failure, ten minutes after a wrong host key) and the Home moves down the list. The link fails on the host key only when every route presented a wrong key.
- **A stale tunnel blocks a link with several routes.** If the Home stopped without stopping a link's ssh, the link reads **failed** with the reason "stale tunnel may hold the port" until that ssh exits; stop it (its pid is in `<config dir>/link/<link id>.pid`). If that pid now belongs to an unrelated program, first make sure no ssh for the link is still running, then delete the `.pid` file and restart OpenCodex on the Home. If OpenCodex cannot read the `.pid` file at all, the link stays blocked the same way, also after a restart. Make the file readable again, and the link waits for the ssh it names to exit, as above; or, once no ssh for the link is running, delete the file, and the link starts within a second.
- **A stale tunnel blocks a link with several routes.** If the Home stopped without stopping a link's ssh, the link reads **failed** with the reason "stale tunnel may hold the port" until that ssh exits; stop it (its pid is in `<config dir>/link/<link id>.pid`). If that pid now belongs to an unrelated program, first make sure no ssh for the link is still running, then delete the `.pid` file and restart OpenCodex on the Home. If OpenCodex cannot read the `.pid` file at all, the link stays blocked the same way, also after a restart. Make the file readable again, and the link waits for the ssh it names to exit, as above; or, once no ssh for the link is running, delete the file, and the link starts within a second, or when its retry is due.
- **Returning to a better route.** Adding or reordering routes does not move a running tunnel, and a link connected on a later route stays there while it works. The Home tries the list from the top again at the next connection: after the tunnel drops, or when you run `ocx link reconnect --link-id <id>`, which interrupts requests that are running on that link.
- **Status.** `ocx link status --json` shows, for a link with several routes, `route` (the route in use and since when), `routes` (each route, when it may be tried again and the last outcome) and `routeBlocked`.
- **Requirements.** The Home's `ssh` must be OpenSSH (the link otherwise reads failed with `ssh_client_unsupported`). The Child needs what adding a Child already needs: `curl` on macOS and Linux, Windows PowerShell on Windows. The route commands use the Home's management API with the admin token on loopback, like `ocx link reconnect`; they are not available through the private control socket a non-loopback Hub opens for `ocx link status`.
Expand Down
23 changes: 16 additions & 7 deletions src/link/supervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,15 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
return sameArgv(actualArgv, argv) && actualStart === startTime ? "same" : "different";
};

/**
* Remove a pidfile whose process is gone. Such a file is harmless (the next start finds its process
* gone again), so a removal that fails (a read-only directory, a sharing lock) is warned and never
* stops the caller: a tick goes on to every other link, and the block it was lifting stays lifted.
*/
const dropStalePidfile = (linkId: string): void => {
try { removePidfile(pidfilePath(linkId)); } catch { warn(`[link] stale pidfile could not be removed link=${linkId}`); }
};

/**
* A multi-route link never starts a candidate while an earlier tunnel of it may live: the readiness
* proof's attribution relies on that. Only a process proven to be the pidfile's own (pid, argv and
Expand All @@ -349,9 +358,8 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
* instance's watch only observes, as no watch acts for a replaced instance.
*/
const reapMultiRouteOrphan = (record: LinkRecord, pidfile: Pidfile, act = true): void => {
const path = pidfilePath(record.id);
const watched: WatchedProcess = { pid: pidfile.pid, argv: pidfile.argv, ...(pidfile.startTime === undefined ? {} : { startTime: pidfile.startTime }) };
if (processGone(watched)) { if (act) removePidfile(path); return; }
if (processGone(watched)) { if (act) dropStalePidfile(record.id); return; }
orphanUnverified.add(record.id);
const identity = platform === "linux" && pidfile.startTime !== undefined ? processIdentity(pidfile.pid, pidfile.argv, pidfile.startTime) : "unreadable";
if (act && identity === "same") {
Expand Down Expand Up @@ -431,7 +439,7 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
continue;
}
orphanUnverified.delete(linkId);
if (!watch.observeOnly && file !== null && file.linkId === linkId && sameFile(file, watch.file)) removePidfile(pidfilePath(linkId));
if (!watch.observeOnly && file !== null && file.linkId === linkId && sameFile(file, watch.file)) dropStalePidfile(linkId);
// The link was waiting for this: start its first round now.
const record = store.links.find(link => link.id === linkId);
if (record && !children.has(linkId) && (states.get(linkId) ?? IDLE).kind === "idle") spawnFor(record);
Expand Down Expand Up @@ -803,11 +811,12 @@ export function createLinkSupervisor(deps: LinkSupervisorDeps = {}): LinkSupervi
}
for (const record of nextStore.links) if (!rounds.isMultiRoute(record)) orphanWatches.delete(record.id);
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 (recheckPidfile: an unreadable pidfile keeps the flag).
// A one-route link that gains routes with no running child (its spawn failed: ssh missing, say)
// settles from its pidfile, flagged or not: removing a link drops its flag, so a re-added link
// may carry none. Watch an earlier process, or lift the flag when there is none (recheckPidfile:
// an unreadable pidfile keeps the flag).
for (const record of gainedRoutes) {
if (!orphanUnverified.has(record.id) || orphanWatches.has(record.id)) continue;
if (orphanWatches.has(record.id) || children.has(record.id)) continue;
recheckPidfile(record);
}
if (routesEdited.some(record => rounds.isMultiRoute(record))) rounds.recheckClient();
Expand Down
Loading
Loading