diff --git a/pkgs/mcpx/internal/cli/connect.go b/pkgs/mcpx/internal/cli/connect.go index c91dbbb036..be3e43fe65 100644 --- a/pkgs/mcpx/internal/cli/connect.go +++ b/pkgs/mcpx/internal/cli/connect.go @@ -209,6 +209,13 @@ func siblingSockets(paths daemon.Paths) []string { // started, this is the difference between mcpx working slowly and not // working. func (a *App) inlineDaemon(ctx context.Context) (*Client, error) { + // A cancelled caller must not be read as "no daemon answered": Ping is + // ctx-bound, so it could never succeed, and the poll below would burn + // the whole inline start timeout before reporting the wrong error. + // EnsureDaemon learned this in #233; this rung gets the same checks. + if err := ctx.Err(); err != nil { + return nil, err + } cfg, err := config.Load(a.ConfigPath) if err != nil { return nil, fmt.Errorf("inline mode needs a configuration: %w", err) @@ -254,7 +261,16 @@ func (a *App) inlineDaemon(ctx context.Context) (*Client, error) { if c.Ping(ctx) { return c, nil } - time.Sleep(a.Settings().Duration("daemon.inlineStartPoll")) + select { + case <-ctx.Done(): + a.inlineStop() + return nil, ctx.Err() + case <-time.After(a.Settings().Duration("daemon.inlineStartPoll")): + } + } + if err := ctx.Err(); err != nil { + a.inlineStop() + return nil, err } a.inlineStop() return nil, fmt.Errorf("the in-process daemon did not answer") diff --git a/pkgs/mcpx/internal/cli/inlinedaemon_test.go b/pkgs/mcpx/internal/cli/inlinedaemon_test.go new file mode 100644 index 0000000000..90eafecd1c --- /dev/null +++ b/pkgs/mcpx/internal/cli/inlinedaemon_test.go @@ -0,0 +1,64 @@ +package cli + +import ( + "context" + "errors" + "os" + "path/filepath" + "testing" + "time" + + "github.com/dezren39/mcpx/internal/daemon" + "github.com/dezren39/mcpx/internal/testsupport" +) + +// inlineDaemonApp builds an App whose configuration declares no servers, +// so its in-process daemon has nothing to spawn and should answer at once. +func inlineDaemonApp(t *testing.T) *App { + t.Helper() + root := testsupport.TempDir(t) + t.Setenv("HOME", root) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(root, "xdg")) + cfg := filepath.Join(root, "mcpx.json") + if err := os.WriteFile(cfg, []byte(`{"mcpServers":{}}`), 0o600); err != nil { + t.Fatal(err) + } + return &App{ConfigPath: cfg, Paths: daemon.PathsAt(root, "")} +} + +// A context that is already cancelled must not start an in-process daemon +// and then wait out the full inline start timeout (5s by default): Ping is +// ctx-bound, so it could never succeed, and the old code reported the +// generic "did not answer" error instead of the cancellation (#430, the +// inline counterpart of the #233 EnsureDaemon fix). +func TestInlineDaemonAlreadyCancelledContext(t *testing.T) { + a := inlineDaemonApp(t) + ctx, cancel := context.WithCancel(context.Background()) + cancel() + start := time.Now() + _, err := a.inlineDaemon(ctx) + if !errors.Is(err, context.Canceled) { + t.Fatalf("inlineDaemon(cancelled ctx) = %v, want context.Canceled", err) + } + if elapsed := time.Since(start); elapsed > 2*time.Second { + t.Fatalf("inlineDaemon(cancelled ctx) took %v, want an immediate return", elapsed) + } + if a.inlineStop != nil { + t.Fatal("inlineDaemon started an in-process daemon despite the cancelled context") + } +} + +// The uncancelled path is unchanged: the in-process daemon starts and the +// returned client can ping it. +func TestInlineDaemonStartsWithLiveContext(t *testing.T) { + a := inlineDaemonApp(t) + ctx := context.Background() + c, err := a.inlineDaemon(ctx) + if err != nil { + t.Fatalf("inlineDaemon(live ctx): %v", err) + } + defer a.CloseInline() + if !c.Ping(ctx) { + t.Fatal("the client returned by inlineDaemon does not ping its daemon") + } +}