Skip to content

mcpx: EnsureDaemon ignores a cancelled context — spawns a daemon and waits out the full 20s autostart timeout #284

Closed

Description

@dezren39

Problem

Client.EnsureDaemon (pkgs/mcpx/internal/cli/client.go:327) ignores a cancelled context:

  1. It never checks ctx.Err(). A failed Ping is always read as "no daemon", even when the ping failed because the caller's context was already cancelled.
  2. It then starts a daemon process the caller no longer wants.
  3. The readiness poll loop (client.go:381-387) never selects on ctx.Done() — it sleeps autostart.pollInterval and re-pings with the cancelled context (which always fails), so it waits out the full autostart.connectTimeout (20s by default, pkgs/mcpx/internal/defaults/defaults.json:181) and finally returns the misleading error daemon did not become ready, not context.Canceled.

So a call cancelled early (e.g. a notifications/cancelled over mcpx serve, or a user hitting Ctrl-C during autostart) takes ~20s to unwind and can leave an unwanted daemon running.

(The same defect is tracked for the dezren39/nix copy of mcpx as dezren39/darwin-nix#233; this repo's client.go is byte-identical except for an issue-reference comment, and the defect is untracked here.)

Evidence (reproduced on main 228019a1)

A test in internal/cli constructs a client with autostart.bin set to a fake binary that only records that it was spawned, autostart.connectTimeout=3s, and calls EnsureDaemon with an already-cancelled context:

elapsed=3.026s err=daemon did not become ready; see .../state/daemon.log
spawnedFakeDaemon=true

Expected: prompt return of context.Canceled, no daemon spawned. Observed: full timeout waited, daemon spawned, wrong error.

Suggested fix

  • Check ctx.Err() at the top of EnsureDaemon and again after a failed Ping, before treating the failure as "no daemon" / starting one.
  • In the poll loop, stop on ctx.Done() (select on it alongside the poll sleep) and return ctx.Err().

Acceptance criteria

  • EnsureDaemon with an already-cancelled context returns context.Canceled promptly (<1s) and does not start a daemon.
  • Cancelling the context while EnsureDaemon is waiting for a started daemon to become ready returns context.Canceled promptly instead of waiting out autostart.connectTimeout.
  • Normal behaviour is unchanged: a reachable daemon returns nil, an unreachable one is still auto-started and awaited, and a daemon that never becomes ready still fails with daemon did not become ready after the timeout.
  • Regression tests cover both cancellation cases; go test ./internal/cli/ passes.

Activity

  1. added a commit that references this issue on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions