Skip to content

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

Open
canarik wants to merge 1 commit into
modelcontextprotocol:rel/1.0.0from
canarik:oss/stdio-cancelled-send-torn-frame
Open

canarik wants to merge 1 commit 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
canarik requested a review from a team as a code owner November 29, 2026 22:40
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.

1 participant