Skip to content

feat(cli): dune ps probes health and reads the instance registry - #53

Merged
zumbrunn merged 26 commits into
mainfrom
feat/ps-registry-health
Oct 27, 2026
Merged

zumbrunn merged 26 commits into
mainfrom
feat/ps-registry-health

Conversation

@zumbrunn

@zumbrunn zumbrunn commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Refs #42

Draft, stacked on three open PRs. This branch contains #37 (dune ps/dune stop), #51 (watchdog) and #52 (registry) via merge commits; review only feat(cli): dune ps probes health and reads the instance registry and fix(cli): dune ps probes more carefully. I'll retarget to main and rebase once those land.

What

  • Health probe. Every listening instance is sent a GET /health/live (it shows up in the instance's access log). A blocked event loop still lets the kernel accept the TCP connection but never answers it, so the instance is shown as NOT RESPONDING and counted separately: the case lsof can't see.
    • It probes the port the instance registered when it did (any HTTP answer counts); otherwise every listening port, counting only dune's own answer (2xx JSON with a status field). A debugger's inspector port (9229) can't make a hung server look healthy.
    • It takes three failed probes in a row (3 s each, ~10 s in all). One slow answer from a busy server proves nothing. A port that speaks HTTP but not like dune yields "unknown", never a hung verdict.
    • Probes 127.0.0.1, not localhost.
  • Registry. dune ps reads the records from feat(runtime): instance registry written by dev and serve #52. A registered process is attached to its instance (adding its @dune/core version) and counts as confirmed (the process itself says it is a dune site, stronger evidence than anything on its command line); one the command-line matching misses is added from its record. Stale records are pruned as a side effect of listing.
  • dune stop --idle also takes confirmed instances that are listening but not responding, once they are at least 2 minutes old (a server that just bound its port may still be indexing synchronously).

Why a probe rather than a registry marker

(Plan change noted on #52.) Marking "hung" from the watchdog only helps in warn/off mode: in the default mode a hung process is killed within seconds, so NOT RESPONDING mostly matters there. A probe needs no cooperation from the stuck process and also works for unregistered ones.

Verification

  • deno lint src/; deno test -A --unstable-kv tests/cli: 417 passed
  • probePort tells dune's answer, another HTTP service and silence apart; judgeResponsive needs every attempt to fail, prefers the registered port, and treats the inspector case as "unknown"; real processes for a responding instance, a listener that accepts but never answers, a not-listening instance (not probed), a process found only via the registry (version, root, title, port), one found both ways (listed once), a registered process confirmed even though its command line says nothing about a site, and a stale record (adds nothing).
  • Live (earlier revision): DUNE_WATCHDOG=off dune serve with a page that spins forever; dune ps showed NOT RESPONDING; dune stop <pid> left it alive (a blocked loop can't handle SIGTERM) and printed the --force hint; --force SIGKILLed the tree.

Known limits

  • A very busy server that stays silent for ~10 s is flagged; the wording says "may be hung" and --idle lists what it would stop and asks first.
  • A multi-site instance without a registry record and without a dune-style /health/live answer reads as "unknown" or "not responding" depending on what its ports say.
  • Docs: dune-docs#12.

🤖 Generated with Claude Code

zumbrunn and others added 8 commits October 22, 2026 09:28
…stening", adds --stop

Found while cleaning up a machine running ~45 dune processes: `dune ps` listed
19 of them as "port unknown — is lsof installed?" and missed the one server
that was actually serving.

- Matching: besides the generated `main.ts dev|serve`, match the dune CLI run
  directly (`cli.ts dev|serve` via the global shim or a checkout,
  `jsr:@dune/core@x/cli serve`) and `deno task dev|serve` wrappers. A bare
  `cli.ts` / `deno task` could be any project, so those are "weak" matches
  that count only when a config/site.yaml is found via --root, PWD or live cwd.
- One instance per process tree: wrapper, re-exec child and esbuild workers
  are grouped under the topmost matching process, from one `ps -eo
  pid,ppid,...` snapshot (drops the pgrep dependency). `--root` on the
  command line is used to find the site, resolved against the process's cwd
  when relative.
- Wording: `portStatus` distinguishes "listening", "not-listening" (lsof ran,
  found nothing: a hung or leftover process) and "unknown" (lsof can't be
  spawned). lsof exits 1 on no matches, so availability is checked by
  spawning it, not by its exit code.
- `--stop`: SIGTERM to every pid in every listed instance tree, waits, reports
  survivors (exit code 1). Asks first; --yes skips the prompt; without a
  terminal and without --yes it refuses. Never signals this process or its
  ancestors, whatever the caller passes. It stops everything it lists, so it
  is documented as such.

Tests: pure matching/--root parsing tables; real processes for the CLI-style
invocation (absolute and relative --root), a not-listening process, a
`deno task` tree reported once, and stopInstances really stopping a tree and
refusing to signal itself; the command flow runs against injected fakes so no
test can ever stop a real instance. Mutation-checked: disabling tree grouping
and the weak-match filter each fail exactly their test. Verified on the real
machine: the previously-missed iofc dev server is now listed with title, root,
port and children, and `ps --stop` without --yes/TTY refuses and kills nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…alone

`dune ps --stop` is all-or-nothing, and on a shared machine that includes
servers you want: while building --stop, a live dev server sat next to ~45
hung leftovers. --idle narrows both the listing and --stop to instances that
are running but "not listening" (hung or leftover), so
`dune ps --stop --idle` clears the debris and leaves anything serving alone.
`dune ps --idle` alone is the preview.

An instance whose port state is unknown (no lsof) is never assumed idle: with
nothing provably idle, --idle says so and --stop does nothing (exit 1). When
nothing is idle it says "No idle instances" rather than prompting. The
confirmation names the idle count and the process count.

Tests (fakes only, so nothing real can be stopped): the listing shows only
idle instances and counts the live ones left out; --stop --idle hands the
stopper only idle instances and not even a live one's child pids; it still
refuses without --yes and a terminal; the nothing-idle and lsof-unavailable
cases. Disabling the filter fails 4 of them, including the safety one. Also
run for real on the machine: with a live and an idle fixture next to a live
server from another session, `ps --stop --idle --yes` stopped only the idle
fixture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A process can ignore SIGTERM indefinitely. A `dune serve` closed its listener
on the first SIGTERM and then stayed alive until it was sent a second one, so
"SIGTERM, then report survivors" left exactly the idle processes this command
exists to clear, and made a plain `--stop` on a live serve end in "still
running".

stopInstances() now sends SIGTERM, waits up to a 10 s grace period (for real
servers draining in-flight requests), then SIGKILLs whatever is still alive and
waits briefly for it to go. StopResult gains `killed` (the subset of `stopped`
that needed the SIGKILL) so the escalation is never silent; `survivors` now
means "alive even after SIGKILL" and is the only thing that makes the exit code
non-zero. Still never signals this process or its ancestors.

Tests: a real process that swallows SIGTERM is escalated (killed, stopped, no
survivors, died by SIGKILL), a cooperative process is not SIGKILLed, and the
command's output names the killed pids and says nothing about SIGKILL when
there were none. Removing the escalation fails the stubborn-process test.
Also reproduced the original situation for real: a serve that got one SIGTERM
sat idle, `ps --idle` listed it, and `ps --stop --idle --yes` cleared it while
a live server from another session stayed up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One synchronous loop blocks the only thread serving requests, so every URL
stops answering at once with nothing in the logs. A Worker now expects a
heartbeat from the main thread; after 15s (DUNE_WATCHDOG_TIMEOUT_MS) of
silence it writes a report to <site>/.dune/hangs/ (in-flight requests, plus a
native stack sample on macOS) and SIGKILLs the process so a supervisor can
restart it. DUNE_WATCHDOG=warn reports only, =off disables.

SIGKILL rather than Deno.exit(): an exit requested from a Worker only ends the
Worker, the spinning main thread carries on. A Worker also can't be unref'd, so
serve's graceful-shutdown paths stop it explicitly.

The main thread additionally logs a warning when its own timer ran over a
second late.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Each running instance records itself in ~/.dune/instances/<pid>.json (pid,
process start time, mode, root, title, port, version) and removes the record on
a graceful exit. Readers verify a record against the live process (alive and
same start time, since pids are reused) and prune stale ones. Groundwork for
`dune ps` to list instances from what each one says about itself instead of
parsing command lines and lsof; nothing reads it yet.

Refs #42

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
`dune ps` is read-only again. Stopping is its own command with a required
target (a pid, --port, --idle or --all), SIGTERM by default and SIGKILL only
with --force, so stopping every server on a machine has to be asked for by
name and a kill that can orphan a wrapper's child is opt-in.

Code is split along the same line: instances.ts (discovery, shared), ps.ts
(listing), stop.ts (selection, confirmation, signalling). Tests follow, with the
shared fixtures in ps_helpers.ts.

Refs #46

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Every listening instance gets a /health/live request (3 s timeout, any status
counts); one that accepts the connection but never answers is shown as NOT
RESPONDING, the signature of a blocked event loop that lsof can't see. Instances
that registered themselves are listed even when their command line doesn't look
like dune, and show their version. `dune stop --idle` also takes instances that
are listening but not responding.

Refs #42

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
zumbrunn added a commit to duneorg/dune-docs that referenced this pull request Oct 26, 2026
Refs duneorg/dune#53

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
zumbrunn and others added 5 commits October 26, 2026 22:46
…imately busy

Review of #51 and a test of my own turned up false-kill cases and a kill that
could fail to happen:

- A process suspended and resumed (laptop sleep, Ctrl-Z/SIGSTOP) was killed on
  resume: confirmed by SIGSTOPping a watched process for 4s. The stall clock is
  now monotonic (performance.now()), and a gap in the Worker's own ticks resets
  it instead of firing.
- Startup gets its own, much longer limit (2 min) until the server calls
  ready(), so a big site's initial indexing can't put a supervised process into
  a kill/restart loop.
- watchdogProgress() (rate-limited heartbeat for long synchronous loops) and
  watchdogGrace() (capped, auto-expiring, nestable busy window), exported as
  @dune/core/watchdog.
- Under --inspect/--inspect-brk the default becomes warn.
- The kill no longer depends on the report: writing it is raced against a
  timeout, and a failed Deno.kill is logged instead of silencing the watchdog.
- Reports are pruned to the newest 20, and path segments over 24 characters are
  masked before they are written.

Refs #42

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Review of #37:
- --all/--idle act only on confirmed instances (a config/site.yaml at the site
  root). A command line that merely looks like dune (any Deno project's
  `main.ts serve`, a path containing "dune") is still listed but has to be named
  by pid, with a warning.
- Only the current user's processes are listed and signalled; a refused signal
  (EPERM) is reported as "not permitted", not as "ignored SIGTERM".
- Every pid's start time is captured when the instance is found and re-checked
  right before each SIGTERM and SIGKILL, so a pid that exited and was reused
  while the confirmation prompt was open is never signalled.
- --idle leaves out a not-listening instance younger than 2 minutes: it may
  still be starting (indexing a big site).
- Help and docs say that the whole process tree is stopped.

Refs #46

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Review of #53:
- Probe the port the instance registered, else every listening port, counting
  only dune's own answer: with --inspect (9229) on the lowest port, probing
  ports[0] would have made a hung server look healthy.
- Three failed probes in a row (about 10 s) before "not responding"; one slow
  answer from a busy server no longer counts. A port that speaks HTTP but not
  like dune yields "unknown", never a hung verdict.
- --idle takes not-responding instances only once they are 2 minutes old, and
  only confirmed ones (a site root, or a registry record).
- Probe 127.0.0.1 rather than localhost.
- Drop the stray deno.lock line from test-run drift.

Refs #42

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
zumbrunn and others added 12 commits October 27, 2026 16:15
Second-pass review of #37:
- "confirmed" now needs evidence from the command line itself (the directory of
  the main.ts it runs, --root, or the dune CLI's cwd), not merely that the
  process runs from inside a site directory, which any tool started from a site
  repo inherits. stop prints each instance's full command line before asking.
- Our own uid is read from our own row in the process table instead of
  Deno.uid(), which needs --allow-sys; without it `dune ps` used to throw.
- A missing --allow-run now says so (instead of "no instances" or "already
  gone"), and Deno.errors.NotCapable on a kill is reported separately from
  EPERM (another user's process) and from a pid that already exited.

Refs #46

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ealth

Brings the stricter 'confirmed' (registry records also confirm) and the
permission handling from #37.
# Conflicts:
#	CHANGELOG.md
#	src/cli/dev.ts
#	src/cli/serve.ts
@zumbrunn
zumbrunn changed the base branch from feat/ps-shim-notlistening-stop to main October 27, 2026 16:48
@zumbrunn
zumbrunn marked this pull request as ready for review October 27, 2026 17:31
@zumbrunn
zumbrunn merged commit 379d84a into main Oct 27, 2026
7 checks passed
zumbrunn added a commit to duneorg/dune-docs that referenced this pull request Oct 27, 2026
… listening"; new dune stop (#12)

* docs(cli): dune ps finds the dune CLI and task wrappers, reports "not listening", and has --stop

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(cli): document dune ps --idle, the selective stop

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(cli): dune ps --stop escalates to SIGKILL after a grace period

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(cli): dune stop replaces dune ps --stop / --idle

Refs duneorg/dune#46

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* docs(cli): dune ps health probe and instance registry

Refs duneorg/dune#53

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* docs(cli): dune stop acts only on confirmed instances; probe details

Refs duneorg/dune#53

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* docs(cli): what 'confirmed' means for dune stop; permissions

Refs duneorg/dune#46

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant