From 8a325869f48fd08e8a7307fdf583a895137b8d99 Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Fri, 20 Nov 2026 08:45:28 +0100 Subject: [PATCH] http2: copy small DATA chunks out of socket reads A DATA chunk handed to JavaScript was always a slice of the ArrayBuffer holding the whole socket read. A small chunk queued in a paused stream therefore kept alive every other byte of that read, such as other frames or padding. Copy a chunk into a right-sized backing store when the socket read is more than four times larger than the chunk, and keep slicing it otherwise, so full-size DATA frames in a full read still avoid the copy. Assisted-By: OpenAI GPT-5.6 Sol Signed-off-by: Paolo Insogna --- src/node_http2.cc | 35 ++++++-- .../test-http2-data-chunk-backing-store.js | 90 +++++++++++++++++++ 2 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 test/parallel/test-http2-data-chunk-backing-store.js diff --git a/src/node_http2.cc b/src/node_http2.cc index 92ca4e7d452f..34e09ec4fbc7 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -1479,7 +1479,8 @@ int Http2Session::OnDataChunkReceived(nghttp2_session* handle, // into those buffers. // The typical case is actually the exception here; Http2StreamListeners // know about the HTTP2 session associated with this stream, so they know - // about the larger from-socket read buffer, so they do not require copying. + // about the larger from-socket read buffer, so they usually do not require + // copying. do { uv_buf_t buf = stream->EmitAlloc(len); ssize_t avail = len; @@ -1580,6 +1581,13 @@ int Http2Session::OnNghttpError(nghttp2_session* handle, return 0; } +// A DATA chunk is passed to JavaScript as a slice of the socket read buffer +// only if that buffer is at most this many times larger than the chunk. +// Smaller chunks are copied, so that a queued Buffer cannot keep alive a +// socket read made mostly of other frames or padding. With the default +// limits, full-size DATA frames in a full socket read are still sliced. +static constexpr size_t kMaxDataChunkSliceRatio = 4; + uv_buf_t Http2StreamListener::OnStreamAlloc(size_t size) { // See the comments in Http2Session::OnDataChunkReceived // (which is the only possible call site for this method). @@ -1598,6 +1606,21 @@ void Http2StreamListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf) { return; } + // Verify that the data is inside the current read buffer. + size_t offset = buf.base - session->stream_buf_.base; + CHECK_LE(offset, session->stream_buf_.len); + CHECK_LE(offset + buf.len, session->stream_buf_.len); + + if (static_cast(nread) * kMaxDataChunkSliceRatio < + session->stream_buf_.len) { + std::unique_ptr bs = ArrayBuffer::NewBackingStore( + env->isolate(), nread, BackingStoreInitializationMode::kUninitialized); + memcpy(bs->Data(), buf.base, nread); + stream->CallJSOnreadMethod( + nread, ArrayBuffer::New(env->isolate(), std::move(bs)), 0); + return; + } + Local ab; if (session->stream_buf_ab_.IsEmpty()) { ab = ArrayBuffer::New(env->isolate(), @@ -1610,12 +1633,6 @@ void Http2StreamListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf) { // There is a single large array buffer for the entire data read from the // network; create a slice of that array buffer and emit it as the // received data buffer. - size_t offset = buf.base - session->stream_buf_.base; - - // Verify that the data offset is inside the current read buffer. - CHECK_LE(offset, session->stream_buf_.len); - CHECK_LE(offset + buf.len, session->stream_buf_.len); - stream->CallJSOnreadMethod(nread, ab, offset); } @@ -2336,8 +2353,8 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) { static_cast(nread)); // Store this so we can create an ArrayBuffer for read data from it. - // DATA frames will be emitted as slices of that ArrayBuffer to avoid having - // to copy memory. + // Large DATA frames will be emitted as slices of that ArrayBuffer to avoid + // having to copy memory. stream_buf_allocation_ = std::move(bs); ConsumeHTTP2Data(); diff --git a/test/parallel/test-http2-data-chunk-backing-store.js b/test/parallel/test-http2-data-chunk-backing-store.js new file mode 100644 index 000000000000..db047919d335 --- /dev/null +++ b/test/parallel/test-http2-data-chunk-backing-store.js @@ -0,0 +1,90 @@ +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +const assert = require('assert'); +const http2 = require('http2'); +const net = require('net'); + +// A DATA chunk queued in a paused request body must not keep alive the +// whole socket read it arrived in. Each batch below is an ignored extension +// frame followed by a one-byte DATA frame. + +const batchCount = 64; + +function frame(type, flags, id, payload) { + const header = Buffer.alloc(9); + header.writeUIntBE(payload.length, 0, 3); + header[3] = type; + header[4] = flags; + header.writeUInt32BE(id, 5); + return Buffer.concat([header, payload]); +} + +const filler = frame(0xb, 0, 0, Buffer.alloc(16_365, 0x5a)); +const data = frame(0, 0, 1, Buffer.from('a')); +const batch = Buffer.concat([filler, data]); +const pingPayload = Buffer.from('datachnk'); + +let requestStream; + +const server = http2.createServer(); +server.on('stream', common.mustCall((stream) => { + requestStream = stream; + // Leave the request body unconsumed so the chunks stay queued. +})); + +server.listen(0, common.mustCall(() => { + const peer = net.connect(server.address().port); + let received = Buffer.alloc(0); + let settingsSeen = false; + + peer.on('connect', () => { + peer.write(Buffer.from('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n')); + peer.write(frame(4, 0, 0, Buffer.alloc(0))); + }); + + peer.on('data', (chunk) => { + received = Buffer.concat([received, chunk]); + while (received.length >= 9) { + const length = received.readUIntBE(0, 3); + if (received.length < 9 + length) break; + const type = received[3]; + const flags = received[4]; + const payload = received.subarray(9, 9 + length); + received = received.subarray(9 + length); + + if (type === 4 && !(flags & 1) && !settingsSeen) { + settingsSeen = true; + peer.write(frame(4, 1, 0, Buffer.alloc(0))); + const hpack = Buffer.from([0x83, 0x86, 0x84, 0x01, 0x01, 0x61]); + peer.write(frame(1, 4, 1, hpack)); + for (let i = 0; i < batchCount; i++) + peer.write(batch); + // The PING is answered only after everything before it was parsed. + peer.write(frame(6, 0, 0, pingPayload)); + } else if (type === 6 && (flags & 1) && payload.equals(pingPayload)) { + check(); + peer.destroy(); + server.close(); + } + } + }); +})); + +function check() { + const buffers = []; + let buffer; + while ((buffer = requestStream.read(1)) !== null) + buffers.push(buffer); + + assert.strictEqual(buffers.length, batchCount); + for (const chunk of buffers) { + assert.strictEqual(chunk.toString(), 'a'); + assert.ok(chunk.buffer.byteLength < 1024, + `a ${chunk.length} byte chunk retains ` + + `${chunk.buffer.byteLength} bytes`); + } +}