Skip to content

fix(link): send SIGKILL to a one-route ssh that ignores SIGTERM, keep a stopped one-route child owned until it exits, and fix orphan wording - #8640

Merged
lidge-jun merged 4 commits into
devfrom
chore/link-orphan-wording-and-tick-exit
Nov 13, 2026
Merged

lidge-jun merged 4 commits into
devfrom
chore/link-orphan-wording-and-tick-exit

Conversation

@lidge-jun

Copy link
Copy Markdown
Owner

Summary

The last follow-up to #8626 (issue #8609), from the final review of that PR.

A failed link's running child was stopped without waiting. A one-route spawn registered its ssh child, wrote the pidfile, and only then attached the exit watch. If the pidfile write failed, the link went to failed while the child kept running unwatched. The next supervisor tick sent it SIGTERM, forgot it, and removed the pidfile naming it without waiting for the exit. Until that ssh actually exited, two things could start past it: the link's retry, or a reload that gave the link routes (it took the no-child path, read ENOENT, and started a round). An ssh that ignored SIGTERM never got SIGKILL and was never watched again.

The spawn now attaches the exit watch before writing the pidfile, as the multi-route spawn already did. For a successful write, only the moment the exit callback is registered moves. The tick sends a failed link's running child SIGTERM once and SIGKILL 2 s later (CANDIDATE_KILL_GRACE_MS, as for a stopped candidate), and keeps the child until its exit is seen. The exit watch then forgets it and removes the pidfile conditionally, as for any exit. The reducer leaves a failed, not-in-flight state unchanged on an exit, so the retry schedule is the same. Until that exit, neither the retry nor a reload that gives the link routes starts a tunnel: the reload stops the child and settles from the pidfile after the exit (#8626).

No stop waits forever on an ssh that ignores SIGTERM. Before, a stop, reload or reconnect sent a one-route child only SIGTERM and then awaited its exit, and the tick, which could have escalated, returned early while that lifecycle work ran. A stubborn ssh therefore held the lifecycle queue for every link. Shutdown cleared the timer before its stops and had the same problem. Now:

  • stopChild records the child it drains, keeping a stop time the tick already set.
  • The tick sends SIGKILL to every draining child 2 s after its first SIGTERM. It does this before its early returns for pending lifecycle work and for shutdown.
  • Shutdown keeps the timer until its stops are done. It waits for every stop to settle, so one stop that throws cannot cut another child's drain short. It clears the timer in a finally block, so a failed stop never leaves the interval running, and still rethrows the first failure.

Wording.

  • In structure/remote-link.md, the ENOENT clause of the gained-routes sentence now follows "gets the same watch from its pidfile", so it no longer reads as the adopted child's outcome. The doc also describes the stop and SIGKILL behaviour above.
  • The dropStalePidfile comment names its callers: a process that is gone, one sent SIGTERM at one-route startup, or this process's own child that exited or was stopped.
  • The observe-only comment says "as for a changed record", because a route edit that restarts the child keeps the record's instance.
  • The test file loses its double and trailing blank lines.

What keeps this inert. Nothing changes for an ssh that exits on SIGTERM within 2 s, which is every ssh in normal operation, or for a one-route spawn whose pidfile write succeeds. Behaviour changes only when a pidfile write fails, when an ssh ignores SIGTERM for 2 s, or when a child's stop throws during shutdown. No configuration or default changes, and recordInstance is untouched.

Verification

Tests written first in tests/clients/link-route-supervisor.test.ts. The harness can now make a tunnel's kill throw and counts timer clears. With src/link/supervisor.ts put back to dev (6ee1b91), the final test file gives 74 pass and 7 fail; four of the seven fail by the 5 s test timeout, because the old code never sends SIGKILL and the stop never ends.

  • a one-route tunnel the tick stops after its pidfile write failed is watched until it exits: SIGKILL after the grace, no second tunnel meanwhile: on dev, no SIGKILL.
  • a reload that gives routes while a stopped, unrecorded one-route tunnel is still exiting starts no round until it exits: on dev a probe starts while the old ssh lives.
  • a reload that stops a running child / a child the tick already stopped which ignores SIGTERM sends SIGKILL after the grace: SIGKILL is asserted while the reload is still pending.
  • shutdown during a reload's drain / on its own still sends SIGKILL after the grace to a one-route child that ignores SIGTERM.
  • a shutdown whose stop of one child fails still escalates the other drains and clears its timer.

The existing #8626 test of a tick stop whose pidfile removal fails still passes; that removal now happens when the exit is seen.

Commands on the head (exit codes):

  • TMPDIR=/tmp bun test tests/clients/link-route-supervisor.test.ts tests/clients/link-supervisor.test.ts: 117 pass, 0 fail (exit 0).
  • bun run typecheck: exit 0. bun run privacy:scan: exit 0. bun run structure:check: exit 0.
  • 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 (212 files, including tests/clients/link-boundary.test.ts): 3626 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. The signals go through the same runner kill the stopped-candidate path already uses on every platform.

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

  • Round 1 found one P1: a reload, reconnect or stop that began before the grace never escalated.
  • Round 2 found one P1: shutdown cancelled an escalation already in progress.
  • Round 3 found one P2: a failed stop during shutdown left the timer running.

Each fix has a test in the list above. Round 4 found no P0–P2 in the whole slice and ran the two supervisor test files on the head (117 pass).

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.

…n, and fix orphan wording

A one-route spawn whose pidfile write failed left its child unwatched; the next
tick sent it SIGTERM, forgot it and removed the pidfile without waiting, so the
retry or a reload that gave the link routes could start a tunnel while that ssh
was still exiting, and an ssh that ignored SIGTERM was never killed. The spawn now
attaches its exit watch before writing the pidfile, and the tick sends SIGTERM
once, SIGKILL after 2 s, and keeps the child until its exit is seen; the exit
watch then removes the pidfile conditionally, as for any exit.

Also: the ENOENT clause of the gained-routes sentence in structure/remote-link.md
no longer reads as the adopted child's outcome, the dropStalePidfile and
observe-only comments match their callers, and the test file loses its double and
trailing blank lines.

Refs #8609
… after the grace

A stop, reload or reconnect sent a one-route child only SIGTERM and awaited its
exit, and the tick that could escalate returned early while that lifecycle work
ran, so an ssh that ignored SIGTERM held the lifecycle queue for every link. The
tick now sends SIGKILL to a draining child 2 s after its first SIGTERM, keeping a
stop time the tick already set. Shutdown clears the timer first and still sends
only SIGTERM.

Refs #8609
Shutdown cleared the supervisor timer before its stops, and the tick returned at
once while stopping, so a child a reload or reconnect was draining, or one
shutdown itself stopped, never got its SIGKILL when it ignored SIGTERM. Shutdown
now clears the timer after its stops, and the tick sends the drain SIGKILL before
its stopping check.

Refs #8609
Shutdown now waits for every child stop to settle, so one stop that throws cannot
cut another child's drain short, and clears the timer in a finally block, so a
failed stop never leaves the interval running. The first failure is still
rethrown.

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 04:03
@lidge-jun
lidge-jun requested a review from Ingwannu as a code owner November 13, 2026 04:03
@lidge-jun
lidge-jun merged commit 61dfb8b into dev Nov 13, 2026
28 checks passed
@lidge-jun
lidge-jun deleted the chore/link-orphan-wording-and-tick-exit branch November 13, 2026 04:03
@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-13T04:05:08.638875Z be2053a 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.

lidge-jun added a commit that referenced this pull request Nov 13, 2026
One conflict, the import block of src/link/supervisor.ts: dev's
"CANDIDATE_KILL_GRACE_MS, createRouteRounds, type LinkRouteStatus" import is
kept and this branch's operations-journal import is added after it. The rest of
both sides merged on their own: a suspended link whose child is draining is
still escalated and forgotten when it exits, and spawnFor then declines it; a
revoke's hold-then-stopLink now ends within about 2 s for an ssh that ignores
SIGTERM.
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