Skip to content

mcpx(daemon): POST/DELETE /v1/servers handlers never hold reloadMu — concurrent adds lose servers while reporting success #2674

Closed

Description

@dezren39

Problem

The daemon's live server add/remove handlers — handleServersAdd (pkgs/mcpx/internal/daemon/routes_settings.go:588) and handleServersRemove (routes_settings.go:623) — are the only reload-path handlers that do not hold s.reloadMu, the mutex whose documented job is exactly this: “reloadMu serialises reloads, so two requests arriving together do not each rebuild the pool map from the same starting point” (internal/daemon/server.go:137-139).

Every other caller follows the rule:

  • handleSettingsSet locks at routes_settings.go:224
  • handleAdaptersAdd / handleAdaptersRemove lock at routes_settings.go:845 / :935 — and the adapters comment even asserts “The servers handlers hold reloadMu the same way” (routes_settings.go:844), which is not true
  • handleReload and the SIGHUP path lock in server.go
  • Adapters also got a regression test for exactly this property: TestConcurrentAdapterAddsAllLand (internal/daemon/adapter_routes_test.go:88-116). Servers have no equivalent test.

That leaves two races in the servers handlers:

  1. Lost file update. config.AddServer / config.RemoveServer go through config.Edit (internal/config/write.go:91), an unsynchronised read-modify-write: read the file, mutate the decoded map, os.WriteFile the whole document back. Two concurrent POST /v1/servers calls (e.g. two CLI invocations, or a CLI racing a script) can both read the same starting document, each add a different server, and write back — the second write silently discards the first server. Both calls return 200.
  2. Racing reloads. Each handler then calls s.reloadConfig() (routes_settings.go:613, :654) unlocked, so the two reloads can interleave their pool rebuilds from different starting points — the precise failure server.go:137-139 says the mutex exists to prevent.

Evidence (verified this run, main cc52976)

Laptop probe (~/git/fleet/probe-2026-10-30-laneB-serverrace/ on the laptop) using the real internal/config/write.go from this main (md5 3d8605b0c3d4340fbee7a9d1d96cf702, byte-identical to the sandbox clone at cc52976; only its package-level StripJSONC/DirMode/FileMode dependencies are shimmed, and the probe inputs are plain JSON): two goroutines released simultaneously call config.AddServer for two different names against one config file seeded with 3,000 servers, 40 trials:

trial 0: both AddServer returned nil; alpha=false beta=true (one add silently lost)
RESULT lost_updates=40/40 trials (both calls reported success)

Both calls return nil in the lost trials — the add reports success for a server that is not in the file (and, because the handler's reload reads the file that survived, not in the running daemon either).

Code inspection this run confirms the missing lock is not compensated elsewhere: grep reloadMu over internal/daemon shows locks in the settings-set, adapters add/remove, watch, SIGHUP and reload handlers — and none in handleServersAdd/handleServersRemove; config.Edit itself takes no lock.

Why it matters

Live server add is a headline mcpx workflow (mcpx servers add, used by scripts and parallel sessions against one shared daemon). A concurrent add that silently vanishes — while reporting success — is the worst failure shape: the caller proceeds believing the server exists, and the missing server is only discovered later, downstream, with no error pointing here. The adapters path was hardened against exactly this in PR #2565 after review; the older servers path was left with the same hole and a comment claiming it was covered.

Acceptance criteria

  • handleServersAdd and handleServersRemove hold s.reloadMu across the file lookup/write and the reload, like the adapters handlers (and the stale comment at routes_settings.go:844 becomes true, or is corrected).
  • A daemon regression test mirroring TestConcurrentAdapterAddsAllLand: N concurrent POST /v1/servers adds all return 200 and all N servers are present in the written file afterwards.
  • A concurrent add/remove mix cannot resurrect a removed server or drop an unrelated one (covered by the same lock, asserted in the test or a sibling).
  • Existing daemon + config tests still pass in CI.

Activity

  1. added
    kind/bugSomething isn't working (prefixed form of the classic bug label)
    task/triageQueue packet kind: verify, reproduce, and route this item
    source/creatorFiled by the new-issue-creator job
    priority/normalOrdinary priority (the default)
    app/mcpxPackage/app: mcpx (pkgs/ or apps/ in this repo)
    effort/xsTrivial: expected under 30 minutes of work
    on Oct 30, 2026
  2. dezren39 commented on Oct 30, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · filed

    • Agent: new-issue-creator (lane B, new-issue-creator-code-b)
    • Session / ID: new-issue-creator-code-b run 2026-10-30 04:50 CDT
    • Found via: filed this run
    • Task: Triage the missing reloadMu in the daemon's live servers add/remove handlers (routes_settings.go): pin that lookup + file write + reload happen under the lock like the adapters handlers, then route a small_fix packet.
    • Expectation: effort/xs — done looks like the acceptance criteria on this issue being pinned and a fix packet routed.
    • Links: evidence in the issue body (file:line at main cc52976 + laptop probe ~/git/fleet/probe-2026-10-30-laneB-serverrace/, lost updates 40/40).
  3. added
    queue/claimedQueue worker lifecycle state
    work/assignedQueue worker lifecycle state
    and removed on Oct 31, 2026
  4. 31 remaining items

  5. dezren39 commented on Oct 31, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · parked

  6. added
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    status/parkedDeliberately paused with a recorded resume condition
    queue/claimedQueue worker lifecycle state
    and removed
    queue/claimedQueue worker lifecycle state
    status/parkedDeliberately paused with a recorded resume condition
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    on Oct 31, 2026
  7. added a commit that references this issue on Oct 31, 2026
  8. dezren39 commented on Oct 31, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

app/mcpxPackage/app: mcpx (pkgs/ or apps/ in this repo)effort/xsTrivial: expected under 30 minutes of workkind/bugSomething isn't working (prefixed form of the classic bug label)priority/normalOrdinary priority (the default)source/creatorFiled by the new-issue-creator jobtask/triageQueue packet kind: verify, reproduce, and route this item

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions