Skip to content

fix(link): settle a gaining link from its pidfile when a reload restarts it, and let every pidfile removal fail softly - #8626

Merged
lidge-jun merged 4 commits into
devfrom
fix/link-orphan-reload-instance-and-pidfile-removal
Nov 13, 2026
Merged

lidge-jun merged 4 commits into
devfrom
fix/link-orphan-reload-instance-and-pidfile-removal

Conversation

@lidge-jun

Copy link
Copy Markdown
Owner

Summary

The last follow-up to #8621 (issue #8609), from the final review of that PR. A Remote Link with several routes must never start a route candidate while an earlier ssh tunnel of it may still run. The Home supervisor flags such links orphan-unverified and watches the process named in the link's pidfile.

A link that gained routes during a restart skipped its pidfile. #8621 made a one-route link that gains routes settle from its pidfile even without a flag. A reload sends some links through its restart path instead of the gained-routes check, and those links were never checked:

  • A record whose instance changed. The instance covers the alias, fingerprint, API key id, createdAt, tunnel port and the store-wide listener port, so this includes a link that one reload gave both a new listener port and routes.
  • A route edit on a one-route link whose child is not connected yet.

After such a reload the link's rounds started without the pidfile being read. That matters most when a one-route spawn's pidfile write failed: the child is registered before the write, so the older pidfile stays in place and may name an earlier live ssh. Every link that goes from one route to several through a restart now settles from its pidfile, observe-only (no signal, no removal: the rule for a replaced record), after the reload has stopped its child. Only a child in the connected state, which the link adopts as its tunnel, skips the check. That behaviour is unchanged, and structure/remote-link.md notes that for a one-route tunnel the connected state is the grace or catalog heuristic, so it does not prove the forward is held.

Every pidfile removal now fails softly. #8621 made the orphan removals warn instead of throw. The removals of this Home's own pidfiles still threw on anything other than ENOENT (a read-only directory, an immutable flag, a Windows sharing lock):

  • After a tunnel exit, the exit was never classified, including a multi-route candidate's route outcome.
  • At the tick that stops a failed link's running child, the rest of that tick was skipped.
  • When stopping a child, the stop or reload was aborted.
  • At one-route startup, the error escaped start().

conditionalRemovePidfile and the one-route startup removal now go through the same dropStalePidfile helper, which logs [link] stale pidfile could not be removed link=<id> and goes on. The read and the condition (the pidfile names this link and this child) are unchanged, so a readable, removable pidfile is handled exactly as before; a test pins that.

Comments and docs. The helper's comment no longer calls a leftover pidfile harmless without qualification. On Linux with a start time the next start sees its process gone; without a start time, a pid reused by another process blocks a multi-route link at the next start until that process exits. structure/remote-link.md now says a process with a proven identity is signalled at startup, when a reload adds the link or gives it routes, or on a later tick once an unreadable pidfile becomes readable. It also covers the new removal sites and the restart-path check. The guide has no sentence about signalling, so it is unchanged.

What keeps this inert. Behaviour changes only when a pidfile removal fails, or when a reload gives a one-route link routes through its restart path; the latter costs one pidfile read, and a link without a pidfile is unaffected. One-route links still spawn, clear their flag and overwrite their pidfile as before, which is the documented one-route exception. No configuration or default changes.

Verification

Tests written first in tests/clients/link-route-supervisor.test.ts. The harness's pidfile-write option now receives the path and the pidfile, so a test can read back what the supervisor wrote. With src/link/supervisor.ts put back to dev (ad3eb91), the final test file gives 66 pass and 8 fail:

  • a reload that changes an unflagged one-route link's record and gives it routes still checks its pidfile: on dev the link is not flagged.
  • a one-route link whose pidfile write failed and that gains routes while it changes its record in the same reload / keeps its record settles from the older pidfile once its tunnel is stopped: started without a tick. On dev the link is not flagged.
  • a one-route startup that stops an earlier ssh whose pidfile cannot be removed still starts the link: on dev, EACCES escapes start().
  • a one-route / multi-route tunnel exit whose pidfile cannot be removed is still classified: on dev the removal error escapes and the exit is not classified.
  • a reload that stops a one-route link whose pidfile cannot be removed still completes: on dev the reload rejects.
  • a one-route tunnel stopped by the tick after its pidfile write failed survives a pidfile that cannot be removed: on dev the removal error escapes the tick.
  • a one-route exit removes exactly its own pidfile once: pins existing behaviour, so it passes on dev and on this branch.

The tick branch that stops a failed link's running child can be reached: after a one-route pidfile write fails, the link is failed with a running child. The last test above covers that branch.

Commands (exit codes):

  • On the head: TMPDIR=/tmp bun scripts/test.ts --parallel=2 tests/clients/link-route-supervisor.test.ts tests/clients/link-supervisor.test.ts tests/clients/link-boundary.test.ts tests/ci-workflows/structure-ssot.test.ts tests/ci-workflows/ci-structure-gate.test.ts tests/test-layout.test.ts tests/test-layout-tooling.test.ts tests/ci-workflows/file-size-ratchet.test.ts: 205 pass, 0 fail (exit 0).
  • On the head: bun run typecheck: exit 0. bun run privacy:scan: exit 0. bun run structure:check: exit 0.
  • On 37231f3, the last code commit; the two commits after it change only structure/remote-link.md: TMPDIR=/tmp bun scripts/test.ts --parallel=2 tests/clients tests/server/link-management-routes.test.ts tests/test-layout.test.ts tests/test-layout-tooling.test.ts tests/ci-workflows/structure-ssot.test.ts tests/ci-workflows/ci-structure-gate.test.ts tests/ci-workflows/file-size-ratchet.test.ts (206 files): 3558 pass, 67 skip, 3 fail (exit 1). The 3 failures are the Rust ocx-child tests (tests/clients/link-ocx-child-e2e.test.ts, two cases, and the Rust case of tests/clients/link-ocx-child-conformance.test.ts). They stop before any test code runs, because cargo on the build machine has no default rustup toolchain. This branch touches no Rust code.

NOT RUN: the full bun run test suite (the build machine allows focused files only; CI covers the rest); the three Rust ocx-child tests above (no toolchain); Windows and macOS. A Windows sharing lock is the case the removal fix covers, exercised here through an injected EACCES.

Review: four read-only rounds by one independent reviewer (gpt-6.1-terra, xhigh effort), continued across rounds.

  • Round 1 found one P1: excluding links with a child missed an older pidfile left by a failed write. Fixed, and the route-edit restart path was included.
  • Rounds 2 and 3 found two P2s on the wording about the adopted child ("proven", then "holds the forward"). Both fixed.
  • Round 4 found no P0–P2 in the whole slice.

The reviewer also confirmed that the observe-only re-check never signals, and that readable, removable pidfiles are handled as before.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

…every pidfile removal fail softly

A reload that both changed a one-route link's record (a new listener port, say)
and gave it routes skipped the gained-routes pidfile check, so an unflagged link
with no child could start a candidate while an earlier ssh held the port. Such a
link now settles from its pidfile, observe-only, with the flagged changed links.
The conditional removal after a tunnel's exit or stop and the one-route startup
removal now go through the same warning helper as the orphan removals, so a failed
removal no longer aborts a tick, an exit's classification, a stop, a reload or the
start. Readable and removable pidfiles are handled exactly as before.

Refs #8609
…ops its unproven child

A one-route link whose pidfile write failed keeps its child registered and the
older pidfile in place, which may name an earlier live ssh. When a reload gave
such a link routes, with or without a record change, it stopped the child and
started rounds without reading that pidfile. Every link that goes from one route
to several through a restart now settles from its pidfile, observe-only, after the
stop. A test also covers the tick that stops an unrecorded child when the removal
of its pidfile fails.

Refs #8609
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Nov 13, 2026
@lidge-jun
lidge-jun marked this pull request as ready for review November 13, 2026 03:36
@lidge-jun
lidge-jun requested a review from Ingwannu as a code owner November 13, 2026 03:36
@lidge-jun
lidge-jun merged commit 6ee1b91 into dev Nov 13, 2026
28 checks passed
@lidge-jun
lidge-jun deleted the fix/link-orphan-reload-instance-and-pidfile-removal branch November 13, 2026 03:36
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T03:38:04.441628Z b1c9743 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant