Skip to content

test(client): add batch send-failure regression test - #386

Merged
jeanmatthieu merged 2 commits into
modelcontextprotocol:rel/1.0.0from
Bugraciftci:fix/batch-send-failure-cleanup
Nov 27, 2026
Merged

jeanmatthieu merged 2 commits into
modelcontextprotocol:rel/1.0.0from
Bugraciftci:fix/batch-send-failure-cleanup

Conversation

@Bugraciftci

@Bugraciftci Bugraciftci commented Nov 27, 2026 •

Copy link
Copy Markdown

Rebases #386 onto rel/1.0.0, drops the pre-refactor Client.swift changes, and adds the missing regression test for #324 next to throwingBody() in Tests/MCPTests/RequestLifecycleTests.swift.

Summary

  • Adds BatchTests.sendFailure() connecting a .handshake Client over MockTransport, enabling setFailSend(true), adding two Ping.request() calls inside client.withBatch, and asserting both withBatch and the two returned request Tasks fail with MCPError.
  • Relies on the Batches suite .timeLimit(.minutes(1)) so any regression hang is caught directly without artificial sleeps.

Verification

  • swift test --filter BatchTests: 4 tests in 1 suite passed.
  • swift test: 1,420 tests in 144 suites passed (0 failures).

Fixes #324.

@Bugraciftci
Bugraciftci requested a review from a team as a code owner November 27, 2026 10:44
@jeanmatthieu

Copy link
Copy Markdown
Contributor

Thanks for taking a look at this, @Bugraciftci, and for the clear root-cause write-up and tests.

This is addressed on the rel/1.0.0 branch, where the batch path was reworked as part of the request lifecycle changes:

  • Registration race: Batch.addRequest now calls registerBatchRequest, which goes through register(_:) on the client actor. The request is encoded and its response slot is in pendingRequests before addRequest returns, so there is no unstructured registration task left to race with. An encode failure throws from addRequest before anything is registered.
  • body throws: withBatch fails the requests added so far through abandonBatch and rethrows (withBatch). Covered by throwingBody().
  • Send fails (Client.withBatch leaves per-request tasks pending when transport.send throws #324): sendBatch removes and fails every request it tried to send, then rethrows. On a stateless connection each request goes through transmit, which fails that request on its own.
  • Connection closed or replaced while the batch was built: the batch is abandoned with MCPError.connectionClosed and not sent.

One gap: rel/1.0.0 has no regression test for the exact #324 scenario (a batch whose send fails). If you'd like to keep going with this, please rebase onto rel/1.0.0, drop the Client.swift changes, and add a single test for that case, for example next to throwingBody() in Tests/MCPTests/RequestLifecycleTests.swift: connect a .handshake client over MockTransport, call setFailSend(true), add two Ping requests in withBatch, and expect both withBatch and the returned tasks to throw. The Batches suite's .timeLimit catches a hang, so no sleep or timeout task is needed.

If you'd rather not, no problem: once you've had a chance to review the 1.0.0 change, feel free to close this PR.

@Bugraciftci
Bugraciftci force-pushed the fix/batch-send-failure-cleanup branch from 8d0083a to 17db2f1 Compare November 27, 2026 11:31
@Bugraciftci Bugraciftci changed the title fix(client): clean up pending batch tasks when withBatch fails test(client): add batch send-failure regression test Nov 27, 2026
@Bugraciftci
Bugraciftci changed the base branch from main to rel/1.0.0 November 27, 2026 11:31
@Bugraciftci

Copy link
Copy Markdown
Author

Thanks for the pointer @jeanmatthieu! I've rebased the PR onto rel/1.0.0, dropped the Client.swift and ClientTests.swift changes, retargeted the base branch to rel/1.0.0, and added the single sendFailure() regression test next to throwingBody() in Tests/MCPTests/RequestLifecycleTests.swift. Verified locally: swift test --filter BatchTests (4/4 passed) and full swift test (1,420 tests across 144 suites passed).

… of hanging

A pending request does not respond to task cancellation, so the suite's
`.timeLimit` cannot end a test that awaits a request nobody fails: it
cancels the test's task, but `task.value` waits on a separate `Task`.
With the cleanup in `sendBatch` removed, `sendFailure()` hung the test
run. Await each request through `outcome(within:)` instead, so the
regression fails in seconds, and expect the send's `transportError`
rather than any `MCPError`, so an unrelated failure that never reaches
`sendBatch` does not pass.

Answer `initialize` with `#require` and decoding that throws, as
`ClientUndecodableResponseTests` does, so a broken handshake records an
issue instead of being skipped silently.

Written with AI assistance.
@jeanmatthieu

Copy link
Copy Markdown
Contributor

Thanks @Bugraciftci, the rebase and the new test are exactly what we needed!

I made a mistake in my earlier comment, though: the suite's .timeLimit does not catch a hang here. When the limit is reached, Swift Testing cancels the test's own task, but first.value waits on a separate Task returned by addRequest, and a pending request does not respond to cancellation. I checked by removing the loop in sendBatch that fails the requests: sendFailure() then hung the whole test run instead of failing. Sorry for sending you down that path.

I pushed a small follow-up commit to your branch:

  • Each returned task is awaited through outcome(within:) (Tests/MCPTests/Helpers/Deadline.swift), which the repo has for exactly this case. With the fix removed, the test now fails in about 10 seconds with "A request of a batch whose send failed never completed".
  • The test expects the send's MCPError.transportError rather than any MCPError, so a failure that never reaches sendBatch (for example connectionClosed) doesn't pass.
  • The initialize responder uses #require and a throwing decode instead of if let … try?, so a broken handshake records an issue instead of being skipped silently.

Verified locally on macOS: swift test --filter BatchTests passes with the fix and fails as expected without it, and the full swift test passes. I restarted CI on the new commit. The earlier failure on RHEL UBI 9 was a FoundationNetworking crash in the OAuth URLSession tests, unrelated to this change.

@jeanmatthieu
jeanmatthieu merged commit db583c0 into modelcontextprotocol:rel/1.0.0 Nov 27, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants