Skip to content

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

Merged
canarik merged 2 commits into
modelcontextprotocol:rel/1.0.0from
canarik:oss/read-loop-line-limit
Nov 30, 2026
Merged

canarik merged 2 commits 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
`searchedByteCount` exists only so tests can check how many bytes the
read loop searched for a newline, but it grows by every received byte
for the life of the transport and was updated with trapping `+=`. On
32-bit watchOS (arm64_32) `Int` is 32 bits, so a transport that has
received 2 GiB overflows the counter, and Swift traps on overflow in
release builds too. Both `StdioTransport` and `NetworkTransport` build
for watchOS.

Use `&+=` at the four sites, so test instrumentation can never crash
production code. The tests send far less than 2 GiB, so their exact
counts are unchanged.

Assisted-by: Claude

@jeanmatthieu jeanmatthieu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed d9aebd8 on top: searchedByteCount is test-only but grew by every received byte for the life of the transport and used trapping +=. On 32-bit watchOS (arm64_32) Int is 32 bits, so a transport that received 2 GiB would trap, in release builds too. It now uses &+= at the four sites in StdioTransport and NetworkTransport; the tests' exact counts are unchanged. Verified locally: swift test passes (1506 tests), and a -O build modelling a 32-bit Int traps with += and wraps with &+=. No arm64_32 build: no watchOS SDK here, and CI has no watchOS lane. LGTM

@canarik
canarik merged commit afb87cb into modelcontextprotocol:rel/1.0.0 Nov 30, 2026
14 checks passed
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