Copilot commented on code in PR #3720:
URL: https://github.com/apache/brpc/pull/3720#discussion_r4545828947


##########
src/brpc/rdma/rdma_helper.cpp:
##########
@@ -135,6 +138,14 @@ static void GlobalRelease() {
 
     RdmaEndpoint::GlobalRelease();
 
+    SrqReceivePool::RetryDeregistrations();
+    if (SrqWorkerCount() != 0 || SrqReceivePool::RegisteredBytes() != 0) {

Review Comment:
   `StopAllSrqWorkers()` releases the worker's pool reference, but an in-flight 
growth bthread can still retain the `SrqReceivePool` and execute 
`PrepareRegion()`/`IbvRegMr()` after this returns. In that state 
`SrqWorkerCount()` can already be zero and `RegisteredBytes()` can still be 
zero because registration has not started, so this path closes the RDMA 
context/PD while the growth task still has access to them. Include 
`SrqReceivePool::LivePools()` (or otherwise wait for all growth-task 
references) in the retention condition before releasing the RDMA context.



##########
test/rdma_test_helper.h:
##########
@@ -0,0 +1,255 @@
+// 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.
+
+#ifndef BRPC_TEST_RDMA_TEST_HELPER_H
+#define BRPC_TEST_RDMA_TEST_HELPER_H
+
+#include <unistd.h>
+#include <functional>
+#include <iostream>
+#include <string>
+#include <vector>
+#include <gtest/gtest.h>
+#include "butil/atomicops.h"
+#include "butil/endpoint.h"
+#include "butil/files/temp_file.h"
+#include "butil/iobuf.h"
+#include "butil/time.h"
+#include "brpc/acceptor.h"
+#include "brpc/channel.h"
+#include "brpc/closure_guard.h"
+#include "brpc/controller.h"
+#include "brpc/server.h"
+#include "brpc/socket.h"
+#include "brpc/rdma/block_pool.h"
+#include "brpc/rdma_transport.h"
+#include "echo.pb.h"
+
+namespace brpc {
+namespace rdma_test {
+
+static std::string g_ip = "127.0.0.1";
+static butil::EndPoint g_ep;
+
+// Number of Echo requests the server has actually served. The churn tests 
below
+// cut the connection while requests are in flight, and from the client side
+// "the server never saw this request" is indistinguishable from "the server
+// answered it and the reply died with the connection" -- both just look like a
+// failed RPC. Only this counter tells the two apart, and it is the server 
having
+// real work in flight that makes the race those tests hunt reachable at all.
+static butil::atomic<int> g_echo_served(0);
+
+// The server side runs in its own threads, so the only thing a test can rely 
on
+// is that an expected transition happens *eventually*. Polling with a generous
+// upper bound keeps the common case as fast as the machine allows and does not
+// turn into a flake when CI is loaded, which a fixed sleep does.
+static const int64_t WAIT_TIMEOUT_US = 5000000;
+static const int64_t WAIT_INTERVAL_US = 1000;
+
+// Returns true if `pred` turned true before the timeout expired.
+static bool WaitUntil(const std::function<bool()>& pred,
+                      int64_t timeout_us = WAIT_TIMEOUT_US) {
+    const int64_t deadline = butil::gettimeofday_us() + timeout_us;
+    while (!pred()) {
+        if (butil::gettimeofday_us() >= deadline) {
+            return false;
+        }
+        usleep((useconds_t)WAIT_INTERVAL_US);
+    }
+    return true;
+}
+
+class MyEchoService : public ::test::EchoService {
+    void Echo(google::protobuf::RpcController* cntl_base,
+              const ::test::EchoRequest* req,
+              ::test::EchoResponse* res,
+              google::protobuf::Closure* done) {
+        Controller* cntl = static_cast<Controller*>(cntl_base);
+        ClosureGuard done_guard(done);
+        g_echo_served.fetch_add(1, butil::memory_order_relaxed);
+        if (req->server_fail()) {
+            cntl->SetFailed(req->server_fail(), "Server fail1");
+            cntl->SetFailed(req->server_fail(), "Server fail2");
+            return;
+        }
+        if (req->close_fd()) {
+            usleep(1);
+            LOG(INFO) << "close fd...";
+            cntl->CloseConnection("Close connection according to request");
+            return;
+        }
+        if (req->sleep_us() > 0) {
+            LOG(INFO) << "sleep " << req->sleep_us() << "us...";
+            bthread_usleep(req->sleep_us());
+        }
+        res->set_message("MyEchoService");
+        if (req->code() != 0) {
+            res->add_code_list(req->code());
+        }
+        cntl->response_attachment().append(cntl->request_attachment());
+    }
+};
+
+class RdmaTest : public ::testing::Test {
+protected:
+    explicit RdmaTest(int port = 8713) {
+        butil::ip_t ip;
+        EXPECT_EQ(0, butil::str2ip(g_ip.c_str(), &ip));
+        butil::EndPoint ep(ip, port);
+        g_ep = ep;
+        EXPECT_EQ(0, _server_list.save(butil::endpoint2str(g_ep).c_str()));
+        _naming_url = std::string("File://") + _server_list.fname();
+        _server.AddService(&_svc, SERVER_DOESNT_OWN_SERVICE);
+    }
+
+    ~RdmaTest() { }
+
+    virtual void SetUp() { }
+
+    virtual void TearDown() {
+        rdma::DumpMemoryPoolInfo(std::cout);
+    }
+
+protected:
+    void StartServer(bool use_rdma = true) {
+        ServerOptions options;
+        options.enabled_protocols = "baidu_std";
+        options.socket_mode = use_rdma ? SOCKET_MODE_RDMA : SOCKET_MODE_TCP;
+        options.idle_timeout_sec = 5;
+        options.max_concurrency = 0;
+        options.internal_port = -1;
+        EXPECT_EQ(0, _server.Start(g_ep.port, &options));
+    }
+
+    void StopServer() {
+        _server.Stop(0);
+        _server.Join();
+    }
+
+    Socket* GetSocketFromServer(size_t index) {
+        std::vector<SocketId> sids;
+        _server._am->ListConnections(&sids);
+        if (index >= sids.size()) {
+            return nullptr;
+        }
+        SocketUniquePtr s;
+        if (Socket::Address(sids[index], &s) == 0) {
+            return s.get();
+        }
+        return nullptr;

Review Comment:
   `Socket::Address` returns a `SocketUniquePtr`, but this helper discards that 
owner before returning `s.get()`. Every direct caller (and 
`WaitForServerSocket`) therefore receives a raw pointer after the Socket 
generation reference has been released; the object can be recycled while the 
test uses it, causing use-after-recycle races in the churn/close tests. Return 
a `SocketUniquePtr` and retain it at each call site instead of exposing 
`Socket*`.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to