This is an automated email from the ASF dual-hosted git repository.

wwbmmm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new fbbb7daf3 Fix HTTP/2 window accounting and parser test socket leaks 
(#3749)
fbbb7daf3 is described below

commit fbbb7daf33ee2a8c398dd82aeb84d27d6bc9f4d8
Author: Bright Chen <[email protected]>
AuthorDate: Tue Nov 17 20:09:24 2026 +0800

    Fix HTTP/2 window accounting and parser test socket leaks (#3749)
    
    * Fix HTTP/2 window accounting and parser test socket leaks
    
    * Guard the HTTP/2 test pipe during socket creatio
---
 src/brpc/policy/http2_rpc_protocol.cpp   |   8 +-
 test/brpc_couchbase_unittest.cpp         |   3 +
 test/brpc_http_rpc_protocol_unittest.cpp | 159 +++++++++++++++++++++++++++++++
 test/brpc_memcache_unittest.cpp          |   3 +
 4 files changed, 171 insertions(+), 2 deletions(-)

diff --git a/src/brpc/policy/http2_rpc_protocol.cpp 
b/src/brpc/policy/http2_rpc_protocol.cpp
index 438603bdc..2768cb37e 100644
--- a/src/brpc/policy/http2_rpc_protocol.cpp
+++ b/src/brpc/policy/http2_rpc_protocol.cpp
@@ -931,6 +931,12 @@ H2ParseResult H2StreamContext::OnData(
     butil::IOBuf data;
     it.append_and_forward(&data, frag_size);
     it.forward(pad_length);
+    // Account for the full DATA payload, including padding, before OnBody()
+    // can reject it. Stream cleanup must return this frame's connection credit
+    // even when the body exceeds max_body_size.
+    int64_t acc = frame_head.payload_size +
+        _deferred_window_update.fetch_add(
+            frame_head.payload_size, butil::memory_order_relaxed);
     for (size_t i = 0; i < data.backing_block_num(); ++i) {
         const butil::StringPiece blk = data.backing_block(i);
         if (OnBody(blk.data(), blk.size()) != 0) {
@@ -942,8 +948,6 @@ H2ParseResult H2StreamContext::OnData(
         }
     }
 
-    int64_t acc = frag_size +
-        _deferred_window_update.fetch_add(frag_size, 
butil::memory_order_relaxed);
     int64_t quota = static_cast<int64_t>(
         _conn_ctx->local_settings().stream_window_size /
         (_conn_ctx->VolatilePendingStreamSize() + 1));
diff --git a/test/brpc_couchbase_unittest.cpp b/test/brpc_couchbase_unittest.cpp
index 87278d6b6..6ec6584d9 100644
--- a/test/brpc_couchbase_unittest.cpp
+++ b/test/brpc_couchbase_unittest.cpp
@@ -70,6 +70,7 @@ TEST_F(CouchbaseUnitTest, 
RejectOversizedResponseBeforeBufferingBody) {
   ASSERT_EQ(0, brpc::Socket::Create(options, &id));
   brpc::SocketUniquePtr socket;
   ASSERT_EQ(0, brpc::Socket::Address(id, &socket));
+  ASSERT_EQ(0, socket->ReleaseAdditionalReference());
 
   brpc::policy::CouchbaseResponseHeader couchbase_header = {};
   couchbase_header.magic = brpc::policy::CB_MAGIC_RESPONSE;
@@ -80,6 +81,8 @@ TEST_F(CouchbaseUnitTest, 
RejectOversizedResponseBeforeBufferingBody) {
             brpc::policy::ParseCouchbaseMessage(
                 &couchbase_buf, socket.get(), false, nullptr).error());
 
+  socket.reset();
+  EXPECT_EQ(-1, brpc::Socket::Status(id));
 }
 
 TEST_F(CouchbaseUnitTest, RequestBuilders) {
diff --git a/test/brpc_http_rpc_protocol_unittest.cpp 
b/test/brpc_http_rpc_protocol_unittest.cpp
index 1546db4a0..96e2f73d7 100644
--- a/test/brpc_http_rpc_protocol_unittest.cpp
+++ b/test/brpc_http_rpc_protocol_unittest.cpp
@@ -38,9 +38,11 @@
 #include <brpc/policy/http_rpc_protocol.h>
 #include <butil/base64.h>
 #include "brpc/http_method.h"
+#include "brpc/global.h"
 #include "brpc/grpc.h"
 #include "butil/iobuf.h"
 #include "butil/logging.h"
+#include "butil/sys_byteorder.h"
 #include "butil/files/scoped_file.h"
 #include "butil/fd_guard.h"
 #include "butil/fd_utility.h"
@@ -2288,6 +2290,163 @@ TEST_F(HttpTest, 
http2_deferred_response_notify_registration_failure_resets_stre
     EXPECT_EQ(0, bthread_id_error(dummy_id, 0));
 }
 
+TEST(Http2FlowControlTest, oversized_body_returns_connection_window) {
+    brpc::GlobalInitializeOrDie();
+    GFLAGS_NAMESPACE::FlagSaver flag_saver;
+    brpc::FLAGS_max_body_size = 4;
+
+    for (bool client_side : {false, true}) {
+        SCOPED_TRACE(client_side);
+        int fds[2];
+        ASSERT_EQ(0, pipe(fds));
+        butil::fd_guard read_fd(fds[0]);
+        butil::fd_guard write_fd(fds[1]);
+        brpc::SocketOptions options;
+        options.fd = write_fd;
+        if (client_side) {
+            options.user = brpc::get_client_side_messenger();
+        }
+        brpc::SocketId id;
+        ASSERT_EQ(0, brpc::Socket::Create(options, &id));
+        write_fd.release();  // Socket owns the fd after successful creation.
+        brpc::SocketUniquePtr socket;
+        ASSERT_EQ(0, brpc::Socket::Address(id, &socket));
+        ASSERT_EQ(0, socket->ReleaseAdditionalReference());
+
+        brpc::policy::H2Context ctx(socket.get(), nullptr);
+        ASSERT_EQ(0, ctx.Init());
+        ASSERT_EQ(client_side, ctx.is_client_side());
+        ctx._conn_state = brpc::policy::H2_CONNECTION_READY;
+        // Flush connection credit on each rejected body, while keeping the
+        // first three bytes of a split body below the stream-update threshold.
+        ctx._local_settings.stream_window_size = 8;
+        ctx._unack_local_settings.stream_window_size = 8;
+
+        auto consume_frame = [&](brpc::policy::H2FrameType type, uint8_t flags,
+                                 int stream_id, const std::string& payload) {
+            char head[brpc::policy::FRAME_HEAD_SIZE];
+            brpc::policy::SerializeFrameHead(
+                head, payload.size(), type, flags, stream_id);
+            butil::IOBuf buf;
+            buf.append(head, sizeof(head));
+            buf.append(payload);
+            butil::IOBufBytesIterator it(buf);
+            brpc::ParseResult result = ctx.Consume(it, socket.get());
+            EXPECT_EQ(0u, it.bytes_left());
+            return result;
+        };
+
+        auto start_stream = [&](int stream_id) {
+            if (client_side) {
+                // A response arrives on a stream already opened by a request.
+                std::unique_ptr<brpc::policy::H2StreamContext> sctx(
+                    new brpc::policy::H2StreamContext(false));
+                sctx->Init(&ctx, stream_id);
+                ASSERT_EQ(0, ctx.TryToInsertStream(stream_id, sctx.get()));
+                sctx.release();
+            }
+            // HPACK static entries: :status 200, or :method POST, :scheme 
http,
+            // :path /. END_HEADERS leaves the body open.
+            std::string headers = client_side ? "\x88" : "\x83\x86\x84";
+            brpc::ParseResult result = consume_frame(
+                brpc::policy::H2_FRAME_HEADERS, 0x4, stream_id, headers);
+            ASSERT_TRUE(result.is_ok()) << result.error_str();
+            ASSERT_EQ(nullptr, result.message());
+        };
+
+        auto check_replies = [&](int stream_id, bool reset,
+                                 uint32_t expected_credit) {
+            int bytes = 0;
+            ASSERT_EQ(0, ioctl(read_fd, FIONREAD, &bytes));
+            ASSERT_GT(bytes, 0);
+            butil::IOPortal replies;
+            ASSERT_EQ(bytes,
+                      replies.append_from_file_descriptor(read_fd, bytes));
+            butil::IOBufBytesIterator it(replies);
+            int resets = 0;
+            uint32_t connection_credit = 0;
+            uint32_t stream_credit = 0;
+            while (it.bytes_left() != 0) {
+                brpc::policy::H2FrameHead head;
+                ASSERT_TRUE(ctx.ConsumeFrameHead(it, &head).is_ok());
+                ASSERT_EQ(4u, head.payload_size);
+                uint32_t value;
+                ASSERT_EQ(sizeof(value),
+                          it.copy_and_forward(&value, sizeof(value)));
+                value = butil::NetToHost32(value);
+                if (head.type == brpc::policy::H2_FRAME_RST_STREAM) {
+                    EXPECT_EQ(stream_id, head.stream_id);
+                    EXPECT_EQ(brpc::H2_ENHANCE_YOUR_CALM, value);
+                    ++resets;
+                } else {
+                    ASSERT_EQ(brpc::policy::H2_FRAME_WINDOW_UPDATE, head.type);
+                    if (head.stream_id == 0) {
+                        connection_credit += value;
+                    } else {
+                        EXPECT_EQ(stream_id, head.stream_id);
+                        stream_credit += value;
+                    }
+                }
+            }
+            EXPECT_EQ(reset ? 1 : 0, resets);
+            EXPECT_EQ(expected_credit, connection_credit);
+            EXPECT_EQ(reset ? 0u : expected_credit, stream_credit);
+        };
+
+        int stream_id = 1;
+        for (bool split_body : {false, true}) {
+            for (bool padded : {false, true}) {
+                SCOPED_TRACE(stream_id);
+                ASSERT_NO_FATAL_FAILURE(start_stream(stream_id));
+                if (split_body) {
+                    brpc::ParseResult first = consume_frame(
+                        brpc::policy::H2_FRAME_DATA, 0, stream_id, "hel");
+                    ASSERT_TRUE(first.is_ok());
+                    ASSERT_EQ(nullptr, first.message());
+                }
+                std::string payload = split_body ? "lo" : "hello";
+                uint8_t flags = 0x1;  // END_STREAM
+                if (padded) {
+                    payload.insert(0, 1, '\x02');
+                    payload.append(2, '\0');
+                    flags |= 0x8;  // PADDED
+                }
+                brpc::ParseResult result = consume_frame(
+                    brpc::policy::H2_FRAME_DATA, flags, stream_id, payload);
+                brpc::DestroyingPtr<brpc::InputMessageBase> message(
+                    result.message());
+                ASSERT_TRUE(result.is_ok()) << result.error_str();
+                EXPECT_EQ(client_side, message != nullptr);
+                EXPECT_EQ(nullptr, ctx.FindStream(stream_id));
+                // All five body bytes must be returned, including the entire
+                // offending frame, plus the pad-length byte and padding.
+                ASSERT_NO_FATAL_FAILURE(
+                    check_replies(stream_id, true, padded ? 8 : 5));
+                EXPECT_EQ(0, ctx._deferred_window_update.load(
+                                 butil::memory_order_relaxed));
+                stream_id += 2;
+            }
+        }
+
+        // A body exactly at the limit still completes on the same connection.
+        // Padding consumes flow-control credit but does not count as body 
data.
+        ASSERT_NO_FATAL_FAILURE(start_stream(stream_id));
+        std::string payload(1, '\x02');
+        payload.append("good");
+        payload.append(2, '\0');
+        brpc::ParseResult result = consume_frame(
+            brpc::policy::H2_FRAME_DATA, 0x9, stream_id, payload);
+        brpc::DestroyingPtr<brpc::policy::H2StreamContext> message(
+            static_cast<brpc::policy::H2StreamContext*>(result.message()));
+        ASSERT_TRUE(result.is_ok()) << result.error_str();
+        ASSERT_TRUE(message != nullptr);
+        EXPECT_EQ("good", message->body().to_string());
+        ASSERT_NO_FATAL_FAILURE(check_replies(stream_id, false, 7));
+        EXPECT_EQ(brpc::policy::H2_CONNECTION_READY, ctx._conn_state);
+        EXPECT_FALSE(socket->Failed());
+    }
+}
+
 TEST_F(HttpTest, http2_settings) {
     char settingsbuf[brpc::policy::FRAME_HEAD_SIZE + 36];
     brpc::H2Settings h2_settings;
diff --git a/test/brpc_memcache_unittest.cpp b/test/brpc_memcache_unittest.cpp
index a43a1e013..92cb0a00f 100644
--- a/test/brpc_memcache_unittest.cpp
+++ b/test/brpc_memcache_unittest.cpp
@@ -50,6 +50,7 @@ TEST(MemcacheParserTest, 
RejectOversizedResponseBeforeBufferingBody) {
     ASSERT_EQ(0, brpc::Socket::Create(options, &id));
     brpc::SocketUniquePtr socket;
     ASSERT_EQ(0, brpc::Socket::Address(id, &socket));
+    ASSERT_EQ(0, socket->ReleaseAdditionalReference());
 
     brpc::policy::MemcacheResponseHeader header = {};
     header.magic = brpc::policy::MC_MAGIC_RESPONSE;
@@ -60,6 +61,8 @@ TEST(MemcacheParserTest, 
RejectOversizedResponseBeforeBufferingBody) {
               brpc::policy::ParseMemcacheMessage(
                   &buf, socket.get(), false, nullptr).error());
 
+    socket.reset();
+    EXPECT_EQ(-1, brpc::Socket::Status(id));
 }
 
 TEST(MemcacheParserTest, PopStoreRejectsNegativeValueSize) {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to