Skip to content

fix(daemon): close a detached daemon that finds its registration deleted or replaced - #4837

Merged
thymikee merged 2 commits into
mainfrom
t3/daemon-closes-on-registration-loss
Nov 20, 2026
Merged

thymikee merged 2 commits into
mainfrom
t3/daemon-closes-on-registration-loss

Conversation

@thymikee

@thymikee thymikee commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Problem

A daemon polls its own daemon.json every 15 s. When the record is deleted or names another process, the daemon writes daemon_metadata_lost to its log and keeps running. No new client can find it. An idle one leaves at the idle reap; one with an open session stays until someone kills it, with its sessions and runner.

This is reachable today by anything that removes or replaces a live daemon's record. It becomes easy to reach with #4682: an older CLI cannot compare the new start-time token, so its daemon stop reports "not running", deletes the record, and leaves the newer daemon alive. The older CLI's own error for a newer daemon tells the user to run that stop.

Change

When the watch reports the loss, a detached daemon closes through the same close() that a signal, a quiesce and the idle reap use. The log line is written first. An embedded runtime does not close: its host still uses it.

armDaemonMetadataLossWatch takes the callback; the runtime passes close for a detached daemon. The watch, its poll and the close path are unchanged. The existing fence still stops a closing daemon from removing a successor's record.

Evidence

Unit (daemon-runtime-metadata-ownership.test.ts):

  • "a detached daemon whose registration is replaced by a successor closes itself" and "... is deleted closes itself": the runtime's closed settles, teardown ran, and its lock is released. Both fail on main (the runtime never closes).
  • "an embedded runtime that loses its registration keeps serving its host": the loss is logged and no teardown runs.

Live, macOS, released 0.21.41 CLI against a build of this branch merged with #4682 (af8197c), one simulator, isolated claim, lease and state directories:

Step Without this change With this change
Daemon has an open session; 0.21.41 runs daemon stop in its state directory Reports "not running", record deleted, daemon alive 3 minutes later Same report; the daemon exits by itself after about 10 s and releases its device claim and lock
Same, then a 0.21.41 command starts a successor at once Two daemons on one state directory The first daemon exits after about 11 s; the successor's record and lock are intact

Local: packages/daemon/src/server (125 files, 974 tests), typecheck, lint, the eager-closure gate and the layering scan pass. No allowlist, baseline or budget rows.

Size

Production: +22 −6, net +16 lines, 5 of them comments. No new module, so the built CLI startup cost does not change.

Notes

  • A client that still holds a connection to the daemon (a long-lived SDK or MCP client) loses its sessions when the daemon closes. Those sessions were already unsafe: a successor settles the device claims of the daemon it replaced.
  • The close logs daemon_shutdown_record_failed and daemon_registration_finish_failed when the lock is already gone. Both are warnings from the existing close path.

Part of #4649. Recommended before #4682.

🤖 Generated with Claude Code


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

View guided diff Turn on auto-fix

…ted or replaced

A daemon whose daemon.json was deleted or taken over only logged
daemon_metadata_lost and kept running: with an open session, until someone
killed it. No new client can find such a daemon, so it now closes by itself.
An embedded runtime keeps serving its host.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.47 MB 6.47 MB +37 B
Package (unpacked) 6.47 MB 6.47 MB +37 B
Package (download) 1.97 MB 1.97 MB +36 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 24.1 ms 26.7 ms +2.6 ms
CLI --help 52.0 ms 56.2 ms +4.2 ms

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 2 new findings on f023aeb. The verdict and the full list are in the summary comment.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 9a758e2

Since the review of f023aeb: 0 new · 0 still open · 2 no longer raised (1 marked fixed)

No open findings.

1 finding below minor hidden (user_settings.min_severity).

Reviewed with this repository's own Sentinel configuration (832b3b3e99f3).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@thymikee

Copy link
Copy Markdown
Member Author

This PR is ready at 9a758e2: a detached daemon now closes when its registration is deleted or replaced, and the code and its regression test hold up on review. All 19 checks pass at this commit, and no conflicts are known. Nothing blocks merge.

Not blocking, and fine to take or leave: when the whole state directory is removed, the watch in daemon-metadata-loss.ts:76 cancels without calling onLoss, so that daemon keeps running unreachable (a follow-up issue could close it on any unreachable registration); and the doc sentence at daemon-metadata-loss.ts:36 says "now", which could be stated as a rule or dropped since daemon-runtime.ts already says it.

Of the two open inline threads, the one on the close initialization order is fixed at this head (#4837 (comment)), and the one on retainRegistration: true does not apply because the record is already gone or names the successor, and skipping the census matches SIGTERM close (#4837 (comment)). Both can be resolved.

Not verified here: the tests were not run, the live stop and successor-race run in the PR body was not reproduced, and runner teardown against a successor on the same device was not traced (the same teardown already ran on idle reap of an orphan before this PR).

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 20, 2026
@thymikee
thymikee merged commit d55238c into main Nov 20, 2026
23 of 24 checks passed
@thymikee
thymikee deleted the t3/daemon-closes-on-registration-loss branch November 20, 2026 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant