From 5a8639b3bb16b1a4a31a99cb204a8b21812651bc Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Mon, 19 Oct 2026 17:00:14 +0000 Subject: [PATCH] mcpx tests: assert the spawn happened in TestEnsureDaemonCancelledWhileWaiting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The dezren39/nix copy of this test (TestEnsureDaemonCancelWhilePolling) has always asserted, after the prompt context.Canceled return, that the daemon spawn actually happened — waiting up to 5s for the fake daemon's marker, since cmd.Start returns before the child runs and the child is Setsid + Released. This copy discarded the marker, so it would pass even if EnsureDaemon returned context.Canceled without spawning at all. Ported as part of the mcpx dedup (code becomes the single source; the nix copy is deleted and pinned to this repo). Ablation: with cmd.Start stubbed out, the test now fails with 'expected the daemon spawn to have happened before cancellation'; unmodified code passes, as does the full internal/cli suite. Closes #361 --- pkgs/mcpx/internal/cli/ensuredaemon_test.go | 27 ++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/pkgs/mcpx/internal/cli/ensuredaemon_test.go b/pkgs/mcpx/internal/cli/ensuredaemon_test.go index 1ceaf5d78d..1d6d9bea3f 100644 --- a/pkgs/mcpx/internal/cli/ensuredaemon_test.go +++ b/pkgs/mcpx/internal/cli/ensuredaemon_test.go @@ -60,7 +60,7 @@ func TestEnsureDaemonAlreadyCancelledContext(t *testing.T) { // Regression for #284: cancelling while EnsureDaemon waits for the // started daemon must end the wait promptly, not at the deadline. func TestEnsureDaemonCancelledWhileWaiting(t *testing.T) { - c, _ := ensureTestClient(t, "30s") + c, marker := ensureTestClient(t, "30s") ctx, cancel := context.WithCancel(context.Background()) go func() { time.Sleep(200 * time.Millisecond) @@ -74,6 +74,31 @@ func TestEnsureDaemonCancelledWhileWaiting(t *testing.T) { if elapsed := time.Since(start); elapsed > 5*time.Second { t.Fatalf("took %v after cancellation, want a prompt return", elapsed) } + // The prompt return above is only the right behaviour if the daemon + // was actually started and the cancellation unwound the wait for it: + // a context.Canceled return with no spawn at all (a cancellation + // check ahead of the spawn, say) would pass both assertions while + // never starting anything. The spawn is cmd.Start + Release + // (client.go): Start returns once the process exists, before the + // child has run any code, and this test's whole point is that + // EnsureDaemon returns the instant cancellation lands — so the + // child's touch can legitimately land after the return. Asserting + // the marker instantly races the child's scheduling (the flake that + // got the dezren39/nix copy of this test skipped in its darwin nix + // build, dezren39/nix#428). The child is Setsid and Released, so it + // still runs: wait for the marker instead, and fail only if it never + // appears. Ported from the dezren39/nix copy in #361; its + // TestEnsureDaemonCancelWhilePolling has asserted this all along. + deadline := time.Now().Add(5 * time.Second) + for { + if _, statErr := os.Stat(marker); statErr == nil { + break + } + if time.Now().After(deadline) { + t.Fatal("expected the daemon spawn to have happened before cancellation") + } + time.Sleep(10 * time.Millisecond) + } } // The uncancelled path is unchanged: a daemon that never becomes ready