Client.withBatch leaves per-request tasks pending when transport.send throws

Đang mở
#324 2 bình luận 1 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

@bbdu3 đang làm issue này rồi.

Từ ngày 9/11/2026.

  • #328 của @bbdu3 — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
swift
Lĩnh vực
api, backend

Hướng nghiên cứu

Start with Client.withBatch(), Batch.addRequest(), and the single request Client.send() failure path described in the issue; use the existing MockTransport to reproduce the send failure with two Ping requests. Trace how pending request registration and continuation resumption are coordinated, including exactly-once cleanup. Done when each affected task receives the send error, its pending entry is removed, and unrelated requests remain unaffected.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Client.withBatch() propagates a transport send error to its caller, but does not propagate that failure to the tasks previously returned by Batch.addRequest().

How to found

After successfully connecting a client:

  1. Configure the transport to throw from send() before delivering the batch, while keeping the receive stream open.
  2. Add two requests inside withBatch() and retain their returned task handles.
  3. Catch the error thrown by withBatch().
  4. Await the retained tasks without disconnecting or explicitly cancelling their requests.

The batch was not delivered, so no responses will arrive. However, the per-request tasks have no send-failure path that completes them.

This can be exercised using the existing MockTransport: complete initialization successfully, call setFailSend(true), then submit a batch of Ping requests.

Expected behavior

When sending the batch fails, its outstanding per-request tasks should complete with the send error, and their pending request entries should be removed.

Current code path

Batch.addRequest() returns an independent task that waits on a CheckedContinuation. Registration in pendingRequests happens through another asynchronous task:

Task {
    await client.addPendingRequest(
        id: request.id,
        continuation: continuation,
        type: M.Result.self
    )
}

withBatch() subsequently sends the collected requests:

let data = try encoder.encode(requests)
try await connection.send(data)

There is no failure handler here to remove those pending requests or resume their continuations. Throwing from withBatch() does not automatically complete the separately created tasks.

By comparison, the single-request Client.send() path explicitly removes its pending request and resumes its continuation when sending fails.

Impact

Code awaiting the retained batch task results can remain suspended after the outer batch call has already failed. Registered pending requests also remain retained until another path handles them, such as explicit request cancellation or client disconnection.

This report concerns local transport send failure with no response received. It does not concern a successfully delivered batch containing JSON-RPC error responses, which already has response-handling logic.

my suggestion

I think withBatch() should propagate a send failure to all still-pending requests belonging to that batch, without affecting unrelated requests.

The fix also needs to account for asynchronous registration in Batch.addRequest(): simply removing currently registered IDs in a catch block may miss a continuation registered after cleanup. Registration and failure handling should be coordinated, while preserving exactly-once continuation resumption.

Related work

  • #113 provides relevant exactly-once cleanup behavior for individual requests and response handling, but does not add batch send-failure cleanup.
  • #210 and #213 change withBatch() to return the closure’s result; their diffs leave the transport send-failure path without cleanup.
  • #314 adds rejection of batching for its modern protocol mode, rather than repairing failure handling in the existing batching path.

I did not find an existing issue or PR specifically addressing this failure path in the searches performed. If this issue and repair directions are correct, I am willing to open a PR.

Ngôn ngữ chính
Swift
Star
1.6k
Fork
270
Merge trung bình
2 ngày 7 giờ
Pull request đã merge (30 ngày)
15

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của modelcontextprotocol/swift-sdk

Tất cả issue của modelcontextprotocol/swift-sdk

Issue tương tự

Thêm issue về Swift

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.