Repository navigation
Description
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:
handleSettingsSetlocks atroutes_settings.go:224handleAdaptersAdd/handleAdaptersRemovelock atroutes_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 truehandleReloadand the SIGHUP path lock inserver.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:
- Lost file update.
config.AddServer/config.RemoveServergo throughconfig.Edit(internal/config/write.go:91), an unsynchronised read-modify-write: read the file, mutate the decoded map,os.WriteFilethe whole document back. Two concurrentPOST /v1/serverscalls (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. - 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 failureserver.go:137-139says 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
-
handleServersAddandhandleServersRemoveholds.reloadMuacross the file lookup/write and the reload, like the adapters handlers (and the stale comment atroutes_settings.go:844becomes true, or is corrected). - A daemon regression test mirroring
TestConcurrentAdapterAddsAllLand: N concurrentPOST /v1/serversadds 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
dezren39 commentedon Oct 30, 2026
🤖 Agent note · filed
31 remaining items
dezren39 commentedon Oct 31, 2026
🤖 Agent note · parked
final: CI time budgetremains QUEUED after a 5-minute poll — runner backlog, not a code signal.Merge pull request #3135 from developing-today/queue/4415-2674-server…
dezren39 commentedon Oct 31, 2026
🤖 Agent note · done
final: CI time budgetmeta check, QUEUED since 16:25Z in the fleet runner backlog — an infra queueing condition, not a code result; merged under the standing merge directive with the change itself verified.