THRIFT-6243: Grow the TNonblockingServer read buffer as the payload arrives - #3847
Merged
Merged
Conversation
…rrives Client: cpp TConnection reserved the whole declared frame the moment it read the four-byte length prefix: transition() reallocated the read buffer to the frame size before any payload byte had arrived, so a peer could make the server reserve up to the maximum frame size by sending only a header. The buffer now grows toward the frame size in SOCKET_RECV as bytes are actually read -- by doubling, with the growth guarded against uint32_t overflow -- so the reservation tracks the payload received. transition() reserves only the length prefix and a small initial read. A frame that fully arrives ends at the same buffer size as before; a header with no payload reserves next to nothing. A large frame that does arrive pays the amortized doubling copy any grow-as-you-go buffer pays. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Jens-G
force-pushed
the
cpp-nonblocking-incremental-buffer
branch
from
September 11, 2026 20:39
707bae3 to
0cd76a3
Compare
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.
TNonblockingServer'sTConnectionreserved the whole declared frame the moment it read the four-byte length prefix:transition()reallocated the read buffer to the frame size before any payload byte had arrived, so a peer could make the server reserve up togetMaxFrameSize()bytes per connection by sending only a four-byte header. (The reservation is address space — astd::realloc— rather than resident memory, but it is still an unamplified header-to-allocation step.)This bounds the allocation by what the peer actually sends:
transition()reserves only enough to hold the length prefix and begin reading (the prefix plus about a kilobyte), not the declared frame.SOCKET_RECVas bytes are actually read — by the same doubling the up-front allocation used, now driven by what the peer sends, with the doubling guarded so it cannot overflowuint32_t.A frame that fully arrives ends at the same buffer size as before, so there is no change for legitimate traffic in the steady state, and no change at all for frames of a kilobyte or less. A header with no payload reserves next to nothing. A large frame that does arrive now pays the amortized doubling copy any grow-as-you-go buffer pays.
The
uint32_toverflow guard in the newgrowReadBufferhelper is a small addition the old inline doubling lacked (the oldnewSize *= 2would loop forever for amaxFrameSizeabove 2 GiB); it is included here — happy to split it to its own ticket if a reviewer prefers.Test
Four cases in
TNonblockingServerTest: a Linux-only, forked case asserts that a 256 MiB frame header with no body does not grow the server'sVmSize; two portable cases drive multi-megabyte requests that are assembled over many libevent callbacks (the buffer doubling as bytes arrive) and must round-trip byte-for-byte, one freeing the grown buffer at connection close and one between frames on a live connection; and a portable case writes two requests in a single send and checks that both are answered, so each read stops at the end of its own frame even when the buffer is larger than the frame.Related
This is part (4) of the nonblocking-server frame handling; part (2)+(3), wiring the server to
TConfiguration, is THRIFT-6242 (#3846, merged), and this branch is rebased onto it. This revision also stops each read at the end of the current frame (the buffer can be larger than the frame, and the previous revision could read into a following request) and spells the newmincalls(std::min)so the change compiles with MSVC.Prepared with AI assistance (Claude Opus 4.8).