You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by "wasphin (via GitHub)" <gi...@apache.org> on 2026/09/07 15:15:55 UTC

[PR] Fix stream count test cleanup (brpc)

wasphin opened a new pull request, #3531:
URL: https://github.com/apache/brpc/pull/3531

   ### What problem does this PR solve?
   
   Issue Number: resolve
   
   Problem Summary:
   
   `StreamingRpcTest.limit_streams_accepted_per_request` can hang while joining
   the server. The service discards the response stream identifiers returned by
   `StreamAccept`, so cleanup depends on the timing of close frames from the
   client side.
   
   ### What is changed and the side effects?
   
   Changed:
   
   - Keep the accepted response stream identifiers in the test service.
   - Explicitly close both response and request streams after each request.
   
   Side effects:
   - Performance effects: None.
   
   - Breaking backward compatibility: No. This only fixes unit-test cleanup.
   
   ---
   ### Check List:
   - Please make sure your changes are compilable.
   - When providing us with a new feature, it is best to add related tests.
   - Please follow [Contributor Covenant Code of Conduct](https://github.com/apache/brpc/blob/master/CODE_OF_CONDUCT.md).
   


-- 
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] Fix stream count test cleanup (brpc)

Posted by "wasphin (via GitHub)" <gi...@apache.org>.
wasphin commented on code in PR #3531:
URL: https://github.com/apache/brpc/pull/3531#discussion_r3951461861


##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1388,7 +1388,7 @@ class MyServiceWithStreamCountLimit : public test::EchoService {
         brpc::Controller* cntl = static_cast<brpc::Controller*>(controller);
         response->set_message(request->message());
 
-        brpc::StreamIds response_streams;
+        response_streams.clear();
         accept_result.store(
             brpc::StreamAccept(response_streams, *cntl, nullptr),
             std::memory_order_release);

Review Comment:
   Done in bcba685d. `Echo` now collects the accepted ids into a local `brpc::StreamIds` and appends them to a private `_response_streams` under a `bthread::Mutex`; the per-request `clear()` is gone.
   
   For the record, the race as described cannot happen in this test today. `stub.Echo(&cntl, &request, &response, nullptr)` is synchronous — `Channel::CallMethod` joins on the correlation id when `done == nullptr` (`src/brpc/channel.cpp:656`) — so there is never more than one in-flight request and `Echo` is not re-entered concurrently. The cross-thread read was also already ordered by the release/acquire pair on `accepted_streams`.
   
   The change is still worth making: it makes the ordering locally evident instead of resting on an argument about the RPC round trip, and it stays correct if this test ever grows an async call or a second concurrent client.
   



##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1446,6 +1447,9 @@ TEST_F(StreamingRpcTest, limit_streams_accepted_per_request) {
                               std::memory_order_acquire));
         }
 
+        for (brpc::StreamId stream_id : service.response_streams) {
+            brpc::StreamClose(stream_id);
+        }
         for (brpc::StreamId stream_id : request_streams) {
             brpc::StreamClose(stream_id);

Review Comment:
   Done in bcba685d — both the response and the request close loops now use `ASSERT_EQ(0, brpc::StreamClose(stream_id))`.
   
   One clarification on the framing: `StreamClose` cannot currently fail. It forwards to `Stream::SetFailed` -> `SetFailedV`, which returns 0 on every path, including the "don't care recycled stream" early return (`src/brpc/stream.cpp:879-888`, `982-984`). So the assertion is not guarding against a masked failure — it pins the contract that close must return 0 here.
   



##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1398,6 +1398,7 @@ class MyServiceWithStreamCountLimit : public test::EchoService {
 
     std::atomic<int> accept_result{0};
     std::atomic<size_t> accepted_streams{0};
+    brpc::StreamIds response_streams;

Review Comment:
   Done in bcba685d. `_response_streams` is private now, and the test drains it through `TakeResponseStreams()`, which swaps the vector out under the same `bthread::Mutex` that `Echo` takes when appending.
   



-- 
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] Fix stream count test cleanup (brpc)

Posted by "wasphin (via GitHub)" <gi...@apache.org>.
wasphin commented on code in PR #3531:
URL: https://github.com/apache/brpc/pull/3531#discussion_r3951461861


##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1388,7 +1388,7 @@ class MyServiceWithStreamCountLimit : public test::EchoService {
         brpc::Controller* cntl = static_cast<brpc::Controller*>(controller);
         response->set_message(request->message());
 
-        brpc::StreamIds response_streams;
+        response_streams.clear();
         accept_result.store(
             brpc::StreamAccept(response_streams, *cntl, nullptr),
             std::memory_order_release);

Review Comment:
   Reworked in 0cdc618a. This test intentionally has one client, one server, and synchronous  calls, so only one request can be in flight and  cannot be re-entered concurrently. Adding concurrency handling only for the stream-id container would be incomplete because , , and the request-side assertions are also single-request state. The lock-based follow-up was therefore removed, keeping the test aligned with its actual scope.



##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1446,6 +1447,9 @@ TEST_F(StreamingRpcTest, limit_streams_accepted_per_request) {
                               std::memory_order_acquire));
         }
 
+        for (brpc::StreamId stream_id : service.response_streams) {
+            brpc::StreamClose(stream_id);
+        }
         for (brpc::StreamId stream_id : request_streams) {
             brpc::StreamClose(stream_id);

Review Comment:
   The close-result assertions are retained in 0cdc618a for both response and request streams.  currently returns 0 on every  path, so these assertions pin the expected cleanup contract rather than changing synchronization behavior.



##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1398,6 +1398,7 @@ class MyServiceWithStreamCountLimit : public test::EchoService {
 
     std::atomic<int> accept_result{0};
     std::atomic<size_t> accepted_streams{0};
+    brpc::StreamIds response_streams;

Review Comment:
   Reworked in 0cdc618a. The public  member is retained as test fixture state because this test deliberately executes one synchronous request at a time. Introducing a private locked accessor would suggest partial concurrent-request support while the other observed fields and assertions remain single-request state.



-- 
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] Fix stream count test cleanup (brpc)

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3531:
URL: https://github.com/apache/brpc/pull/3531#discussion_r3951092126


##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1446,6 +1447,9 @@ TEST_F(StreamingRpcTest, limit_streams_accepted_per_request) {
                               std::memory_order_acquire));
         }
 
+        for (brpc::StreamId stream_id : service.response_streams) {
+            brpc::StreamClose(stream_id);
+        }
         for (brpc::StreamId stream_id : request_streams) {
             brpc::StreamClose(stream_id);

Review Comment:
   The return value from `brpc::StreamClose` is ignored, which can mask cleanup failures and make the test flaky/harder to diagnose. Consider asserting success (or at least logging/asserting in debug) for both response and request stream closes so failures are immediately visible.



##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1388,7 +1388,7 @@ class MyServiceWithStreamCountLimit : public test::EchoService {
         brpc::Controller* cntl = static_cast<brpc::Controller*>(controller);
         response->set_message(request->message());
 
-        brpc::StreamIds response_streams;
+        response_streams.clear();
         accept_result.store(
             brpc::StreamAccept(response_streams, *cntl, nullptr),
             std::memory_order_release);

Review Comment:
   `response_streams` is now shared state on the service instance and is mutated per-RPC. If the server handles multiple requests concurrently, this introduces a data race (concurrent `clear()`/writes by `StreamAccept`). Consider making the stored IDs per-request (e.g., keep a local `brpc::StreamIds` and then append/copy into a shared collection under a mutex), or guard `response_streams` with a lock.



##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1398,6 +1398,7 @@ class MyServiceWithStreamCountLimit : public test::EchoService {
 
     std::atomic<int> accept_result{0};
     std::atomic<size_t> accepted_streams{0};
+    brpc::StreamIds response_streams;

Review Comment:
   `response_streams` is a public data member on the service and is accessed directly by the test. To keep the service encapsulated (even in tests) and to reduce accidental misuse, consider making it private and exposing a small accessor (optionally returning a copy/snapshot under a lock if you address the concurrency concern).



-- 
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] Fix stream count test cleanup (brpc)

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3531:
URL: https://github.com/apache/brpc/pull/3531#discussion_r3953848944


##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1446,8 +1447,11 @@ TEST_F(StreamingRpcTest, limit_streams_accepted_per_request) {
                               std::memory_order_acquire));
         }
 
+        for (brpc::StreamId stream_id : service.response_streams) {
+            ASSERT_EQ(0, brpc::StreamClose(stream_id));
+        }
         for (brpc::StreamId stream_id : request_streams) {
-            brpc::StreamClose(stream_id);
+            ASSERT_EQ(0, brpc::StreamClose(stream_id));
         }

Review Comment:
   Stream cleanup is performed after multiple ASSERT_* checks. If any assertion fails earlier in the loop iteration, the test returns immediately and the streams stay open, which can reintroduce hangs/leaks during failure diagnostics. Consider adding RAII guards (brpc::ScopedStream) for both request and response stream IDs so they get closed even on early-return, while still asserting StreamClose succeeds by releasing the guards after explicit closes.



-- 
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] Fix stream count test cleanup (brpc)

Posted by "wasphin (via GitHub)" <gi...@apache.org>.
wasphin commented on code in PR #3531:
URL: https://github.com/apache/brpc/pull/3531#discussion_r3951461861


##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1388,7 +1388,7 @@ class MyServiceWithStreamCountLimit : public test::EchoService {
         brpc::Controller* cntl = static_cast<brpc::Controller*>(controller);
         response->set_message(request->message());
 
-        brpc::StreamIds response_streams;
+        response_streams.clear();
         accept_result.store(
             brpc::StreamAccept(response_streams, *cntl, nullptr),
             std::memory_order_release);

Review Comment:
   Reworked in 0cdc618a. This test intentionally has one client, one server, and synchronous stub.Echo(..., nullptr) calls, so only one request can be in flight and Echo cannot be re-entered concurrently. Adding concurrency handling only for the stream-id container would be incomplete because accept_result, accepted_streams, and the request-side assertions are also single-request state. The lock-based follow-up was therefore removed, keeping the test aligned with its actual scope.



##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1446,6 +1447,9 @@ TEST_F(StreamingRpcTest, limit_streams_accepted_per_request) {
                               std::memory_order_acquire));
         }
 
+        for (brpc::StreamId stream_id : service.response_streams) {
+            brpc::StreamClose(stream_id);
+        }
         for (brpc::StreamId stream_id : request_streams) {
             brpc::StreamClose(stream_id);

Review Comment:
   The close-result assertions are retained in 0cdc618a for both response and request streams. StreamClose currently returns 0 on every Stream::SetFailedV path, so these assertions pin the expected cleanup contract rather than changing synchronization behavior.



##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1398,6 +1398,7 @@ class MyServiceWithStreamCountLimit : public test::EchoService {
 
     std::atomic<int> accept_result{0};
     std::atomic<size_t> accepted_streams{0};
+    brpc::StreamIds response_streams;

Review Comment:
   Reworked in 0cdc618a. The public response_streams member is retained as test fixture state because this test deliberately executes one synchronous request at a time. Introducing a private locked accessor would suggest partial concurrent-request support while the other observed fields and assertions remain single-request state.



##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1394,10 +1394,25 @@ class MyServiceWithStreamCountLimit : public test::EchoService {
             std::memory_order_release);
         accepted_streams.store(
             response_streams.size(), std::memory_order_release);

Review Comment:
   Reworked in 0cdc618a by removing the lock-based state entirely. This test has one client, one server, and synchronous stub.Echo(..., nullptr) calls, so only one request can be in flight. Extending concurrency handling only to the stream-id container would be misleading: accept_result, accepted_streams, and the request-side assertions are also deliberately single-request state. The test now keeps that explicit scope and only asserts both StreamClose calls.



-- 
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] Fix stream count test cleanup (brpc)

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3531:
URL: https://github.com/apache/brpc/pull/3531#discussion_r3952285370


##########
test/brpc_streaming_rpc_unittest.cpp:
##########
@@ -1394,10 +1394,25 @@ class MyServiceWithStreamCountLimit : public test::EchoService {
             std::memory_order_release);
         accepted_streams.store(
             response_streams.size(), std::memory_order_release);

Review Comment:
   `accept_result`/`accepted_streams` are published (release) before `_response_streams` is updated under `_mutex`. A thread that observes `accepted_streams > 0` (acquire) and then calls `TakeResponseStreams()` can still race and drain an empty vector (because the append hasn’t happened yet), leaving response streams unclosed until the next drain. To make the synchronization scheme consistent, append to `_response_streams` first, then do the release stores (or protect the atomics with the same mutex).



-- 
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] Fix stream count test cleanup (brpc)

Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright merged PR #3531:
URL: https://github.com/apache/brpc/pull/3531


-- 
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