Repository navigation
Conversation
`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)").
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
StdioTransport.send(_:)waits withTask.sleepwhen the pipe is full. If the sending taskwas 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.
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.unwritten and throws
CancellationError, as thesend(_:)documentation says.How Has This Been Tested?
Extends
StdioTransportTestswithtestSendCancelledMidFrame(cancel a 512 KiB send while itwaits for room, with a small send queued behind it, and check that both arrive whole on
separate lines),
testSendCancelledBeforeWritingandtestDisconnectEndsCancelledSendMidFrame.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 testpasses, and the documentation builds with--warnings-as-errors. The race where a waiting send is handed the output just as it iscancelled 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
AI Assistance
Drafted with an AI coding agent, then reviewed by me.