Skip to content

fix: search only new bytes for a newline and limit message length - #405

Open
canarik wants to merge 1 commit into
modelcontextprotocol:rel/1.0.0from
canarik:oss/read-loop-line-limit
Open

canarik wants to merge 1 commit into
modelcontextprotocol:rel/1.0.0from
canarik:oss/read-loop-line-limit

Conversation

@canarik

@canarik canarik commented Nov 29, 2026

Copy link
Copy Markdown

Motivation and Context

StdioTransport.readLoop and NetworkTransport.receiveLoop append each read to a pending
buffer and call firstIndex(of:) on the whole buffer to find the next newline. Until a message
ends, every read searches all the bytes buffered so far again, so the cost grows with the square
of the message size: an 8 MiB message read in 4 KiB pieces is searched about 8 GiB in all. The
buffer has no limit either, so a peer that never sends a newline (a server that prints something
other than JSON-RPC to stdout, for example) makes the reader grow its memory without bound.

  • Each loop remembers how many bytes at the start of its buffer hold no newline, and a read
    searches only the bytes it added. Messages are delivered exactly as before.
  • A received message longer than 16 MiB, the limit of an SSE line in HTTPClientTransport, ends
    the receive stream with internalError("Message longer than 16 MiB") instead of being
    buffered. This holds for a message that ends in the read that completes it and for the
    unterminated start of one. A Client then fails its pending requests with that error.
  • NetworkTransport also cancels its connection. Its limit is
    BufferConfiguration.maxReceiveBufferSize when that is larger than 16 MiB, and
    BufferConfiguration.unlimited keeps no limit. The StdioTransport limit is fixed, as the SSE
    line limit is: no setting raises or removes it.
  • The class documentation, the changelog, the migration guide and the threat model note the limit.

How Has This Been Tested?

Adds four tests to StdioTransportTests and six to NetworkTransportTests. A 1 MiB message
read in pieces is searched for its newline once, checked through an internal
searchedByteCount (more than 100 MiB on stdio and 500 MiB on the network transport before the
fix). A message one byte over the limit ends the stream, both unterminated and ended in the read
that completes it, and on the network transport the connection is cancelled. A message of
exactly 16 MiB is delivered, and on the network transport .unlimited and a 20 MiB buffer let
longer messages through. Without the fix the searched-once and over-limit tests fail. swift test
passes on macOS. I did not run the tests on Linux.

Breaking Changes

None in the API. A peer that sends a message longer than 16 MiB over stdio, or over a
NetworkTransport whose buffer size is at most 16 MiB, now ends the connection with
internalError; such a message was buffered whole before.

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.readLoop` and `NetworkTransport.receiveLoop` append
each read to a pending buffer and call `firstIndex(of:)` on the whole
buffer to find the next newline. Until a message ends, every read
searches all the bytes buffered so far again, so an 8 MiB message read
in 4 KiB pieces is searched about 8 GiB in all. Nothing limits the
buffer either, so a peer that never sends a newline grows it without
bound.
Fix: each loop remembers how many bytes at the start of its buffer
hold no newline, and a read searches only the bytes after them.

A received message longer than 16 MiB, the limit of an SSE line in
`HTTPClientTransport`, now ends the receive stream with
`internalError` instead of being buffered. A `Client` then fails its
pending requests with that error. `NetworkTransport` also cancels its
connection. Its limit is `BufferConfiguration.maxReceiveBufferSize`
when that is larger than 16 MiB, and `.unlimited` keeps no limit. The
class documentation, the changelog, the migration guide and the threat
model say so.

Adds tests to `StdioTransportTests` and `NetworkTransportTests`: a
1 MiB message read in pieces is searched once, read through an internal
`searchedByteCount`; a message one byte over the limit, unterminated or
ended in the read that completes it, ends the stream; a message of
exactly 16 MiB is delivered; and on the network transport `.unlimited`
and a 20 MiB buffer let longer messages through. The searched-once and
over-limit tests fail without the fix.
@canarik
canarik requested a review from a team as a code owner November 29, 2026 22:12
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