From 3de5bb92ecd0aeecf14ac708819c890fef0bba3f Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Sun, 18 Oct 2026 16:47:25 -0500 Subject: [PATCH] fix(mcpx): stop --all builds per-daemon clients from the found daemon's paths stopAll used the caller's Paths (only the socket overridden) while CmdDaemons used the listing's paths for the same daemon records, and allDaemons built those listing paths as a partial Paths{State: dir} with Info/Cache empty. Listings now carry a complete PathsAt(dir) and stopAll builds its client from the listing, like CmdDaemons. New stopall_test.go: allDaemons carries complete paths for a daemon found only in the default state dir (fails pre-fix), and stop --all stops a fake daemon serving on that record's socket. Fixes developing-today/code#316 (same change lands in dezren39/nix, whose pkgs/mcpx is vendored from this repo). --- pkgs/mcpx/internal/cli/commands.go | 4 +- pkgs/mcpx/internal/cli/stopall_test.go | 114 +++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 pkgs/mcpx/internal/cli/stopall_test.go diff --git a/pkgs/mcpx/internal/cli/commands.go b/pkgs/mcpx/internal/cli/commands.go index 8c484a5639..158326e598 100644 --- a/pkgs/mcpx/internal/cli/commands.go +++ b/pkgs/mcpx/internal/cli/commands.go @@ -1551,7 +1551,7 @@ func (a *App) allDaemons() []daemonListing { var out []daemonListing seen := map[string]bool{} for _, dir := range daemon.DaemonSearchDirs(a.Paths.State) { - p := daemon.Paths{State: dir} + p := daemon.PathsAt(dir, "") p.PruneStale() for _, info := range p.ListDaemons() { if seen[info.Socket] { @@ -1576,7 +1576,7 @@ func (a *App) stopAll(ctx context.Context) error { stopped := 0 for _, listing := range listings { info := listing.info - p := a.Paths + p := listing.paths p.Socket = info.Socket c := NewClient(p, info.ConfigPath) if !c.Ping(ctx) { diff --git a/pkgs/mcpx/internal/cli/stopall_test.go b/pkgs/mcpx/internal/cli/stopall_test.go new file mode 100644 index 0000000000..c15266bada --- /dev/null +++ b/pkgs/mcpx/internal/cli/stopall_test.go @@ -0,0 +1,114 @@ +package cli + +import ( + "context" + "encoding/json" + "net" + "net/http" + "os" + "path/filepath" + "sync/atomic" + "testing" + "time" + + "github.com/dezren39/mcpx/internal/daemon" +) + +// writeDaemonRecord plants a daemon record for a live process (the test's +// own, so PruneStale keeps it) in the given state directory. +func writeDaemonRecord(t *testing.T, dir, socket, configPath string) { + t.Helper() + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + b, err := json.Marshal(daemon.Info{ + PID: os.Getpid(), Socket: socket, ConfigPath: configPath, + }) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "daemon.json"), b, 0o644); err != nil { + t.Fatal(err) + } +} + +// customStateApp returns an App whose own state directory is a scratch dir, +// with the default state directory redirected under a scratch XDG home, so +// the only daemon record on the machine is the one the test plants in the +// default directory. +func customStateApp(t *testing.T) (a *App, defaultDir string) { + t.Helper() + xdg := t.TempDir() + t.Setenv("XDG_STATE_HOME", xdg) + t.Setenv("XDG_CACHE_HOME", filepath.Join(xdg, "cache")) + t.Setenv("MCPX_STATE_DIR", "") + t.Setenv("MCPX_CACHE_DIR", "") + defaultDir = filepath.Join(xdg, "mcpx") + a = &App{Paths: daemon.PathsAt(filepath.Join(t.TempDir(), "custom"), "")} + return a, defaultDir +} + +// A daemon found in another state directory must be listed with a complete +// Paths rooted in that directory -- before the fix allDaemons built +// Paths{State: dir} with Info/Cache empty, and stopAll ignored even that +// and used the caller's paths (developing-today/code#316). +func TestAllDaemonsCarriesCompletePathsForFoundDir(t *testing.T) { + a, defaultDir := customStateApp(t) + writeDaemonRecord(t, defaultDir, filepath.Join(defaultDir, "daemon.sock"), "/cfg/a.json") + + listings := a.allDaemons() + if len(listings) != 1 { + t.Fatalf("allDaemons returned %d listings, want 1", len(listings)) + } + p := listings[0].paths + if p.State != defaultDir { + t.Errorf("listing paths.State = %q, want %q", p.State, defaultDir) + } + if want := filepath.Join(defaultDir, "daemon.json"); p.Info != want { + t.Errorf("listing paths.Info = %q, want %q", p.Info, want) + } + if p.Cache == "" { + t.Error("listing paths.Cache is empty, want a complete Paths") + } +} + +// stop --all must actually stop a daemon whose record lives only in the +// default state directory, not the caller's own. +func TestStopAllStopsDaemonFoundInDefaultDir(t *testing.T) { + a, defaultDir := customStateApp(t) + if err := os.MkdirAll(defaultDir, 0o755); err != nil { + t.Fatal(err) + } + socket := filepath.Join(defaultDir, "daemon.sock") + ln, err := net.Listen("unix", socket) + if err != nil { + t.Fatal(err) + } + var shutdowns atomic.Int32 + mux := http.NewServeMux() + mux.HandleFunc("/v1/health", func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte(`{"status":"ok"}`)) + }) + mux.HandleFunc("/v1/shutdown", func(w http.ResponseWriter, r *http.Request) { + shutdowns.Add(1) + _, _ = w.Write([]byte(`{}`)) + // Stop answering shortly after replying, like a daemon going down, + // so waitUntilStopped sees the socket refuse. + go func() { + time.Sleep(50 * time.Millisecond) + _ = ln.Close() + }() + }) + srv := &http.Server{Handler: mux} + go func() { _ = srv.Serve(ln) }() + defer srv.Close() + + writeDaemonRecord(t, defaultDir, socket, "/cfg/a.json") + + if err := a.stopAll(context.Background()); err != nil { + t.Fatalf("stopAll: %v", err) + } + if got := shutdowns.Load(); got != 1 { + t.Fatalf("fake daemon received %d shutdowns, want 1", got) + } +}