From 1bf809d026108d7904fc6f2b16879f6593c04d4e Mon Sep 17 00:00:00 2001 From: ljccjlljc <939159710@qq.com> Date: Thu, 25 Jun 2026 00:18:05 +0800 Subject: [PATCH] Fix StreamCreate failure handling --- src/brpc/stream.cpp | 5 +++- test/brpc_stream_create_unittest.cpp | 34 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 test/brpc_stream_create_unittest.cpp diff --git a/src/brpc/stream.cpp b/src/brpc/stream.cpp index a2a106a8b1..01504d3df6 100644 --- a/src/brpc/stream.cpp +++ b/src/brpc/stream.cpp @@ -837,7 +837,10 @@ int StreamCreate(StreamId *request_stream, Controller &cntl, return -1; } StreamIds request_streams; - StreamCreate(request_streams, 1, cntl, options); + const int rc = StreamCreate(request_streams, 1, cntl, options); + if (rc != 0) { + return rc; + } *request_stream = request_streams[0]; return 0; } diff --git a/test/brpc_stream_create_unittest.cpp b/test/brpc_stream_create_unittest.cpp new file mode 100644 index 0000000000..45bab8c073 --- /dev/null +++ b/test/brpc_stream_create_unittest.cpp @@ -0,0 +1,34 @@ +// 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. + +#include + +#include "brpc/controller.h" +#include "brpc/stream.h" + +class StreamCreateBugTest : public testing::Test {}; + +TEST_F(StreamCreateBugTest, create_twice_on_same_controller_returns_error) { + brpc::Controller cntl; + + brpc::StreamId first_stream = brpc::INVALID_STREAM_ID; + ASSERT_EQ(0, brpc::StreamCreate(&first_stream, cntl, NULL)); + + brpc::StreamId second_stream = brpc::INVALID_STREAM_ID; + ASSERT_EQ(-1, brpc::StreamCreate(&second_stream, cntl, NULL)); + ASSERT_EQ(brpc::INVALID_STREAM_ID, second_stream); +}