Repository navigation
Conversation
`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.
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.
Motivation and Context
StdioTransport.readLoopandNetworkTransport.receiveLoopappend each read to a pendingbuffer and call
firstIndex(of:)on the whole buffer to find the next newline. Until a messageends, 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.
searches only the bytes it added. Messages are delivered exactly as before.
HTTPClientTransport, endsthe receive stream with
internalError("Message longer than 16 MiB")instead of beingbuffered. This holds for a message that ends in the read that completes it and for the
unterminated start of one. A
Clientthen fails its pending requests with that error.NetworkTransportalso cancels its connection. Its limit isBufferConfiguration.maxReceiveBufferSizewhen that is larger than 16 MiB, andBufferConfiguration.unlimitedkeeps no limit. TheStdioTransportlimit is fixed, as the SSEline limit is: no setting raises or removes it.
How Has This Been Tested?
Adds four tests to
StdioTransportTestsand six toNetworkTransportTests. A 1 MiB messageread 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 thefix). 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
.unlimitedand a 20 MiB buffer letlonger messages through. Without the fix the searched-once and over-limit tests fail.
swift testpasses 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
NetworkTransportwhose buffer size is at most 16 MiB, now ends the connection withinternalError; such a message was buffered whole before.Types of changes
AI Assistance
Drafted with an AI coding agent, then reviewed by me.