Skip to content

fix(daemon): give a closing daemon its full teardown before anything kills it - #4806

Merged
thymikee merged 4 commits into
mainfrom
t3/daemon-teardown-grace
Nov 20, 2026
Merged

thymikee merged 4 commits into
mainfrom
t3/daemon-teardown-grace

Conversation

@thymikee

@thymikee thymikee commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Problem

A closing daemon stops listening at its first teardown step and removes daemon.json at its last. Until it exits, another client sees a live, registered process that refuses connections.

  • A compatible client judged that daemon "unreachable" after about 1 s, sent SIGTERM (ignored, because the close is already running) and sent SIGKILL 3 s later.
  • daemon stop sent SIGKILL 10 s after SIGTERM.
  • The daemon bounds its own teardown at about 13 s when idle, and near 69 s for a web session that records (23 s of fixed steps, including the 4 s app-log stop, plus a 46 s session budget). Steps without a budget are not measured, so 75 s is a budget, not a measurement.

A killed teardown skips lease release, claim release, runner shutdown and the shutdown report. On a host shared by several agents, the trigger is another agent's command that arrives while a daemon closes (idle reap, an accepted drain, or daemon stop).

ensureDaemon() also judged a leaving daemon again for only 10 s (40 × 250 ms) and then failed with daemon-lifecycle-contended, although its comment says "for as long as a drain can take".

Change

One constant, DAEMON_TEARDOWN_GRACE_MS (75 s) in daemon-contracts/daemon-process:

  • A graceful retirement (stopAndRetireDaemon) waits this long after SIGTERM by default (was 3 s). The wait ends when the process exits.
  • daemon stop passes the same grace explicitly (was 10 s), and its test asserts 75 s. DAEMON_STOP_GRACE_TIMEOUT_MS is deleted.
  • The cleanup of a one-shot daemon after replay (cleanupDaemonAfterRequest in daemon-client-lifecycle.ts, including replay --save-script) uses the default, so it now waits up to 75 s before SIGKILL (was 3 s). That daemon also tears down a session.
  • The ensureDaemon() judge-again budget is derived from it (was a fixed 10 s).
  • A managed takeover inside a startup caps the wait at the remaining startup budget, so the startup ceiling still holds.
  • The cleanup of a client's own failed launch keeps 3 s: that daemon never served, so it has no teardown to wait for.

Cost: a daemon that is stuck in its teardown delays the caller by up to 75 s before SIGKILL (was 4 s, or 12 s for daemon stop).

Not in this PR: the request-timeout path still decides with one 1 s liveness probe, where ensureDaemon() uses three.

Evidence

  • daemon-client-ensure-drain.test.ts, "a compatible daemon that is closing is waited for, not killed, by another client": a registered same-version daemon refuses connections and takes 30 virtual seconds to leave. On the branch the client sends one SIGTERM and no SIGKILL, then starts the successor. With the production change reverted the test fails: SIGKILL is recorded.
  • session-teardown-budget.test.ts, "the grace a client gives a closing daemon covers the largest teardown the daemon bounds": fails if a teardown budget grows past the grace.
  • packages/daemon-client, packages/daemon-contracts and the budget test: 64 files, 593 tests pass. Eager-closure gate (913 tests), typecheck, lint and the layering scan pass. No allowlist, baseline or budget rows.

Size

Production: +33 −8, net +25 lines (12 are comments). No new module, so the built CLI startup cost does not change.

Part of #4649.

🤖 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

…kills it

A closing daemon stops listening first and removes its registration last.
Another client read that as unreachable and sent SIGKILL 3 s after SIGTERM,
and daemon stop did the same after 10 s, while the daemon's own teardown is
bounded at up to 65 s. One grace of 75 s now covers graceful retirement,
daemon stop and the ensureDaemon judge-again budget.

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.45 MB 6.45 MB +661 B
Package (unpacked) 6.45 MB 6.45 MB +661 B
Package (download) 1.97 MB 1.97 MB +225 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 23.6 ms 22.7 ms -1.0 ms
CLI --help 50.9 ms 47.9 ms -3.0 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: 4 new findings on 13a4927. The verdict and the full list are in the summary comment.

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

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

This PR is ready at c3d0f3c: nothing needs a change before merge, and CI is green (19 checks, 0 not passing). There are no conflicts, and the PR can merge once the open review threads below are resolved.

Not blocking, and you can take or leave these: (1) the 75 s that daemon stop promises comes only from the stopAndRetireDaemon default (https://github.com/callstack/agent-device/blob/c3d0f3c/packages/daemon-contracts/src/daemon-stop.test.ts#L61), so passing termTimeoutMs: params.graceTimeoutMs ?? DAEMON_TEARDOWN_GRACE_MS in daemon-stop.ts and asserting that value would guard it; (2) the one-shot replay --save-script cleanup (https://github.com/callstack/agent-device/blob/c3d0f3c/packages/daemon-client/src/daemon-client-lifecycle.ts#L356) now waits 75 s instead of 3 s before SIGKILL, so the PR body could name that route; (3) the budget guard (https://github.com/callstack/agent-device/blob/c3d0f3c/packages/daemon/src/sessions/__tests__/session-teardown-budget.test.ts#L53) should sum every budget the shutdown path awaits, including the 4 s app-log stop, which puts the worst case near 69 s, not 65 s; (4) ENSURE_ATTEMPTS (https://github.com/callstack/agent-device/blob/c3d0f3c/packages/daemon-client/src/daemon-client-ensure.ts#L39) counts attempts, not time, so could a wall-time bound on the loop be clearer, with a comment that names it; (5) the ADR line (https://github.com/callstack/agent-device/blob/c3d0f3c/docs/adr/0006-daemon-rpc-protocol-version.md#L141) could add 'except a client's own failed launch (3 s) and a takeover capped by the startup ceiling'. The unbudgeted shutdown steps are not measured, so the 75 s bound is a budget, not a measurement.

The inline thread on the hand-summed 19_000 budget still applies (#4806 (comment), same as note 3), so please keep it open. The cli-help.ts and commands.md thread is fixed at this head (#4806 (comment)). Two threads do not apply, so please resolve them: a daemon that accepts a quiesce holds no session, so the 60 s wait covers its teardown (#4806 (comment)), and a closing daemon counts as starting up, so the takeover path is not reached for it (#4806 (comment)).

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 20, 2026
…down budget, name the exceptions

Review follow-up on the teardown grace.

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

Copy link
Copy Markdown
Member Author

Review notes addressed in 153b871.

  1. Stop grace at its call: daemon-stop.ts passes params.graceTimeoutMs ?? DAEMON_TEARDOWN_GRACE_MS, and daemon-stop.test.ts asserts 75_000. The constant is read through a function-scoped import, because a static import grew the module's eager closure and the closure gate refused it.
  2. Replay cleanup: named in the PR body (3 s to 75 s).
  3. Budget guard: the test now lists every awaited budget by name, including the 4 s app-log stop: 23 s fixed plus 46 s session, 69 s against the 75 s grace. The constant's comment and the PR body say the same, and that unbudgeted steps are not measured. This answers the thread on the hand-summed value (fix(daemon): give a closing daemon its full teardown before anything kills it #4806 (comment)); the step budgets stay private, so they are still listed, not imported.
  4. ENSURE_ATTEMPTS: kept as a count, with a comment that says the budget is counted in waits. A wall-clock loop would spin for the whole grace in the tests that replace sleep, and the count already bounds the waits to the grace.
  5. ADR 0006: names the two shorter waits (a client's own failed launch, and a takeover capped by the startup ceiling).

The two threads that do not apply were already resolved.

@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: 1 new finding on 153b871. 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 97d3af5

Since the review of 153b871: 0 new · 0 still open · 1 no longer raised

No open findings.

Reviewed with this repository's own Sentinel configuration (0a1c2cce7b9b).

💬 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
thymikee merged commit 22d36bc into main Nov 20, 2026
19 checks passed
@thymikee
thymikee deleted the t3/daemon-teardown-grace branch November 20, 2026 13:26
thymikee added a commit that referenced this pull request Nov 20, 2026
* origin/main:
  refactor(apple-runner): generate the Swift runner error-code enums from runner-contract.ts (#4810)
  fix(daemon-client): report a newer daemon that won a startup race as daemon-incompatible (#4801)
  feat(shake): add a shake command for iOS simulators and Android emulators (#4741)
  fix(daemon): give a closing daemon its full teardown before anything kills it (#4806)
  fix(android): use typed evidence in the readiness boot poller (#4717)
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