Skip to content

fix: Write concurrent stdio sends one at a time, in call order - #388

Merged
jeanmatthieu merged 2 commits into
rel/1.0.0from
fix/stdio-serialize-sends
Nov 28, 2026
Merged

jeanmatthieu merged 2 commits into
rel/1.0.0from
fix/stdio-serialize-sends

Conversation

@jeanmatthieu

Copy link
Copy Markdown
Contributor

Summary

Fixes #263. Based on #266 by Rohit Salla, credited with Co-authored-by. That PR conflicts with rel/1.0.0 and 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.send sleeps 10 ms on EAGAIN mid-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.

 send(message)
   guard isConnected
   activeSends += 1, defer: -= 1, wake disconnect()
+  acquireOutput()          // FIFO: take the output, or wait for it
+  defer releaseOutput()    // hand it to the oldest waiting send
   write frame + "\n"; on EAGAIN sleep 10 ms and retry
  • A send cancelled while it waits for its turn leaves the queue and throws CancellationError. A send that fails, or is cancelled while it writes, still hands the output on.
  • After disconnect(), each waiting send gets its turn and throws ENOTCONN, so disconnect() still waits for every send.
  • No semaphore, no blocking, no @unchecked Sendable. activeSends goes from private to private(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/releaseOutput lines removed): the frame test fails 10 runs out of 10, with Frame 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 with LIBDISPATCH_COOPERATIVE_POOL_STRICT=1.
    • scripts/run-conformance.sh --spec-version 2025-11-25 and --spec-version 2026-07-28: both pass against the baseline.
    • scripts/run-interop.sh --check: matches interop/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 StdioTransport users. 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.

jeanmatthieu and others added 2 commits November 26, 2026 18:34
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
@jeanmatthieu
jeanmatthieu requested a review from a team as a code owner November 27, 2026 14:08
@jeanmatthieu
jeanmatthieu merged commit 3ae4699 into rel/1.0.0 Nov 28, 2026
14 checks passed
@jeanmatthieu
jeanmatthieu deleted the fix/stdio-serialize-sends branch November 28, 2026 16:00
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