Skip to content

THRIFT-6243: Grow the TNonblockingServer read buffer as the payload arrives - #3847

Merged
Jens-G merged 1 commit into
apache:masterfrom
Jens-G:cpp-nonblocking-incremental-buffer
Sep 12, 2026
Merged

THRIFT-6243: Grow the TNonblockingServer read buffer as the payload arrives#3847
Jens-G merged 1 commit into
apache:masterfrom
Jens-G:cpp-nonblocking-incremental-buffer

Conversation

@Jens-G

@Jens-G Jens-G commented Sep 11, 2026

Copy link
Copy Markdown
Member

TNonblockingServer's 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 getMaxFrameSize() bytes per connection by sending only a four-byte header. (The reservation is address space — a std::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.
  • The buffer grows toward the frame size in SOCKET_RECV as 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 overflow uint32_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_t overflow guard in the new growReadBuffer helper is a small addition the old inline doubling lacked (the old newSize *= 2 would loop forever for a maxFrameSize above 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's VmSize; 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 new min calls (std::min) so the change compiles with MSVC.


Prepared with AI assistance (Claude Opus 4.8).

@mergeable mergeable Bot added the c++ Pull requests that update C++ code label Sep 11, 2026
…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
Jens-G force-pushed the cpp-nonblocking-incremental-buffer branch from 707bae3 to 0cd76a3 Compare September 11, 2026 20:39
@Jens-G
Jens-G merged commit ef00e99 into apache:master Sep 12, 2026
100 of 102 checks passed
@Jens-G
Jens-G deleted the cpp-nonblocking-incremental-buffer branch September 12, 2026 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Pull requests that update C++ code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant