Repository navigation
Conversation
Since modelcontextprotocol#1549 the service loop drops the send future of a progress notification once the peer cancels its request. AsyncRwTransport::send encodes the whole frame into the FramedWrite buffer and then flushes it; dropped during that flush (a full output pipe), the rest of the frame stays buffered, in the codec or in a buffered writer below it. A later message flushes it, but close() dropped the writer unflushed, so with nothing sent after it the peer read a truncated JSON line before EOF. close() now flushes the writer (FramedWrite::flush, which also flushes the writer below the codec) before dropping it. This is best effort, bounded by CLOSE_FLUSH_TIMEOUT (500 ms) so that a peer that stopped reading cannot hold the shutdown; a write error or the timeout still leaves the line partial. send() and receive() are unchanged. Tests: the abandoned frame reaches the peer whole over a plain pipe, over a BufWriter holding the tail below an empty codec buffer, when EOF arrives together with the cancel, and when EOF is read before the send future is dropped (each fails on main); with a peer that stops reading, the server still stops within the drain deadline plus CLOSE_FLUSH_TIMEOUT.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This branch has not been deployed
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.
Fixes #1557.
Motivation and Context
This regression was introduced by #1549, our pull request. Since #1549, the service loop drops a progress
notification's send future when the peer cancels the request.
AsyncRwTransport::sendbuffers the whole frame in itsFramedWrite, then flushes. If the future is dropped whilethat flush waits on a full pipe, the tail stays buffered.
close()drops the writer without flushing it, so with nolater message the peer reads a truncated line before EOF.
This was pointed out by the automated review on #1549. It also breaks the
Transport::sendcontract that #1549documented.
How Has This Been Tested?
tests/test_progress_flush.rs: a 64-byte output pipe and a progress frame longer than the pipe, whose write is pendingwhen the request is cancelled; in the forced-EOF case no cancel is sent, and the drain deadline drops the write after
EOF. The first four check that every line the peer reads is a whole JSON message; the fifth checks
that shutdown stays bounded.
50fc08aBufWriterholding the tail below an empty codec bufferCLOSE_FLUSH_TIMEOUT)Against the rejected
receive()-side flush (below), theBufWriter, cancel-with-EOF and forced-EOF tests fail 3/3,and the timing test fails 2/3 (its stalled reads).
On
181cb98, before #1549, the first test passes: progress sends were never abandoned.Full runs on this branch:
cargo test -p rmcp --features <all but local>: 1 993 / 0 / 18;--all-features: 1 305 / 0 / 32;--lib --no-default-features: 210 / 0 / 0;-D warnings: clean in all 34 runs (all features, all but local, each feature alone, no defaultfeatures, default).
Breaking Changes
None.
Types of changes
Additional context
AsyncRwTransport::close()now flushes the writer before dropping it:FramedWrite::flush, whichalso flushes the
AsyncWritebelow the codec. The flush is bounded by a 500 ms timeout (CLOSE_FLUSH_TIMEOUT).send future. This is best effort: within the 500 ms and absent a write error. A peer that does not read in that
time still gets a partial line.
send()andreceive()are unchanged, so the change adds no output-dependent wait toreceive(). Its existingprotocol-error reply path already waits for the writer.
graceful_shutdown()closes the transport, and so the child's stdin, before it waits for the child to exit.close(), alive past the drain deadline, andblocked_sends_respect_drain_deadlinefails.receive()while the writer is free. It misses a frame whose send is dropped after EOF hasbeen read, and one still buffered in a writer below the codec. It also stops reads while the peer does not read.
AsyncRwTransportonly, the stdio and child-process transport.SinkStreamTransporthas the sameSinkExt::sendshape and a no-opclose(). Whether a dropped send leaves apartial message there depends on the user's
Sink; aFramedWritewould.refinement of the transport model (each frame written in two chunks, which a full pipe can hold apart; Daavar
benchmarks/mcp-cancel, the byte-level framecell) shows the truncated frame as a violation on main and none on this branch, over all interleavings of that model
within the cell. The model assumes a peer that keeps reading drains within the close bound and that no write fails;
its drain covers only a write already holding the writer (the cell's README states these limits).
Found with. We are a research team at MPI-SWS building Daavar, a conformance checker for distributed protocol
implementations. This follow-up came from the automated review comment on #1549; we confirmed it natively (above) and with
a byte-level refinement of our model of rmcp's stdio write path, where each frame is written in two chunks that a full pipe
can hold apart. On that model the transport at main violates the specification program and the fixed transport conforms, over
all interleavings of the cell. This fix was prepared with AI assistance and reviewed by us.
Reproduce with Daavar (conformance test)
The cell
ca,s0,frA: one tool call with a progress token, the client cancels it and shuts down, then reads the server's outputto EOF. The specification program writes both chunks of a frame together, so every frame the client reads is whole or absent.
implfofollows rmcp main50fc08a;implfffollows the fix. The join prints the words of the implementation the specificationdoes not produce as
uncovered.The cells
frC(no cancel; the drain deadline drops the held write after EOF) andfrE(a later request after the abandonedone) are in the benchmark's README with their counts.