Repository navigation
Description
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:
- Configure the transport to throw from
send()before delivering the batch, while keeping the receive stream open. - Add two requests inside
withBatch()and retain their returned task handles. - Catch the error thrown by
withBatch(). - 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
- Prevent double continuation resumption in client #113 provides relevant exactly-once cleanup behavior for individual requests and response handling, but does not add batch send-failure cleanup.
- fix atomic continuation guards #210 and fix: resolve CheckedContinuation deadlock in NetworkTransport on actor dealloc (Swift 6.3) #213 change
withBatch()to return the closure’s result; their diffs leave the transport send-failure path without cleanup. - feat: dual-era MCP 2026-07-28 modern protocol profile #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.
Activity
imabuddha commentedon Nov 6, 2026
You provided more than enough information for me to reproduce the problem, and generate a fix. You hit the nail on the head. The crux of the issue is the asynchronous registration.
I have a fix that coordinates the two on the Client actor. A request that registers after its batch has failed is resumed with the error as it registers. Each continuation is resumed exactly once. It also covers a body closure that throws after adding requests. Tests for both, and the full suite passes.
Are you still planning to open a PR? If so, I'm happy to leave it to you and share what I found. If not, I can open mine.
(I worked on this with Claude Opus 5.5)