Repository navigation
fix: Write concurrent stdio sends one at a time, in call order - #388
Merged
Merged
Conversation
StdioTransport.send writes a frame in a loop on a non-blocking descriptor. When the pipe is full, write() fails with EAGAIN and the loop sleeps 10 ms before it retries. The actor is reentrant across that await, and nothing ordered sends, so a second send could write into the pipe in the middle of the first frame. The peer then read two torn lines that do not decode, and both messages were lost. A server that answers concurrent requests with large results to a client that reads slowly hits this. Sends now take the output in turn. The first send holds it while it writes its whole frame, including across EAGAIN waits. The others wait in a FIFO queue, and each finished send hands the output to the oldest one waiting. A send cancelled while it waits leaves the queue at once and throws CancellationError. A send that fails, or is cancelled while it writes, still hands the output on. After disconnect() the waiting sends get their turn one after another and fail with ENOTCONN, as the send in progress does, so disconnect() still waits for every send to end. The tests make the first send fill a pipe and suspend mid-frame, start more sends while it waits, then drain the pipe. Each frame has to arrive whole and in call order. Without the fix, the frame test fails in 10 runs out of 10, with frames torn. Fixes #263. Based on #266, which chained each send to the previous one with tasks. Co-authored-by: Rohit Salla <sallarohit1011@gmail.com> Assisted-by: Claude
The test of disconnect with sends queued for the output accepted any error, so a waiting send that failed for another reason, such as a CancellationError from the queue, still passed. It now requires each send to fail with MCPError.transportError wrapping ENOTCONN, the error send throws on a disconnected transport. That includes the send that held the output: nothing reads the pipe, so it cannot finish its frame before disconnect. When a waiting send handed the output on and threw CancellationError on disconnect instead, the test failed for both waiting sends and passed for the send in progress. Assisted-by: Claude
varun06
approved these changes
Nov 28, 2026
This was referenced Dec 5, 2026
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.
Summary
Fixes #263. Based on #266 by Rohit Salla, credited with
Co-authored-by. That PR conflicts withrel/1.0.0and chains sends through tasks, so a cancelled caller stays stuck behind a send that can't finish. This PR keeps its idea and test shape and serializes sends inside the actor.StdioTransport.sendsleeps 10 ms onEAGAINmid-frame. The actor is reentrant across that sleep and nothing ordered sends, so a second send could write into the middle of the first frame, and the peer read two torn lines.CancellationError. A send that fails, or is cancelled while it writes, still hands the output on.disconnect(), each waiting send gets its turn and throwsENOTCONN, sodisconnect()still waits for every send.@unchecked Sendable.activeSendsgoes fromprivatetoprivate(set)so the tests can wait on it. No public API change..gitignore: adds/fuzz/.build.CHANGELOG.md: one line under 1.0.0 Fixes.Not changed: a send cancelled partway through a frame still leaves half a frame on the wire, as before. That needs a decision on what a started send does when cancelled.
Evidence
New tests in
StdioTransportTests:"Concurrent backpressured sends write whole frames in call order": the first 512 KiB send fills the pipe and suspends mid-frame, more sends start, then the pipe is drained. Each frame must arrive whole and in call order.
"A send cancelled while it waits for its turn throws, and later sends still go out"
"Disconnect releases sends waiting for their turn": the send in progress and each waiting send fail with
transportError(ENOTCONN). A waiting send that threw any other error fails the test.Before (only the
acquireOutput/releaseOutputlines removed): the frame test fails 10 runs out of 10, withFrame 0 arrived torn or out of order(also frames 1 and 2), and the cancellation test fails.After:
swift test: 1407 tests in 142 suites pass.swift test --filter StdioTransportTests: 20 of 20 runs pass, and it also passes withLIBDISPATCH_COOPERATIVE_POOL_STRICT=1.scripts/run-conformance.sh --spec-version 2025-11-25and--spec-version 2026-07-28: both pass against the baseline.scripts/run-interop.sh --check: matchesinterop/RESULTS.md.Not run: the Linux legs. The change is in the POSIX path shared by Linux, and uses only actor state and continuations.
Merge Danger
Door: two-way. Revert the commit.
Blast Radius: stdio transport
All
StdioTransportusers. Concurrent sends now wait for each other instead of writing in parallel, so a send can take longer to return when the pipe is full. Before, those messages were lost.AI assistance: written and opened by Claude at the author's request, for human review before merge.