You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by "wwbmmm (via GitHub)" <gi...@apache.org> on 2026/08/28 09:17:38 UTC
[PR] Contain malformed Thrift T_EXCEPTION replies on the client (brpc)
wwbmmm opened a new pull request, #3500:
URL: https://github.com/apache/brpc/pull/3500
## What problem does this PR solve?
Problem Summary:
A bRPC Thrift client can be brought down by a peer reply that carries a
malformed `T_EXCEPTION`. The exception struct is decoded in
`ReadThriftException` without a try/catch; a bad field or an oversized
length makes the underlying thrift library throw (`TProtocolException`,
`TTransportException` or `std::length_error`). The exception is not caught
anywhere on the return path and eventually reaches the bthread task frame,
where `std::terminate()` is called, terminating the whole process.
This only affects builds with `WITH_THRIFT` enabled (off by default).
## What is changed and the side effects?
Changed:
`ReadThriftException` now wraps the decode of the exception struct in the
same `try/catch` that `ReadThriftStruct` already uses for the normal reply
body. A malformed `T_EXCEPTION` reply is reported as a failed RPC instead
of crashing the process.
Side effects:
- None. The fix only contains an otherwise-uncaught exception.
- Performance effects: none.
- Breaking backward compatibility: none.
## Check List
- [x] The change is compilable.
- [x] A unit test is added in `test/brpc_thrift_protocol_unittest.cpp`
that feeds a malformed `T_EXCEPTION` reply through the client
response path and asserts the call does not crash and marks the
controller failed.
- [x] Follows the Contributor Covenant Code of Conduct.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Contain malformed Thrift T_EXCEPTION replies on the client (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3500:
URL: https://github.com/apache/brpc/pull/3500#discussion_r3879529115
##########
test/brpc_thrift_protocol_unittest.cpp:
##########
@@ -0,0 +1,120 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// brpc - A framework to host and access services throughout Baidu.
+
+#include <arpa/inet.h>
+#include <gtest/gtest.h>
+#include <gflags/gflags.h>
+#include "butil/macros.h"
+#include "brpc/socket.h"
+#include "brpc/controller.h"
+#include "brpc/policy/most_common_message.h"
+#include "brpc/policy/thrift_protocol.h"
+
+int main(int argc, char* argv[]) {
+ testing::InitGoogleTest(&argc, argv);
+ GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
+ return RUN_ALL_TESTS();
+}
+
+namespace {
+
+// A nominal thrift message-type value for a T_EXCEPTION reply.
+static const uint32_t THRIFT_TYPE_EXCEPTION = 3;
+static const uint32_t THRIFT_HEAD_VERSION_1 = 0x80010000;
+
+class ThriftProtocolTest : public ::testing::Test {
+protected:
+ ThriftProtocolTest() {
+ EXPECT_EQ(0, pipe(_pipe_fds));
+ brpc::SocketId id;
+ brpc::SocketOptions options;
+ options.fd = _pipe_fds[1];
+ EXPECT_EQ(0, brpc::Socket::Create(options, &id));
+ EXPECT_EQ(0, brpc::Socket::Address(id, &_socket));
+ }
+
+ virtual void SetUp() {}
+ virtual void TearDown() {}
Review Comment:
The fixture allocates OS resources (`pipe`) in the constructor, uses gtest assertions in the constructor, and never closes the pipe fds. This can leak file descriptors across tests and makes failures harder to attribute. Move resource setup into `SetUp()` (use `ASSERT_*` to abort the test on failure) and close both `_pipe_fds[]` in `TearDown()` (or use an RAII wrapper) to prevent fd leaks.
##########
test/brpc_thrift_protocol_unittest.cpp:
##########
@@ -0,0 +1,120 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// brpc - A framework to host and access services throughout Baidu.
+
+#include <arpa/inet.h>
+#include <gtest/gtest.h>
+#include <gflags/gflags.h>
+#include "butil/macros.h"
+#include "brpc/socket.h"
+#include "brpc/controller.h"
+#include "brpc/policy/most_common_message.h"
+#include "brpc/policy/thrift_protocol.h"
Review Comment:
This test file depends on Thrift support being enabled (`WITH_THRIFT` per PR description). As written, it will likely fail to compile in builds where Thrift is disabled. Consider guarding the entire file (or the `#include \"brpc/policy/thrift_protocol.h\"` + tests) behind the repository’s Thrift feature macro (e.g., `#if defined(WITH_THRIFT)` / project-equivalent), and either omit the tests or add a `GTEST_SKIP()`/no-op test when Thrift is not enabled.
##########
test/brpc_thrift_protocol_unittest.cpp:
##########
@@ -0,0 +1,120 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// brpc - A framework to host and access services throughout Baidu.
+
+#include <arpa/inet.h>
+#include <gtest/gtest.h>
+#include <gflags/gflags.h>
+#include "butil/macros.h"
+#include "brpc/socket.h"
+#include "brpc/controller.h"
+#include "brpc/policy/most_common_message.h"
+#include "brpc/policy/thrift_protocol.h"
+
+int main(int argc, char* argv[]) {
+ testing::InitGoogleTest(&argc, argv);
+ GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
+ return RUN_ALL_TESTS();
+}
+
+namespace {
+
+// A nominal thrift message-type value for a T_EXCEPTION reply.
+static const uint32_t THRIFT_TYPE_EXCEPTION = 3;
+static const uint32_t THRIFT_HEAD_VERSION_1 = 0x80010000;
+
+class ThriftProtocolTest : public ::testing::Test {
+protected:
+ ThriftProtocolTest() {
+ EXPECT_EQ(0, pipe(_pipe_fds));
Review Comment:
`pipe()` is used but `<unistd.h>` is not included. Depending on toolchain and transitive includes, this can cause compilation failures (implicit declaration / missing prototype). Add the appropriate header (`#include <unistd.h>`) to make the dependency explicit.
##########
src/brpc/policy/thrift_protocol.cpp:
##########
@@ -184,9 +184,21 @@ void ReadThriftException(const butil::IOBuf& body,
::apache::thrift::transport::TMemoryBuffer::TAKE_OWNERSHIP);
apache::thrift::protocol::TBinaryProtocolT<apache::thrift::transport::TMemoryBuffer> iprot(in_buffer);
- x->read(&iprot);
- iprot.readMessageEnd();
- iprot.getTransport()->readEnd();
+ // A malformed exception struct may make the underlying thrift code throw
+ // (e.g. TProtocolException on a bad field or TTransportException /
+ // std::length_error on a bad length). Such an exception must be contained
+ // here: if it propagated out, it would unwind through ProcessThriftResponse
+ // up to the bthread task frame and call std::terminate(), taking down the
+ // whole process along with every other in-flight RPC on it.
+ try {
+ x->read(&iprot);
+ iprot.readMessageEnd();
+ iprot.getTransport()->readEnd();
+ } catch (std::exception& e) {
Review Comment:
Catch `std::exception` by `const` reference (`const std::exception& e`) to avoid accidental mutation and to match common C++ exception-handling conventions.
##########
src/brpc/policy/thrift_protocol.cpp:
##########
@@ -184,9 +184,21 @@ void ReadThriftException(const butil::IOBuf& body,
::apache::thrift::transport::TMemoryBuffer::TAKE_OWNERSHIP);
apache::thrift::protocol::TBinaryProtocolT<apache::thrift::transport::TMemoryBuffer> iprot(in_buffer);
- x->read(&iprot);
- iprot.readMessageEnd();
- iprot.getTransport()->readEnd();
+ // A malformed exception struct may make the underlying thrift code throw
+ // (e.g. TProtocolException on a bad field or TTransportException /
+ // std::length_error on a bad length). Such an exception must be contained
+ // here: if it propagated out, it would unwind through ProcessThriftResponse
+ // up to the bthread task frame and call std::terminate(), taking down the
+ // whole process along with every other in-flight RPC on it.
+ try {
+ x->read(&iprot);
+ iprot.readMessageEnd();
+ iprot.getTransport()->readEnd();
+ } catch (std::exception& e) {
+ LOG(WARNING) << "Catched thrift exception while parsing T_EXCEPTION reply: " << e.what();
+ } catch (...) {
+ LOG(WARNING) << "Catched unknown thrift exception while parsing T_EXCEPTION reply";
Review Comment:
Correct spelling/grammar in log messages: use 'Caught' instead of 'Catched'.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Contain malformed Thrift T_EXCEPTION replies on the client (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3500:
URL: https://github.com/apache/brpc/pull/3500#discussion_r3880072300
##########
test/brpc_thrift_protocol_unittest.cpp:
##########
@@ -0,0 +1,152 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// brpc - A framework to host and access services throughout Baidu.
+
+#include <gtest/gtest.h>
+#include <gflags/gflags.h>
+#include <unistd.h>
+
+int main(int argc, char* argv[]) {
+ testing::InitGoogleTest(&argc, argv);
+ GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
+ return RUN_ALL_TESTS();
+}
+
+// The malformed T_EXCEPTION reply is decoded by the Thrift framed protocol,
+// which is only compiled in when the build has THRIFT support enabled (the
+// WITH_THRIFT build flag / brpc_with_thrift config, off by default). Keep this
+// test a trivial pass on builds without Thrift so it links cleanly there too.
+#ifdef ENABLE_THRIFT_FRAMED_PROTOCOL
+
+#include <arpa/inet.h>
+#include "butil/macros.h"
+#include "brpc/socket.h"
+#include "brpc/controller.h"
+#include "brpc/policy/most_common_message.h"
+#include "brpc/policy/thrift_protocol.h"
+
+namespace {
+
+// A nominal thrift message-type value for a T_EXCEPTION reply.
+static const uint32_t THRIFT_TYPE_EXCEPTION = 3;
+static const uint32_t THRIFT_HEAD_VERSION_1 = 0x80010000;
+
+class ThriftProtocolTest : public ::testing::Test {
+protected:
+ ThriftProtocolTest() {
+ _pipe_fds[0] = _pipe_fds[1] = -1;
+ EXPECT_EQ(0, pipe(_pipe_fds));
+ brpc::SocketId id;
+ brpc::SocketOptions options;
+ options.fd = _pipe_fds[1];
+ EXPECT_EQ(0, brpc::Socket::Create(options, &id));
+ EXPECT_EQ(0, brpc::Socket::Address(id, &_socket));
+ }
+
+ virtual void SetUp() {}
+ virtual void TearDown() {
+ // Close the pipe fds and destroy the test socket so no OS resources
+ // are leaked across tests.
+ if (_pipe_fds[0] >= 0) {
+ close(_pipe_fds[0]);
+ _pipe_fds[0] = -1;
+ }
+ if (_pipe_fds[1] >= 0) {
+ close(_pipe_fds[1]);
+ _pipe_fds[1] = -1;
+ }
+ _socket.reset();
+ }
Review Comment:
In TearDown(), `_pipe_fds[1]` is the write-end passed into `SocketOptions::fd`, and `SocketOptions` explicitly transfers ownership of that fd to `brpc::Socket` (which closes it on recycle/destruction). Closing `_pipe_fds[1]` here can double-close the fd (and even close an unrelated fd if the number gets reused), leading to flaky tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Contain malformed Thrift T_EXCEPTION replies on the client (brpc)
Posted by "wwbmmm (via GitHub)" <gi...@apache.org>.
wwbmmm commented on code in PR #3500:
URL: https://github.com/apache/brpc/pull/3500#discussion_r3886085521
##########
test/BUILD.bazel:
##########
@@ -24,7 +24,12 @@ COPTS = [
"-Wno-unused-parameter",
"-fno-omit-frame-pointer",
"-fno-access-control",
-]
+] + select({
+ # The thrift framed protocol (and the unit test that exercises it) is only
+ # compiled when the build has Thrift support enabled, matching src/BUILD.
+ "//bazel/config:brpc_with_thrift": ["-DENABLE_THRIFT_FRAMED_PROTOCOL=1"],
+ "//conditions:default": [],
+})
Review Comment:
removed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Contain malformed Thrift T_EXCEPTION replies on the client (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright merged PR #3500:
URL: https://github.com/apache/brpc/pull/3500
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Contain malformed Thrift T_EXCEPTION replies on the client (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3500:
URL: https://github.com/apache/brpc/pull/3500#discussion_r3885184926
##########
test/BUILD.bazel:
##########
@@ -24,7 +24,12 @@ COPTS = [
"-Wno-unused-parameter",
"-fno-omit-frame-pointer",
"-fno-access-control",
-]
+] + select({
+ # The thrift framed protocol (and the unit test that exercises it) is only
+ # compiled when the build has Thrift support enabled, matching src/BUILD.
+ "//bazel/config:brpc_with_thrift": ["-DENABLE_THRIFT_FRAMED_PROTOCOL=1"],
+ "//conditions:default": [],
+})
Review Comment:
It seems this select is not needed. unit test targets will inherit this definition of bRPC source targets automatically.
https://github.com/apache/brpc/blob/4a429cdc2153eff616902edfd2349823d6d7c5b5/BUILD.bazel#L45-L48
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org