Skip to content

A stopping daemon closes new connections, so commands get DAEMON_CONNECTION_LOST, not the documented DAEMON_STOPPING #1301

Closed

Description

@V3RON

src/daemon/server.ts:740-743 (#accept) closes every new connection once #stopping is set. So during the stop window:

  • A command from a new connection fails with DAEMON_CONNECTION_LOST ("Daemon connection closed"). The docs say it fails with DAEMON_STOPPING: website/src/content/docs/docs/worker/running-the-daemon.mdx:119 and :129, website/src/content/docs/docs/worker/configuration.mdx:49.
  • A second simlock daemon stop fails the same way. The comment at src/daemon/server.ts:847-851 says daemon.stop "stays reachable ... while the daemon is already #stopping", but that holds only for connections opened before the stop.

Evidence: the #1295 reproduction (e2e/daemon-lifecycle.test.ts, branch bug/1295-repro). Its teardown's daemon stop, sent while the old daemon still shuts down an idle device, failed:

Error: daemon stop failed before teardown of .../daemon.sock: {"error":{"code":"DAEMON_CONNECTION_LOST","message":"Daemon connection closed"}}

Found while triaging #1295.

Written by an agent.

Activity

  1. added
    bug:new
    on Nov 5, 2026
  2. self-assigned this
    on Nov 5, 2026
  3. V3RON commented on Nov 5, 2026

    @V3RON
    ContributorAuthor

    Reproduction

    Confirmed. src/daemon/server.test.ts › "answers a connection opened while the daemon is stopping: status.get with DAEMON_STOPPING, daemon.stop with stopping true", on bug/1301-repro (24a62e7). It holds settle open after stop(), opens a new admin connection, and sends hello, status.get, daemon.stop.

    pnpm exec vitest run --project unit src/daemon/server.test.ts -t "opened while the daemon is stopping"
    
    -   { "ok": true },
    -   { "error": { "code": "DAEMON_STOPPING" }, "ok": false },
    -   { "ok": true, "payload": { "stopping": true } },
    +   "connection closed",
    +   "connection closed",
    +   "connection closed",
     ❯ src/daemon/server.test.ts:2796:23
    

    Root cause

    Defect. src/daemon/server.ts:740 (#accept) closes every new socket once #stopping is set, so no frame on it reaches #dispatchLine. The DAEMON_STOPPING gate (:880) and the daemon.stop exception (:852) only ever see connections opened before the stop. The endpoint keeps listening until host.stop() at :666, so clients connect and then lose the connection. No ADR asks for this; the docs and the comment at :847-851 describe the dispatch gates.

    Simplest fix

    Gate #accept on a flag set when #stop reaches #closeConnections() (:660), not on #stopping. Connections opened during the drain then get DAEMON_STOPPING or the daemon.stop reply. Ones that arrive after the close are still closed at once, so none is left open after host.stop(). With the gate removed, the whole file passes (138 tests).

    Alternatives rejected

    • Drop the #accept gate entirely: a socket accepted between #closeConnections() and host.stop() is never closed.
    • Change the docs to DAEMON_CONNECTION_LOST: a second daemon stop or a daemon start retry cannot tell a stopping daemon from a crashed one.
    • Map DAEMON_CONNECTION_LOST to DAEMON_STOPPING in the client: the client cannot know why the socket closed.

    Risk

    • #releaseFailedStart (:689) also closes connections: set the same flag there.
    • The uplink goes through #accept too (acceptUplink): a gateway that reconnects during the drain now gets a session that refuses everything with DAEMON_STOPPING, and the close ends it.
    • src/cli/environment.ts:26-30 already treats both codes as a failed start; check that daemon start against a stopping daemon prints the documented DAEMON_STOPPING.

    Written by an agent.

  4. removed their assignment
    on Nov 5, 2026
  5. added
    bug:ready
    and removed
    bug:new
    on Nov 5, 2026
  6. self-assigned this
    on Nov 5, 2026
  7. added a commit that references this issue on Nov 5, 2026
    bc9201e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug:ready

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions