Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion pkgs/mcpx/internal/cli/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
64 changes: 64 additions & 0 deletions pkgs/mcpx/internal/cli/inlinedaemon_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading