Skip to content
Closed
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
19 changes: 16 additions & 3 deletions pkgs/mcpx/internal/testsupport/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,26 @@ func NewReplayTransport(trace *mcpclient.CallTrace) *ReplayTransport {
return r
}

// Send matches msg against the recording's next client frame.
func (r *ReplayTransport) Send(_ context.Context, msg []byte) error {
// Send matches msg against the recording's next client frame. The
// wait honours ctx: when the caller's deadline ends, the wait wakes
// and Send returns the context error instead of hanging past it
// (#17227 — a stalled interleave previously held a call to the full
// deadline with no cancellation path).
func (r *ReplayTransport) Send(ctx context.Context, msg []byte) error {
stop := context.AfterFunc(ctx, func() {
r.mu.Lock()
defer r.mu.Unlock()
r.cond.Broadcast()
})
defer stop()
r.mu.Lock()
defer r.mu.Unlock()
for r.err == nil && !r.closed && r.next < len(r.frames) && r.frames[r.next].Dir == mcpclient.TraceRecv {
for r.err == nil && !r.closed && ctx.Err() == nil && r.next < len(r.frames) && r.frames[r.next].Dir == mcpclient.TraceRecv {
r.cond.Wait()
}
if r.err == nil && !r.closed && ctx.Err() != nil {
return ctx.Err()
}
if r.err != nil {
return r.err
}
Expand Down
22 changes: 22 additions & 0 deletions pkgs/mcpx/internal/testsupport/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,25 @@ func TestReplayTruncatedTraceFailsTheCall(t *testing.T) {
}
_ = c.Close()
}

// TestReplaySendHonoursContextDeadline pins #17227's structural fix:
// a Send blocked behind an unconsumed server frame must return the
// caller's context error when the deadline ends, not hang past it —
// Send previously ignored its context entirely.
func TestReplaySendHonoursContextDeadline(t *testing.T) {
trace := &mcpclient.CallTrace{Frames: []mcpclient.CallFrame{
{Dir: mcpclient.TraceRecv, Msg: json.RawMessage(`{"jsonrpc":"2.0","id":1,"result":{}}`)},
{Dir: mcpclient.TraceSend, Msg: json.RawMessage(`{"jsonrpc":"2.0","id":2,"method":"tools/list"}`)},
}}
rt := NewReplayTransport(trace)
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
start := time.Now()
err := rt.Send(ctx, []byte(`{"jsonrpc":"2.0","id":2,"method":"tools/list"}`))
if err == nil || !strings.Contains(err.Error(), "context deadline exceeded") {
t.Fatalf("Send err = %v, want the context deadline", err)
}
if elapsed := time.Since(start); elapsed > 5*time.Second {
t.Fatalf("Send returned after %v, want it near the 200ms deadline", elapsed)
}
}