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
9 changes: 8 additions & 1 deletion pkgs/mcpx/internal/cli/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ func siblingSockets(paths daemon.Paths) []string {
// pool -- and neither is required to answer a question. Where one cannot be
// started, this is the difference between mcpx working slowly and not
// working.
// inlineDaemonPing is Client.Ping, indirected so tests can keep the
// in-process daemon from ever answering and so drive inlineDaemon's poll
// loop deterministically: a daemon with an empty configuration answers
// the first Ping, so the wait below is otherwise never entered (#323).
// Production code never reassigns it.
var inlineDaemonPing = (*Client).Ping

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
Expand Down Expand Up @@ -258,7 +265,7 @@ func (a *App) inlineDaemon(ctx context.Context) (*Client, error) {
c := NewClient(paths, a.ConfigPath)
deadline := time.Now().Add(a.Settings().Duration("daemon.inlineStartTimeout"))
for time.Now().Before(deadline) {
if c.Ping(ctx) {
if inlineDaemonPing(c, ctx) {
return c, nil
}
select {
Expand Down
120 changes: 120 additions & 0 deletions pkgs/mcpx/internal/cli/inlinedaemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -48,6 +49,125 @@ func TestInlineDaemonAlreadyCancelledContext(t *testing.T) {
}
}

// neverAnswers replaces inlineDaemon's Ping with one that never reports
// the daemon up, so inlineDaemon stays in its poll loop until the context
// or the deadline ends the wait. The empty-config daemon answers the
// first real Ping, which is why the #430 tests never entered the loop
// (#323).
func neverAnswers(t *testing.T) {
t.Helper()
orig := inlineDaemonPing
inlineDaemonPing = func(*Client, context.Context) bool { return false }
t.Cleanup(func() { inlineDaemonPing = orig })
}

// The deadline-exit test below reuses errAfterContext from
// ensuredaemon_test.go: it reports context.Canceled from Err once its
// moment passes, but its Done channel never fires — a cancellation only
// a post-loop Err check can catch (the #328 shape, here for #323).

// inlineTempDirs lists the mcpx-inline-* directories currently in the
// temp dir, so a test can tell whether inlineDaemon removed the private
// directory of the daemon it started (its stop path) or leaked it.
func inlineTempDirs(t *testing.T) map[string]bool {
t.Helper()
matches, err := filepath.Glob(filepath.Join(os.TempDir(), "mcpx-inline-*"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Track only the temp directory created by this test

This scans the process-wide temporary directory rather than identifying the directory created by this invocation. If another mcpx process or test binary starts an inline daemon between the before/after snapshots, the subsequent loops classify its directory as a leak, delete it with os.RemoveAll, and fail the test even though this daemon cleaned up correctly. Capture the tested daemon's exact directory or inject a test-specific temp parent instead of scanning and deleting every new mcpx-inline-* match.

Useful? React with 👍 / 👎.

if err != nil {
t.Fatal(err)
}
out := make(map[string]bool, len(matches))
for _, m := range matches {
out[m] = true
}
return out
}

// Regression for #323: cancelling while inlineDaemon is still polling for
// its in-process daemon to answer must return context.Canceled promptly
// and stop the daemon, not burn the whole inline start timeout (5s by
// default) and report "the in-process daemon did not answer". Before this
// test, replacing the poll's select on ctx.Done() with a plain sleep left
// the suite green — neither #430 test ever entered the poll wait.
func TestInlineDaemonCancelledWhileWaiting(t *testing.T) {
neverAnswers(t)
a := inlineDaemonApp(t)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Pin the cancellation test's inline start timeout

This test inherits MCPX_DAEMON_INLINE_START_TIMEOUT through a.Settings() instead of fixing the timeout it relies on. If a developer runs the suite with that setting below 200 ms, the daemon times out before cancel() and the test fails; if it is between 200 ms and the 2-second assertion limit, replacing the cancellation-aware select with a sleep can still pass because the post-loop ctx.Err() returns context.Canceled quickly enough. Set a runtime timeout longer than the assertion threshold so the test reliably exercises and distinguishes cancellation during the poll.

Useful? React with 👍 / 👎.

before := inlineTempDirs(t)
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(200 * time.Millisecond)
cancel()
}()
start := time.Now()
c, err := a.inlineDaemon(ctx)
if c != nil {
t.Fatalf("inlineDaemon(cancelled while waiting) returned a client %v, want none", c)
}
if !errors.Is(err, context.Canceled) {
t.Fatalf("inlineDaemon(cancelled while waiting) = %v, want context.Canceled", err)
}
if elapsed := time.Since(start); elapsed > 2*time.Second {
t.Fatalf("inlineDaemon took %v after cancellation, want a prompt return well under the 5s start timeout", elapsed)
}
for dir := range inlineTempDirs(t) {
if !before[dir] {
_ = os.RemoveAll(dir)
t.Fatalf("inlineDaemon left its in-process daemon's directory %s behind after cancellation", dir)
}
}
}

// Regression for #323, the poll loop's other exit: a cancellation that
// only becomes visible as the loop leaves on its own deadline — its Done
// channel never fires, so the in-loop select cannot catch it — must still
// report context.Canceled via the post-loop check, not the generic
// "did not answer" timeout error. Removing that check leaves this test
// red while the mid-poll test above stays green.
func TestInlineDaemonCancelVisibleOnlyAtDeadline(t *testing.T) {
neverAnswers(t)
a := inlineDaemonApp(t)
if err := a.Settings().SetRuntime("daemon.inlineStartTimeout", "800ms"); err != nil {
t.Fatal(err)
}
ctx := errAfterContext{Context: context.Background(), at: time.Now().Add(150 * time.Millisecond)}
c, err := a.inlineDaemon(ctx)
if c != nil {
t.Fatalf("inlineDaemon(cancel visible only at deadline) returned a client %v, want none", c)
}
if !errors.Is(err, context.Canceled) {
t.Fatalf("inlineDaemon(cancel visible only at deadline) = %v, want context.Canceled", err)
}
}

// The genuine timeout path, also untested before #323: with a live
// context and a daemon that never answers, inlineDaemon gives up at the
// inline start timeout with its "did not answer" error (and stops the
// daemon), rather than reporting a spurious cancellation.
func TestInlineDaemonNeverAnswersStillTimesOut(t *testing.T) {
neverAnswers(t)
a := inlineDaemonApp(t)
if err := a.Settings().SetRuntime("daemon.inlineStartTimeout", "800ms"); err != nil {
t.Fatal(err)
}
before := inlineTempDirs(t)
start := time.Now()
c, err := a.inlineDaemon(context.Background())
if c != nil {
t.Fatalf("inlineDaemon(never answers) returned a client %v, want none", c)
}
if err == nil || !strings.Contains(err.Error(), "the in-process daemon did not answer") {
t.Fatalf("inlineDaemon(never answers) = %v, want the did-not-answer timeout error", err)
}
if elapsed := time.Since(start); elapsed < 700*time.Millisecond {
t.Fatalf("inlineDaemon gave up after %v, before the 800ms start timeout", elapsed)
}
for dir := range inlineTempDirs(t) {
if !before[dir] {
_ = os.RemoveAll(dir)
t.Fatalf("inlineDaemon left its in-process daemon's directory %s behind after the timeout", dir)
}
}
}

// The uncancelled path is unchanged: the in-process daemon starts and the
// returned client can ping it.
func TestInlineDaemonStartsWithLiveContext(t *testing.T) {
Expand Down
Loading