Skip to content

fix: finish a cancelled stdio send that has written part of its message - #407

Merged
jeanmatthieu merged 2 commits into
modelcontextprotocol:rel/1.0.0from
canarik:oss/stdio-cancelled-send-torn-frame
Nov 30, 2026
Merged

jeanmatthieu merged 2 commits into
modelcontextprotocol:rel/1.0.0from
canarik:oss/stdio-cancelled-send-torn-frame

Conversation

@canarik

@canarik canarik commented Nov 29, 2026

Copy link
Copy Markdown

Motivation and Context

StdioTransport.send(_:) waits with Task.sleep when the pipe is full. If the sending task
was cancelled during that wait, the sleep threw and the send returned after writing part of
its message and no newline. The next queued send then wrote its message straight after those
bytes, so the peer read one line that is not valid JSON and lost both messages, and a request
answered by the second one never completed. This happens when a peer cancels a request while
its handler is writing a large notification, or, on the client, a large result for a server's
sampling, elicitation or roots request.

  • A send that has written part of its message now finishes it even if its task is cancelled.
    It waits between retries with a sleep that does not end on cancellation. disconnect()
    still ends such a send, and a send that has written nothing still throws CancellationError.
  • A waiting send that is handed the output just as it is cancelled now passes the output on
    unwritten and throws CancellationError, as the send(_:) documentation says.

How Has This Been Tested?

Extends StdioTransportTests with testSendCancelledMidFrame (cancel a 512 KiB send while it
waits for room, with a small send queued behind it, and check that both arrive whole on
separate lines), testSendCancelledBeforeWriting and testDisconnectEndsCancelledSendMidFrame.
Without the fix the first and the third fail; the second covers the unwritten case, which
already worked. The new tests passed 25 runs in a row, and the stdio tests pass under the
thread sanitizer. swift test passes, and the documentation builds with
--warnings-as-errors. The race where a waiting send is handed the output just as it is
cancelled has no deterministic test, so that change rests on review.

Breaking Changes

None in the API. A send that is cancelled after it has written part of its message now runs
until the message is written or the transport disconnects, instead of returning at once.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

AI Assistance

Drafted with an AI coding agent, then reviewed by me.

`StdioTransport.send(_:)` writes a message in a loop and, when the pipe
is full, waits 10 ms with `Task.sleep` before it tries again. If the
sending task was cancelled during that wait, `Task.sleep` threw
`CancellationError` and the send returned after writing only part of
the message and no newline. `releaseOutput()` then handed the output to
the next queued send, which wrote its message straight after the
partial bytes. The peer read one line that is not valid JSON and lost
both messages, and a request answered by the second one never
completed. This happens when a peer cancels a request whose handler is
writing a large notification or result.
Fix: once a send has written part of its message, wait between retries
with a sleep that does not end on cancellation, so the send finishes
the line. A send that has written nothing still throws
`CancellationError` and passes the output on, and `disconnect()` still
ends a send that is part-way through a message.

A waiting send is handed the output by `releaseOutput()`, while its
cancellation reaches `cancelSendWaiter(_:)` through a separate task. If
the output was handed over first, the cancelled send wrote its message
anyway, although `send(_:)` documents that it throws.
Fix: `acquireOutput()` checks for cancellation after it is handed the
output, and passes the output on unwritten.

Adds three tests to `StdioTransportTests`: a send cancelled part-way
through a message, a send cancelled before it writes anything, and a
disconnect that ends a cancelled send part-way through a message.

Introduced in 1e61f20 ("Add support for Linux with glibc (modelcontextprotocol#61)").
canarik pushed a commit to canarik/swift-sdk that referenced this pull request Nov 30, 2026
Two tests in ClientReceiveLoopTerminationTests failed on the Ubuntu 24.04
job of modelcontextprotocol#407, a change to StdioTransport only. The 2-second limit of
outcome(within:) ran out in both. Neither found a wrong result. The tests
that ran alongside took 4.4 to 5.0 seconds each, so the overloaded runner
did not get to the client's work within the limit. The reconnect test
also failed the same way on an unrelated branch's iOS job.

Every wait in the suite for something that must finish now uses one
10-second limit. A passing test returns as soon as the operation does, so
the limit only adds time when a test fails, and the suite's one-minute
time limit still applies. The 200 ms check that a request does not fail
early keeps its short limit.

Assisted-by: Claude
@jeanmatthieu
jeanmatthieu merged commit 7e2dabc into modelcontextprotocol:rel/1.0.0 Nov 30, 2026
11 of 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.

2 participants