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

chenBright 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 e3ac4165f Support automatic RDMA handshake fallback from v3 to v2 
(#3745)
e3ac4165f is described below

commit e3ac4165f2004cb072479d15cb571e25790ed9bd
Author: Bright Chen <[email protected]>
AuthorDate: Tue Nov 17 20:16:29 2026 +0800

    Support automatic RDMA handshake fallback from v3 to v2 (#3745)
    
    * Support automatic RDMA handshake fallback from v3 to v2
    
    * Fix RDMA handshake fd replacement racing with socket failure
---
 docs/cn/rdma.md                  |   3 +
 docs/en/rdma.md                  |   3 +
 src/brpc/rdma/rdma_endpoint.cpp  | 201 ++++++++----
 src/brpc/rdma/rdma_endpoint.h    |  19 +-
 src/brpc/rdma/rdma_handshake.cpp |  24 +-
 src/brpc/rdma/rdma_handshake.h   |  12 +-
 src/brpc/socket.cpp              |  29 +-
 src/brpc/socket.h                |  14 +-
 src/brpc/tcp_transport.cpp       |   2 +-
 src/brpc/ubshm/ub_endpoint.cpp   |  13 +-
 src/brpc/urma/urma_endpoint.cpp  |   5 +-
 test/brpc_rdma_unittest.cpp      | 676 +++++++++++++++++++++++++++++++--------
 test/brpc_socket_unittest.cpp    | 193 +++++++++++
 13 files changed, 969 insertions(+), 225 deletions(-)

diff --git a/docs/cn/rdma.md b/docs/cn/rdma.md
index fd70686bb..00dda37a4 100644
--- a/docs/cn/rdma.md
+++ b/docs/cn/rdma.md
@@ -35,6 +35,8 @@ RDMA与TCP不同,不使用socket接口进行通信。但是在实现上仍然
 
 brpc内部使用RDMA 
RC模式,每个RdmaEndpoint对应一个QP。RDMA连接建立依赖于前置TCP建连,TCP建连后双方交换必要参数,如GID、QPN等,再发起RDMA连接并实现数据传输。这个过程我们称为握手(参见RdmaEndpoint)。因为握手需要TCP连接,因此RdmaEndpoint所在的Socket类中,原本的TCP
 
fd仍然有效。握手过程采用了brpc中已有的AppConnect逻辑。注意,握手用的TCP连接在后续数据传输阶段并不会收发数据,但仍保持为EST状态。一旦TCP连接中断,其上对应的RDMA连接同样会置错。
 
+客户端每次新建逻辑连接都先使用 v3 握手,无需配置版本。如果服务端在返回 v3 标识之前断开或重置连接,或返回 v2 标识,客户端会新建 TCP 连接并用 
v2 重试一次。TCP 建连、握手及重连共用原始连接截止时间。已经识别为 v3 的报文损坏、握手超时或 v2 重试失败时,连接失败;服务端正常要求回退 TCP 
时,客户端继续使用当前连接。
+
 
RdmaEndpoint数据传输逻辑的第一个重要特性是零拷贝。要发送的所有数据默认都存放在IOBuf的Block中,因此所发送的Block需要等到发送CQE触发后才可以释放,这些Block的引用被存放于RdmaEndpoint::_sbuf中。而要实现接收零拷贝,则需要确保接受端所预提交的接收缓冲区必须直接在IOBuf的Block里面,被存放于RdmaEndpoint::_rbuf。注意,接收端预提交的每一段Block,有一个固定的大小(recv_block_size)。发送端发送时,一个请求最多只能有这么大,否则接收端则无法成功接收。
 
 
RdmaEndpoint数据传输逻辑的第二个重要特性是滑动窗口流控。这一流控机制是为了避免发送端持续在发送,其速度超过了接收端处理的速度。TCP传输中也有类似的逻辑,但是是由内核协议栈来实现的。RdmaEndpoint内实现了这一流控机制,通过接收端显式回复ACK来确认接收端处理完毕。为了减少ACK本身的开销,让ACK以立即数形式返回,可以被附在数据消息里。
@@ -66,6 +68,7 @@ RDMA支持事件驱动和轮询两种模式,默认是事件驱动模式,通
 
 可配置参数说明:
 * rdma_trace_verbose: 日志中打印RDMA建连相关信息,默认false。
+* rdma_client_handshake_version: 已废弃,仅为兼容旧启动参数保留。该值不再参与版本选择;客户端始终先尝试 v3,并在对端拒绝 
v3 后重连并用 v2 重试一次。
 * rdma_recv_zerocopy: 是否启用接收零拷贝,默认true。
 * rdma_zerocopy_min_size: 接收零拷贝最小的msg大小,默认512B。
 * rdma_recv_block_type: 
为接收数据预准备的block类型,分为三类default(8KB)/large(64KB)/huge(2MB),默认为default。
diff --git a/docs/en/rdma.md b/docs/en/rdma.md
index 98ac6981b..f45d42b49 100644
--- a/docs/en/rdma.md
+++ b/docs/en/rdma.md
@@ -35,6 +35,8 @@ RDMA does not use socket API like TCP. However, the 
brpc::Socket class is still
 
 brpc uses RDMA RC mode. Every RdmaEndpoint has its own QP. Before establishing 
RDMA connection, a TCP connection is necessary to exchange some information 
such as GID and QPN. We call this procedure handshake. Since handshake needs 
TCP connection, the TCP fd in the corresponding Socket is still valid. The 
handshake procedure is completed in the AppConnect way in brpc. The TCP 
connection will keep in EST state but not be used for data transmission after 
RDMA connection is established. Onc [...]
 
+Each new logical connection starts with handshake v3, with no version 
configuration required. If the server closes or resets the connection before 
returning v3 magic, or replies with v2 magic, the client reconnects and retries 
v2 once. TCP connection establishment, the handshake, and reconnection share 
the original connection deadline. Malformed replies identified as v3, handshake 
timeouts, and failed v2 retries fail the connection. A normal server request to 
fall back to TCP continues o [...]
+
 The first key feature in RdmaEndpoint data transmission is zero copy. All data 
which need to transmit is in the Blocks of IOBuf. Thus all the Blocks need to 
be released after the sent CQEs are triggered. The reference of these Blocks 
are stored in RdmaEndpoint::_sbuf. In order to realize receiving zero copy, the 
receive side must post receive buffers in Blocks of IOBuf, which are stored in 
RdmaEndpoint::_rbuf. Note that all the Blocks posted in the receive side has a 
fixed size (recv_blo [...]
 
 The second key feature in RdmaEndpoint data transmission is sliding window 
flow control. The flow control is to avoid fast transmit side overwhelming slow 
receive side. TCP has similar mechanism in kernel TCP stack. RdmaEndpoint 
implements this mechanism with explicit ACKs from receive side. to reduce the 
overhead of ACKs, the ACK number can be piggybacked in ordinary data message as 
immediate data.
@@ -64,6 +66,7 @@ Examples:
 
 Configurable parameters:
 * rdma_trace_verbose: to print RDMA connection information in log,default is 
false.
+* rdma_client_handshake_version: deprecated and ignored. Retained for 
compatibility with existing command-line arguments. Clients always start with 
v3 and reconnect to retry v2 once if the peer rejects v3.
 * rdma_recv_zerocopy: enable zero copy in receive side,default is true.
 * rdma_zerocopy_min_size: the min message size for receive zero copy (in 
Byte),default is 512.
 * rdma_recv_block_type: the block type used for receiving, can be 
default(8KB)/large(64KB)/huge(2MB),default is default.
diff --git a/src/brpc/rdma/rdma_endpoint.cpp b/src/brpc/rdma/rdma_endpoint.cpp
index 110279b70..a2cea5575 100644
--- a/src/brpc/rdma/rdma_endpoint.cpp
+++ b/src/brpc/rdma/rdma_endpoint.cpp
@@ -19,6 +19,9 @@
 
 #include <algorithm>                            // std::min
 #include <gflags/gflags.h>
+
+#include <memory>
+#include "butil/fd_guard.h"
 #include "butil/fd_utility.h"
 #include "butil/logging.h"                   // CHECK, LOG
 #include "butil/sys_byteorder.h"             // HostToNet,NetToHost
@@ -211,6 +214,7 @@ void RdmaConnect::StartConnect(const Socket* socket,
     CHECK(rdma_transport->_rdma_ep != nullptr);
     SocketUniquePtr s;
     if (Socket::Address(socket->id(), &s) != 0) {
+        done(EFAILEDSOCKET, data);
         return;
     }
     if (!IsRdmaAvailable()) {
@@ -225,11 +229,12 @@ void RdmaConnect::StartConnect(const Socket* socket,
     bthread_t tid;
     bthread_attr_t attr = BTHREAD_ATTR_NORMAL;
     bthread_attr_set_name(&attr, "RdmaProcessHandshakeAtClient");
-    if (bthread_start_background(&tid, &attr,
-                                 RdmaEndpoint::ProcessHandshakeAtClient,
-                                 rdma_transport->_rdma_ep) < 0) {
-        LOG(FATAL) << "Fail to start handshake bthread";
-        Run();
+    int rc = bthread_start_background(&tid, &attr,
+                                      RdmaEndpoint::ProcessHandshakeAtClient,
+                                      rdma_transport->_rdma_ep);
+    if (rc != 0) {
+        LOG(ERROR) << "Fail to start handshake bthread: " << berror(rc);
+        Run(rc);
     } else {
         s.release();
     }
@@ -237,8 +242,8 @@ void RdmaConnect::StartConnect(const Socket* socket,
 
 void RdmaConnect::StopConnect(Socket* socket) { }
 
-void RdmaConnect::Run() {
-    _done(errno, _data);
+void RdmaConnect::Run(int error_code) {
+    _done(error_code, _data);
 }
 
 void RdmaEndpoint::OnNewDataFromTcp(Socket* s) {
@@ -350,6 +355,25 @@ bool RdmaEndpoint::HandleTcpEventAfterEstablished() {
 
 static const int WAIT_TIMEOUT_MS = 50;
 
+static int GetHandshakeWaitTime(Socket* socket, int64_t deadline_us,
+                                timespec* duetime) {
+    if (socket->Failed()) {
+        errno = ECANCELED;
+        return -1;
+    }
+    int64_t now_us = butil::gettimeofday_us();
+    if (deadline_us >= 0 && now_us >= deadline_us) {
+        errno = ETIMEDOUT;
+        return -1;
+    }
+    int64_t wait_until_us = now_us + WAIT_TIMEOUT_MS * 1000L;
+    if (deadline_us >= 0) {
+        wait_until_us = std::min(wait_until_us, deadline_us);
+    }
+    *duetime = butil::microseconds_to_timespec(wait_until_us);
+    return 0;
+}
+
 // Drive an EAGAIN-aware read loop to completion (exactly `len` bytes).
 // `read_once(offset, remaining)` performs ONE underlying read attempt:
 //   returns > 0  : number of bytes consumed (added to running total);
@@ -366,12 +390,16 @@ static const int WAIT_TIMEOUT_MS = 50;
 // mapped, scatter-vector, etc.) can plug in by passing its own
 // `read_once`.
 template <class ReadOnce>
-static int ReadFromFdLoop(butil::atomic<int>* read_butex,
+static int ReadFromFdLoop(Socket* socket, int64_t deadline_us,
+                          butil::atomic<int>* read_butex,
                           size_t len, ReadOnce&& read_once) {
     size_t received = 0;
     while (received < len) {
-        const int expected_val = read_butex->load(butil::memory_order_acquire);
-        const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+        int expected_val = read_butex->load(butil::memory_order_acquire);
+        timespec duetime;
+        if (GetHandshakeWaitTime(socket, deadline_us, &duetime) != 0) {
+            return -1;
+        }
         ssize_t nr = read_once(received, len - received);
         if (nr < 0) {
             if (errno == EAGAIN) {
@@ -395,8 +423,8 @@ static int ReadFromFdLoop(butil::atomic<int>* read_butex,
 
 int RdmaEndpoint::ReadFromFd(void* data, size_t len) {
     CHECK(data != nullptr);
-    const int fd = _socket->fd();
-    return ReadFromFdLoop(_read_butex, len,
+    int fd = _socket->fd();
+    return ReadFromFdLoop(_socket, _socket->_connect_deadline_us, _read_butex, 
len,
         [data, fd](size_t offset, size_t remaining) {
             return read(fd, (uint8_t*)data + offset, remaining);
         });
@@ -404,8 +432,8 @@ int RdmaEndpoint::ReadFromFd(void* data, size_t len) {
 
 int RdmaEndpoint::ReadFromFd(butil::IOPortal* data, size_t len) {
     CHECK(data != nullptr);
-    const int fd = _socket->fd();
-    return ReadFromFdLoop(_read_butex, len,
+    int fd = _socket->fd();
+    return ReadFromFdLoop(_socket, _socket->_connect_deadline_us, _read_butex, 
len,
         [data, fd](size_t /*offset*/, size_t remaining) {
             return data->append_from_file_descriptor(fd, remaining);
         });
@@ -426,10 +454,14 @@ int RdmaEndpoint::ReadFromFd(butil::IOPortal* data, 
size_t len) {
 // becomes writable again. It returns 0 on wake-up (or ETIMEDOUT),
 // non-zero on hard failure.
 template <class WriteOnce, class WaitWritable>
-static int WriteToFdLoop(size_t len, WriteOnce&& write_once, WaitWritable&& 
wait_writable) {
+static int WriteToFdLoop(Socket* socket, int64_t deadline_us, size_t len,
+                         WriteOnce&& write_once, WaitWritable&& wait_writable) 
{
     size_t written = 0;
     while (written < len) {
-        const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+        timespec duetime;
+        if (GetHandshakeWaitTime(socket, deadline_us, &duetime) != 0) {
+            return -1;
+        }
         ssize_t nw = write_once(written, len - written);
         if (nw >= 0) {
             written += nw;
@@ -449,29 +481,83 @@ static int WriteToFdLoop(size_t len, WriteOnce&& 
write_once, WaitWritable&& wait
 int RdmaEndpoint::WriteToFd(void* data, size_t len) {
     CHECK(data != nullptr);
     Socket* s = _socket;
-    const int fd = s->fd();
-    return WriteToFdLoop(len,
+    int fd = s->fd();
+    return WriteToFdLoop(s, s->_connect_deadline_us, len,
         [data, fd](size_t offset, size_t remaining) {
             return write(fd, (uint8_t*)data + offset, remaining);
         },
         [s, fd](const timespec* duetime) {
-            return s->WaitEpollOut(fd, true, duetime) == 0 || errno == 
ETIMEDOUT;
+            return s->WaitEpollOut(fd, true, duetime, false) == 0 ||
+                   errno == ETIMEDOUT;
         });
 }
 
 int RdmaEndpoint::WriteToFd(butil::IOBuf* data) {
     CHECK(data != nullptr);
     Socket* s = _socket;
-    const int fd = s->fd();
-    return WriteToFdLoop(data->size(),
+    int fd = s->fd();
+    return WriteToFdLoop(s, s->_connect_deadline_us, data->size(),
         [data, fd](size_t /*offset*/, size_t /*remaining*/) {
             return data->cut_into_file_descriptor(fd);
         },
         [s, fd](const timespec* duetime) {
-            return s->WaitEpollOut(fd, true, duetime) == 0 || errno == 
ETIMEDOUT;
+            return s->WaitEpollOut(fd, true, duetime, false) == 0 ||
+                   errno == ETIMEDOUT;
         });
 }
 
+int RdmaEndpoint::ReconnectForHandshake() {
+    Socket* s = _socket;
+    timespec duetime;
+    if (GetHandshakeWaitTime(s, s->_connect_deadline_us, &duetime) != 0) {
+        return -1;
+    }
+    timespec deadline = 
butil::microseconds_to_timespec(s->_connect_deadline_us);
+    // Use Socket::Connect to preserve the configured device/local address and
+    // socket options. Keep ownership of the old fd until replacement succeeds.
+    butil::fd_guard new_fd(s->Connect(
+        s->_connect_deadline_us >= 0 ? &deadline : nullptr,
+        nullptr, nullptr, /*check_failed=*/true));
+    if (new_fd < 0 ||
+        GetHandshakeWaitTime(s, s->_connect_deadline_us, &duetime) != 0) {
+        return -1;
+    }
+    return ReplaceHandshakeFd(new_fd.release());
+}
+
+int RdmaEndpoint::ReplaceHandshakeFd(int fd) {
+    Socket* s = _socket;
+    butil::fd_guard new_fd(fd);
+    int old_fd = s->fd();
+    if (s->_transport->HasOnEdgeTrigger()) {
+        s->_io_event.RemoveConsumer(old_fd);
+    }
+    if (s->ResetFileDescriptor(new_fd) != 0) {
+        // ResetFileDescriptor clears `_fd` on error. Retain the old fd so 
normal
+        // Socket recycling closes it and balances the connection counter.
+        s->_fd.store(old_fd, butil::memory_order_release);
+        return -1;
+    }
+    // SetFailed() can race with the earlier checks or fd setup. Validate
+    // after registration, before committing ownership of the new connection.
+    // The handshake's Socket reference prevents recycling or health-check
+    // reset while we undo the replacement.
+    timespec duetime;
+    if (GetHandshakeWaitTime(s, s->_connect_deadline_us, &duetime) != 0) {
+        int saved_errno = errno;
+        if (s->_transport->HasOnEdgeTrigger()) {
+            s->_io_event.RemoveConsumer(new_fd);
+        }
+        s->_fd.store(old_fd, butil::memory_order_release);
+        new_fd.reset(-1);
+        errno = saved_errno;
+        return -1;
+    }
+    new_fd.release();
+    close(old_fd);
+    return 0;
+}
+
 void RdmaEndpoint::ApplyRemoteHello(const ParsedHello& remote) {
     _remote_recv_block_size = remote.block_size;
     _local_window_capacity = std::min(_sq_size, remote.rq_size) - 
RESERVED_WR_NUM;
@@ -502,14 +588,16 @@ void RdmaEndpoint::ApplyRemoteHello(const ParsedHello& 
remote) {
 void* RdmaEndpoint::ProcessHandshakeAtClient(void* arg) {
     auto ep = static_cast<RdmaEndpoint*>(arg);
     SocketUniquePtr s(ep->_socket);
+    // Let AfterAppConnected record the final error after applying connection
+    // timeout policy. SetFailed() here would prevent that error conversion.
     RdmaConnect::RunGuard rg((RdmaConnect*)s->_app_connect.get());
     auto rdma_transport = static_cast<RdmaTransport*>(s->_transport.get());
 
     LOG_IF(INFO, FLAGS_rdma_trace_verbose)
         << "Start handshake on " << s->description();
 
-    std::unique_ptr<RdmaHandshake> handshake = CreateClientHandshake(ep);
-    CHECK(handshake != nullptr);
+    // Each new connection starts with v3 and retries v2 if the peer rejects 
it.
+    std::unique_ptr<RdmaHandshake> handshake(new RdmaHandshakeClientV3(ep));
     ep->_handshake_version = handshake->ProtocolVersion();
 
     // First initialize CQ and QP resources.
@@ -517,39 +605,50 @@ void* RdmaEndpoint::ProcessHandshakeAtClient(void* arg) {
     if (ep->AllocateResources() < 0) {
         PLOG(WARNING) << "Fail to allocate rdma resources, fallback to tcp:"
                       << s->description();
-        errno = 0;
         rdma_transport->_rdma_state = RdmaTransport::RDMA_OFF;
         ep->_state.store(FALLBACK_TCP, butil::memory_order_release);
         return nullptr;
     }
 
-    // Send hello message to server
-    ep->_state.store(C_HELLO_SEND, butil::memory_order_relaxed);
-    if (handshake->SendLocalHello() < 0) {
-        int saved_errno = errno;
-        PLOG(WARNING) << "Fail to send hello message to server:"
-                      << s->description();
-        s->SetFailed(saved_errno, "Fail to complete rdma handshake from %s: 
%s",
-                     s->description().c_str(), berror(saved_errno));
-        ep->_state.store(FAILED, butil::memory_order_relaxed);
-        return nullptr;
-    }
-
-    // Receive and parse remote hello.
-    ep->_state.store(C_HELLO_WAIT, butil::memory_order_relaxed);
     ParsedHello remote{};
-    const RemoteHelloResult r = handshake->ReceiveAndParseRemoteHello(&remote);
-    if (r == RemoteHelloResult::ERROR) {
-        int saved_errno = errno;
-        PLOG(WARNING) << "Fail to receive hello from server:"
+    RemoteHelloResult result;
+    for (;;) {
+        ep->_state.store(C_HELLO_SEND, butil::memory_order_relaxed);
+        if (handshake->SendLocalHello() < 0) {
+            result = RemoteHelloResult::ERROR;
+            if (handshake->ProtocolVersion() == 3 &&
+                (errno == EPIPE || errno == ECONNRESET)) {
+                result = RemoteHelloResult::RETRY_V2;
+            }
+        } else {
+            ep->_state.store(C_HELLO_WAIT, butil::memory_order_relaxed);
+            result = handshake->ReceiveAndParseRemoteHello(&remote);
+        }
+        if (result != RemoteHelloResult::RETRY_V2) {
+            break;
+        }
+
+        LOG_IF(INFO, FLAGS_rdma_trace_verbose)
+            << "Retry RDMA handshake with v2 on " << s->description();
+        if (ep->ReconnectForHandshake() != 0) {
+            result = RemoteHelloResult::ERROR;
+            break;
+        }
+        // No peer parameters have been applied and the QP is still in RESET.
+        // Reuse it, but discard the ECE advertisement of the v3 attempt.
+        ep->_outgoing_ece.reset();
+        handshake = std::make_unique<RdmaHandshakeClientV2>(ep);
+        ep->_handshake_version = handshake->ProtocolVersion();
+    }
+    if (result == RemoteHelloResult::ERROR) {
+        rg.error_code = errno ? errno : ERDMA;
+        PLOG(WARNING) << "Fail to exchange hello with server:"
                       << s->description();
-        s->SetFailed(saved_errno, "Fail to complete rdma handshake from %s: 
%s",
-                     s->description().c_str(), berror(saved_errno));
         ep->_state.store(FAILED, butil::memory_order_relaxed);
         return nullptr;
     }
 
-    if (r != RemoteHelloResult::NEGOTIATED) {
+    if (result != RemoteHelloResult::NEGOTIATED) {
         LOG(WARNING) << "Fail to negotiate with server, fallback to tcp:"
                      << s->description();
         rdma_transport->_rdma_state = RdmaTransport::RDMA_OFF;
@@ -571,11 +670,9 @@ void* RdmaEndpoint::ProcessHandshakeAtClient(void* arg) {
     uint32_t flags = rdma_on ? HELLO_ACK_RDMA_OK : 0;
     uint32_t flags_be = butil::HostToNet32(flags);
     if (ep->WriteToFd(&flags_be, HELLO_ACK_LEN) < 0) {
-        int saved_errno = errno;
+        rg.error_code = errno ? errno : ERDMA;
         PLOG(WARNING) << "Fail to send Ack Message to server:"
                       << s->description();
-        s->SetFailed(saved_errno, "Fail to complete rdma handshake from %s: 
%s",
-                     s->description().c_str(), berror(saved_errno));
         ep->_state.store(FAILED, butil::memory_order_relaxed);
         return nullptr;
     }
@@ -584,10 +681,8 @@ void* RdmaEndpoint::ProcessHandshakeAtClient(void* arg) {
         ep->_state.store(ESTABLISHED, butil::memory_order_release);
         // The handshake is over, so the QP stream may start parsing now.
         if (ep->StartCqEvents() < 0) {
-            const int saved_errno = errno;
+            rg.error_code = errno ? errno : ERDMA;
             PLOG(WARNING) << "Fail to start cq events on " << s->description();
-            s->SetFailed(saved_errno, "Fail to complete rdma handshake from 
%s: %s",
-                         s->description().c_str(), berror(saved_errno));
             ep->_state.store(FAILED, butil::memory_order_relaxed);
             return nullptr;
         }
@@ -600,8 +695,6 @@ void* RdmaEndpoint::ProcessHandshakeAtClient(void* arg) {
             << "Client handshake ends (use tcp) on " << s->description();
     }
 
-    errno = 0;
-
     return nullptr;
 }
 
diff --git a/src/brpc/rdma/rdma_endpoint.h b/src/brpc/rdma/rdma_endpoint.h
index 16dd782d8..fe25aa247 100644
--- a/src/brpc/rdma/rdma_endpoint.h
+++ b/src/brpc/rdma/rdma_endpoint.h
@@ -67,13 +67,18 @@ public:
             void (*done)(int err, void* data), void* data) override;
     void StopConnect(Socket*) override;
     struct RunGuard {
-        RunGuard(RdmaConnect* rc) { this_rc = rc; }
-        ~RunGuard() { if (this_rc) this_rc->Run(); }
+        explicit RunGuard(RdmaConnect* rc) : this_rc(rc), error_code(0) {}
+        ~RunGuard() {
+            if (this_rc) {
+                this_rc->Run(error_code);
+            }
+        }
         RdmaConnect* this_rc;
+        int error_code;
     };
 
 private:
-    void Run();
+    void Run(int error_code);
     void (*_done)(int, void*){nullptr};
     void* _data{nullptr};
 };
@@ -165,6 +170,14 @@ private:
     // Process handshake at the client
     static void* ProcessHandshakeAtClient(void* arg);
 
+    // Replace the TCP connection before retrying the client hello. The QP
+    // is still in RESET and no application data has been sent yet.
+    int ReconnectForHandshake();
+
+    // Take ownership of fd and install it for the next hello. Roll back the
+    // replacement if cancellation or the deadline is observed after setup.
+    int ReplaceHandshakeFd(int fd);
+
     static void OnNewDataFromTcpAtClient(Socket* m);
     static void OnNewDataFromTcpAtServer(Socket* m);
 
diff --git a/src/brpc/rdma/rdma_handshake.cpp b/src/brpc/rdma/rdma_handshake.cpp
index 63681999b..f65e25e60 100644
--- a/src/brpc/rdma/rdma_handshake.cpp
+++ b/src/brpc/rdma/rdma_handshake.cpp
@@ -38,11 +38,9 @@
 namespace brpc {
 namespace rdma {
 
-DEFINE_int32(rdma_client_handshake_version, 2,
-             "RDMA handshake protocol version used by client. "
-             "2 = legacy 'RDMA' magic (default, compatible with all servers); "
-             "3 = new 'RDM3' protobuf-based handshake "
-             "(MUST only be enabled after target servers support v3).");
+DEFINE_int32(rdma_client_handshake_version, 3,
+             "Deprecated and ignored. Retained for command-line compatibility. 
"
+             "Clients always start with v3 and retry v2 once if the peer 
rejects v3.");
 DECLARE_bool(rdma_trace_verbose);
 
 extern const uint16_t MIN_QP_SIZE;
@@ -434,10 +432,16 @@ int RdmaHandshakeClientV3::SendLocalHello() {
 RemoteHelloResult 
RdmaHandshakeClientV3::ReceiveAndParseRemoteHello(ParsedHello* remote) {
     uint8_t magic[HELLO_MAGIC_LEN];
     if (_ep->ReadFromFd(magic, HELLO_MAGIC_LEN) < 0) {
+        if (errno == EEOF || errno == ECONNRESET) {
+            return RemoteHelloResult::RETRY_V2;
+        }
         return RemoteHelloResult::ERROR;
     }
     if (memcmp(magic, HELLO_MAGIC_V3, HELLO_MAGIC_LEN) != 0) {
         errno = EPROTO;
+        if (memcmp(magic, HELLO_MAGIC, HELLO_MAGIC_LEN) == 0) {
+            return RemoteHelloResult::RETRY_V2;
+        }
         return RemoteHelloResult::ERROR;
     }
 
@@ -511,16 +515,6 @@ int RdmaHandshakeServerV3::SendLocalHello() {
     return v3_wire::WriteV3Hello(_ep, local_msg);
 }
 
-std::unique_ptr<RdmaHandshake> CreateClientHandshake(RdmaEndpoint* ep) {
-    switch (FLAGS_rdma_client_handshake_version) {
-    case 3:
-        return std::unique_ptr<RdmaHandshake>(new RdmaHandshakeClientV3(ep));
-    case 2:
-    default:
-        return std::unique_ptr<RdmaHandshake>(new RdmaHandshakeClientV2(ep));
-    }
-}
-
 std::unique_ptr<RdmaHandshake> CreateServerHandshakeByMagic(
     RdmaEndpoint* ep, butil::IOBuf* source, const uint8_t 
magic[HELLO_MAGIC_LEN]) {
     if (memcmp(magic, HELLO_MAGIC, HELLO_MAGIC_LEN) == 0) {
diff --git a/src/brpc/rdma/rdma_handshake.h b/src/brpc/rdma/rdma_handshake.h
index b0d4cfa49..f61975e48 100644
--- a/src/brpc/rdma/rdma_handshake.h
+++ b/src/brpc/rdma/rdma_handshake.h
@@ -71,6 +71,9 @@ enum class RemoteHelloResult {
     FALLBACK,
     // (server only) Not enough data yet.
     NEED_MORE,
+    // (v3 client only) The peer closed before identifying itself as v3, or
+    // replied with v2 magic. Retry v2 on a new TCP connection (errno set).
+    RETRY_V2,
     // IO/protocol error (errno set).
     ERROR,
 };
@@ -169,15 +172,6 @@ public:
     RemoteHelloResult ReceiveAndParseRemoteHello(ParsedHello* remote) override;
 };
 
-// Factory methods
-//
-// Pick the client-side handshake based on
-// FLAGS_rdma_client_handshake_version:
-//   2 (default) -> RdmaHandshakeClientV2
-//   3           -> RdmaHandshakeClientV3
-// Other values fall back to V2.
-std::unique_ptr<RdmaHandshake> CreateClientHandshake(RdmaEndpoint* ep);
-
 // Pick the server-side handshake based on the 4B magic already read.
 // Returns nullptr if `magic` is not a recognized RDMA magic
 // (the caller should then fallback to TCP).
diff --git a/src/brpc/socket.cpp b/src/brpc/socket.cpp
index 607823d8d..ce129a351 100644
--- a/src/brpc/socket.cpp
+++ b/src/brpc/socket.cpp
@@ -459,6 +459,7 @@ Socket::Socket(Forbidden f)
     , _need_on_edge_trigger(false)
     , _user(nullptr)
     , _conn(nullptr)
+    , _connect_deadline_us(-1)
     , _preferred_index(-1)
     , _hc_count(0)
     , _last_readtime_us(0)
@@ -746,6 +747,7 @@ int Socket::OnCreated(const SocketOptions& options) {
     _user = options.user;
     _conn = options.conn;
     _app_connect = _transport->Connect();
+    _connect_deadline_us = -1;
     _preferred_index = -1;
     _hc_count = 0;
     CHECK(_fd_input_processor.read_buf().empty());
@@ -928,7 +930,7 @@ void Socket::OnFailed(int error_code, const std::string& 
error_text) {
         StartHealthCheck(id(), 
GetOrNewSharedPart()->circuit_breaker.isolation_duration_ms());
     }
     // Wake up all threads waiting on EPOLLOUT when closing fd
-    _epollout_butex->fetch_add(1, butil::memory_order_relaxed);
+    _epollout_butex->fetch_add(1, butil::memory_order_release);
     bthread::butex_wake_all(_epollout_butex);
 
     // Wake up all unresponded RPC.
@@ -1256,19 +1258,24 @@ bool Socket::IsWriteComplete(Socket::WriteRequest* 
old_head,
     return false;
 }
 
-int Socket::WaitEpollOut(int fd, bool pollin, const timespec* abstime) {
+int Socket::WaitEpollOut(int fd, bool pollin, const timespec* abstime, bool 
check_failed) {
     if (!ValidFileDescriptor(fd)) {
         return 0;
     }
-    // Do not need to check addressable since it will be called by
-    // health checker which called `SetFailed' before
-    const int expected_val = 
_epollout_butex->load(butil::memory_order_relaxed);
+    // Snapshot before checking Failed(): an earlier cancellation is visible
+    // through the release in OnFailed(), while a later one changes the value
+    // or wakes butex_wait. Health checks deliberately skip this check.
+    int expected_val = _epollout_butex->load(butil::memory_order_acquire);
+    if (check_failed && Failed()) {
+        errno = ECANCELED;
+        return -1;
+    }
     if (_io_event.RegisterEvent(fd, pollin) != 0) {
         return -1;
     }
 
     int rc = bthread::butex_wait(_epollout_butex, expected_val, abstime);
-    const int saved_errno = errno;
+    int saved_errno = errno;
     if (rc < 0 && errno == EWOULDBLOCK) {
         // Could be writable or spurious wakeup
         rc = 0;
@@ -1276,13 +1283,18 @@ int Socket::WaitEpollOut(int fd, bool pollin, const 
timespec* abstime) {
     // Ignore return value since `fd' might have been removed
     // by `RemoveConsumer' in `SetFailed'
     butil::ignore_result(_io_event.UnregisterEvent(fd, pollin));
+    if (check_failed && Failed()) {
+        errno = ECANCELED;
+        return -1;
+    }
     errno = saved_errno;
     // Could be writable or spurious wakeup (by former epollout)
     return rc;
 }
 
 int Socket::Connect(const timespec* abstime,
-                    int (*on_connect)(int, int, void*), void* data) {
+                    int (*on_connect)(int, int, void*), void* data,
+                    bool check_failed) {
     if (_ssl_ctx) {
         _ssl_state = SSL_CONNECTING;
     } else {
@@ -1387,7 +1399,7 @@ int Socket::Connect(const timespec* abstime,
         }
         
     } else {
-        if (WaitEpollOut(sockfd, false, abstime) != 0) {
+        if (WaitEpollOut(sockfd, false, abstime, check_failed) != 0) {
             PLOG(WARNING) << "Fail to wait EPOLLOUT of fd=" << sockfd;
             return -1;
         }
@@ -1439,6 +1451,7 @@ int Socket::ConnectIfNot(const timespec* abstime, 
WriteRequest* req) {
     if (_fd.load(butil::memory_order_consume) >= 0) {
        return 0;
     }
+    _connect_deadline_us = abstime ? butil::timespec_to_microseconds(*abstime) 
: -1;
     // Set tag for client side socket
     _io_event.set_bthread_tag(bthread_self_tag());
     // Have to hold a reference for `req'
diff --git a/src/brpc/socket.h b/src/brpc/socket.h
index b947202c8..68a9721c2 100644
--- a/src/brpc/socket.h
+++ b/src/brpc/socket.h
@@ -799,8 +799,10 @@ private:
     // true, EPOLLIN event will also be included and EPOLL_CTL_MOD will
     // be used instead of EPOLL_CTL_ADD. Note that spurious wakeups may
     // occur when this function returns, so make sure to check whether fd
-    // is writable or not even when it returns 0
-    int WaitEpollOut(int fd, bool pollin, const timespec* abstime);
+    // is writable or not even when it returns 0. If `check_failed' is true,
+    // SetFailed() cancels the wait with ECANCELED, including failures that
+    // precede the wait. Leave it false for health checks on failed sockets.
+    int WaitEpollOut(int fd, bool pollin, const timespec* abstime, bool 
check_failed);
 
     // [Not thread-safe] Establish a tcp connection to `remote_side()'
     // If `on_connect' is nullptr, this function blocks current thread
@@ -810,8 +812,10 @@ private:
     // Returns the socket fd on success, -1 otherwise
     int DoConnect(const timespec* abstime,
                   int (*on_connect)(int fd, int err, void* data), void* data);
+    // `check_failed' only applies to synchronous connects (on_connect == 
nullptr).
     int Connect(const timespec* abstime,
-                int (*on_connect)(int fd, int err, void* data), void* data);
+                int (*on_connect)(int fd, int err, void* data), void* data,
+                bool check_failed = false);
 
     int CheckConnected(int sockfd);
 
@@ -956,6 +960,10 @@ private:
     // Initialized by SocketOptions.app_connect.
     std::shared_ptr<AppConnect> _app_connect;
 
+    // Absolute CLOCK_REALTIME deadline for the current connection attempt,
+    // including transport handshakes/reconnects. -1 means no deadline.
+    int64_t _connect_deadline_us;
+
     IOEvent<Socket> _io_event;
 
     // Last chosen index of the protocol as a heuristic value to avoid
diff --git a/src/brpc/tcp_transport.cpp b/src/brpc/tcp_transport.cpp
index 98fea8167..bd7ab0762 100644
--- a/src/brpc/tcp_transport.cpp
+++ b/src/brpc/tcp_transport.cpp
@@ -54,7 +54,7 @@ ssize_t TcpTransport::CutFromIOBufList(butil::IOBuf** buf, 
size_t ndata) {
 int TcpTransport::WaitEpollOut(butil::atomic<int>* _epollout_butex,
                                bool pollin, timespec duetime) {
     g_vars->nwaitepollout << 1;
-    const int rc = _socket->WaitEpollOut(_socket->fd(), pollin, &duetime);
+    int rc = _socket->WaitEpollOut(_socket->fd(), pollin, &duetime, false);
     if (rc < 0 && errno != ETIMEDOUT) {
         const int saved_errno = errno;
         PLOG(WARNING) << "Fail to wait epollout of " << _socket;
diff --git a/src/brpc/ubshm/ub_endpoint.cpp b/src/brpc/ubshm/ub_endpoint.cpp
index 97e39e5d7..5141a7151 100644
--- a/src/brpc/ubshm/ub_endpoint.cpp
+++ b/src/brpc/ubshm/ub_endpoint.cpp
@@ -198,6 +198,7 @@ void UBConnect::StartConnect(const Socket* socket,
     CHECK(ub_transport->_ub_ep != nullptr);
     SocketUniquePtr s;
     if (Socket::Address(socket->id(), &s) != 0) {
+        done(EFAILEDSOCKET, data);
         return;
     }
     if (!IsUBAvailable()) {
@@ -211,10 +212,12 @@ void UBConnect::StartConnect(const Socket* socket,
     bthread_t tid;
     bthread_attr_t attr = BTHREAD_ATTR_NORMAL;
     bthread_attr_set_name(&attr, "UBProcessHandshakeAtClient");
-    if (bthread_start_background(&tid, &attr,
-                UBShmEndpoint::ProcessHandshakeAtClient, ub_transport->_ub_ep) 
< 0) {
-        LOG(FATAL) << "Fail to start handshake bthread";
-        Run();
+    int rc = bthread_start_background(&tid, &attr,
+                                     UBShmEndpoint::ProcessHandshakeAtClient,
+                                     ub_transport->_ub_ep);
+    if (rc != 0) {
+        LOG(ERROR) << "Fail to start handshake bthread: " << berror(rc);
+        _done(rc, _data);
     } else {
         s.release();
     }
@@ -349,7 +352,7 @@ int UBShmEndpoint::WriteToFd(void* data, size_t len) {
         nw = write(_socket->fd(), (uint8_t*)data + written, len - written);
         if (nw < 0) {
             if (errno == EAGAIN) {
-                if (_socket->WaitEpollOut(_socket->fd(), true, &duetime) < 0) {
+                if (_socket->WaitEpollOut(_socket->fd(), true, &duetime, 
false) < 0) {
                     if (errno != ETIMEDOUT) {
                         return -1;
                     }
diff --git a/src/brpc/urma/urma_endpoint.cpp b/src/brpc/urma/urma_endpoint.cpp
index 26c7a1b9d..8fc5e8b3c 100644
--- a/src/brpc/urma/urma_endpoint.cpp
+++ b/src/brpc/urma/urma_endpoint.cpp
@@ -237,7 +237,7 @@ int UrmaEndpoint::WriteToFd(void* data, size_t len) {
         if (errno != EAGAIN && errno != EWOULDBLOCK) {
             return -1;
         }
-        if (_socket->WaitEpollOut(fd, true, &duetime) != 0 && errno != 
ETIMEDOUT) {
+        if (_socket->WaitEpollOut(fd, true, &duetime, false) != 0 && errno != 
ETIMEDOUT) {
             return -1;
         }
     }
@@ -336,7 +336,7 @@ int UrmaEndpoint::WriteToFd(butil::IOBuf& data) {
         if (errno != EAGAIN && errno != EWOULDBLOCK) {
             return -1;
         }
-        if (_socket->WaitEpollOut(fd, true, &duetime) != 0 && errno != 
ETIMEDOUT) {
+        if (_socket->WaitEpollOut(fd, true, &duetime, false) != 0 && errno != 
ETIMEDOUT) {
             return -1;
         }
     }
@@ -1485,6 +1485,7 @@ void UrmaConnect::StartConnect(const Socket* socket,
                                void (*done)(int, void*), void* data) {
     SocketUniquePtr s;
     if (Socket::Address(socket->id(), &s) != 0) {
+        done(EFAILEDSOCKET, data);
         return;
     }
     _done = done;
diff --git a/test/brpc_rdma_unittest.cpp b/test/brpc_rdma_unittest.cpp
index 59bd9da55..039036a75 100644
--- a/test/brpc_rdma_unittest.cpp
+++ b/test/brpc_rdma_unittest.cpp
@@ -22,6 +22,8 @@
 #include <gflags/gflags.h>
 #if BRPC_WITH_RDMA
 #include <errno.h>
+#include <fcntl.h>
+#include <poll.h>
 #include <unistd.h>
 #include <functional>
 #include <vector>
@@ -29,6 +31,7 @@
 #include "butil/endpoint.h"
 #include "butil/fd_guard.h"
 #include "butil/iobuf.h"
+#include "butil/memory/scope_guard.h"
 #include "butil/sys_byteorder.h"
 #include "butil/time.h"
 #include "butil/files/temp_file.h"
@@ -49,6 +52,7 @@
 #include "brpc/rdma/rdma_handshake_constants.h"
 #include "brpc/rdma/rdma_handshake.pb.h"
 #include "brpc/rdma/rdma_helper.h"
+#include "brpc/policy/baidu_rpc_meta.pb.h"
 #include "echo.pb.h"
 
 static const int PORT = 8713;
@@ -59,6 +63,7 @@ namespace brpc {
 
 DECLARE_int64(socket_max_unwritten_bytes);
 DECLARE_bool(log_idle_connection_close);
+DECLARE_int32(connect_timeout_as_unreachable);
 DEFINE_bool(rdma_test_enable, false, "Enable tests requring rdma runtime.");
 
 namespace rdma {
@@ -159,6 +164,58 @@ static bool ReadAll(int fd, void* buf, size_t len) {
     return true;
 }
 
+static int AcceptPeer(int listener) {
+    pollfd pfd = {listener, POLLIN, 0};
+    if (poll(&pfd, 1, WAIT_TIMEOUT_US / 1000) != 1) {
+        return -1;
+    }
+    butil::fd_guard fd(accept(listener, nullptr, nullptr));
+    if (fd < 0) {
+        return -1;
+    }
+    timeval tv = {WAIT_TIMEOUT_US / 1000000, 0};
+    if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) {
+        return -1;
+    }
+    return fd.release();
+}
+
+static void ReadHello(int fd, int version) {
+    char magic[4];
+    ASSERT_TRUE(ReadAll(fd, magic, sizeof(magic)));
+    ASSERT_EQ(0, memcmp(magic, version == 3 ? "RDM3" : "RDMA", 4));
+    if (version == 3) {
+        uint32_t size_be;
+        ASSERT_TRUE(ReadAll(fd, &size_be, sizeof(size_be)));
+        uint32_t size = butil::NetToHost32(size_be);
+        ASSERT_GT(size, 0u);
+        ASSERT_LE(size, rdma::HELLO_V3_MAX_PB_SIZE);
+        std::string body(size, '\0');
+        ASSERT_TRUE(ReadAll(fd, &body[0], body.size()));
+        rdma::RdmaHello hello;
+        ASSERT_TRUE(hello.ParseFromString(body));
+    } else {
+        char body[rdma::HELLO_V2_MSG_LEN_MIN - 4];
+        ASSERT_TRUE(ReadAll(fd, body, sizeof(body)));
+        rdma::v2_wire::HelloMessage hello{};
+        hello.Deserialize(body);
+        ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, hello.msg_len);
+        ASSERT_EQ(rdma::HELLO_V2_VERSION, hello.hello_ver);
+        ASSERT_EQ(rdma::IMPL_V2_VERSION, hello.impl_ver);
+    }
+}
+
+// Simulate an old server rejecting v3, then return the fresh connection on
+// which the client retries v2. Leave its v2 hello for the test to inspect.
+static void AcceptV2Peer(int listener, butil::fd_guard* peer) {
+    butil::fd_guard first(AcceptPeer(listener));
+    ASSERT_GE(first, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(first, 3));
+    first.reset(-1);
+    peer->reset(AcceptPeer(listener));
+    ASSERT_GE(*peer, 0);
+}
+
 // Connect a raw socket to the test server. Note that sin_addr is taken from
 // `g_ep`: a zeroed one means 0.0.0.0, which only happens to reach the local
 // server on some systems. The receive timeout keeps a missing server reply 
from
@@ -326,28 +383,8 @@ static void MakeV2ClientHello(uint8_t 
(&data)[rdma::HELLO_V2_MSG_LEN_MIN]) {
     msg.Serialize(data + 4);
 }
 
-// Parameterized fixture used by upper-layer RPC tests that have no
-// dependency on the handshake wire format. The parameter is the
-// client-side handshake protocol version 
(FLAGS_rdma_client_handshake_version),
-// so every TEST_P below is automatically executed once per supported
-// version. Add a new version to INSTANTIATE_TEST_SUITE_P at the bottom
-// of this file and these RPC tests will gain coverage for free.
-class RdmaRpcTest : public RdmaTest,
-                    public ::testing::WithParamInterface<int> {
-protected:
-    void SetUp() override {
-        RdmaTest::SetUp();
-        _saved_handshake_version = rdma::FLAGS_rdma_client_handshake_version;
-        rdma::FLAGS_rdma_client_handshake_version = GetParam();
-    }
-    void TearDown() override {
-        rdma::FLAGS_rdma_client_handshake_version = _saved_handshake_version;
-        RdmaTest::TearDown();
-    }
-
-private:
-    int _saved_handshake_version = 2;
-};
+// RPC tests use the same automatic handshake selection as production.
+class RdmaRpcTest : public RdmaTest {};
 
 TEST_F(RdmaTest, stale_cq_callback_does_not_poll_new_generation) {
     SocketOptions main_options;
@@ -976,7 +1013,7 @@ TEST_F(RdmaTest, server_miss_before_hello_send) {
     ChannelOptions chan_options;
     chan_options.socket_mode = SOCKET_MODE_RDMA;
     chan_options.connect_timeout_ms = 500;
-    chan_options.timeout_ms = 500;
+    chan_options.timeout_ms = 5000;
     chan_options.max_retry = 0;
     ASSERT_EQ(0, channel.Init(g_ep, &chan_options));
 
@@ -995,7 +1032,7 @@ TEST_F(RdmaTest, server_miss_before_hello_send) {
     ASSERT_TRUE(acc_fd >= 0);
     bthread_id_join(cntl.call_id());
 
-    ASSERT_EQ(ERPCTIMEDOUT, cntl.ErrorCode());
+    ASSERT_EQ(ETIMEDOUT, cntl.ErrorCode());
 }
 
 TEST_F(RdmaTest, server_close_before_hello_send) {
@@ -1021,11 +1058,11 @@ TEST_F(RdmaTest, server_close_before_hello_send) {
     ASSERT_EQ(0, Socket::Address(cntl._single_server_id, &s));
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::C_HELLO_WAIT, RdmaTransportOf(s));
 
-    butil::fd_guard acc_fd(accept(sockfd, nullptr, nullptr));
-    ASSERT_TRUE(acc_fd >= 0);
+    butil::fd_guard acc_fd;
+    ASSERT_NO_FATAL_FAILURE(AcceptV2Peer(sockfd, &acc_fd));
     uint8_t data[rdma::HELLO_V2_MSG_LEN_MIN];
-    ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, read(acc_fd, data, 
rdma::HELLO_V2_MSG_LEN_MIN));
-    close(acc_fd);
+    ASSERT_TRUE(ReadAll(acc_fd, data, sizeof(data)));
+    acc_fd.reset(-1);
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::FAILED, RdmaTransportOf(s));
     bthread_id_join(cntl.call_id());
 
@@ -1040,7 +1077,7 @@ TEST_F(RdmaTest, server_miss_during_magic_str) {
     ChannelOptions chan_options;
     chan_options.socket_mode = SOCKET_MODE_RDMA;
     chan_options.connect_timeout_ms = 500;
-    chan_options.timeout_ms = 500;
+    chan_options.timeout_ms = 5000;
     chan_options.max_retry = 0;
     ASSERT_EQ(0, channel.Init(g_ep, &chan_options));
 
@@ -1055,17 +1092,16 @@ TEST_F(RdmaTest, server_miss_during_magic_str) {
     ASSERT_EQ(0, Socket::Address(cntl._single_server_id, &s));
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::C_HELLO_WAIT, RdmaTransportOf(s));
 
-    butil::fd_guard acc_fd(accept(sockfd, nullptr, nullptr));
-    ASSERT_TRUE(acc_fd >= 0);
+    butil::fd_guard acc_fd;
+    ASSERT_NO_FATAL_FAILURE(AcceptV2Peer(sockfd, &acc_fd));
     uint8_t data[rdma::HELLO_V2_MSG_LEN_MIN];
-    ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, read(acc_fd, data, 
rdma::HELLO_V2_MSG_LEN_MIN));
+    ASSERT_TRUE(ReadAll(acc_fd, data, sizeof(data)));
     ASSERT_TRUE(WriteAll(acc_fd, "RD", 2));
-    // Half a magic is not enough to decide anything, so the client stays stuck
-    // in the handshake read and the RPC runs into its timeout. Joining below
-    // waits for exactly that, no sleeping needed.
+    // Half a magic is not enough to decide anything. The handshake must stop
+    // at the connection deadline, before the longer RPC timeout.
     bthread_id_join(cntl.call_id());
 
-    ASSERT_EQ(ERPCTIMEDOUT, cntl.ErrorCode());
+    ASSERT_EQ(ETIMEDOUT, cntl.ErrorCode());
 }
 
 TEST_F(RdmaTest, server_close_during_magic_str) {
@@ -1091,10 +1127,10 @@ TEST_F(RdmaTest, server_close_during_magic_str) {
     ASSERT_EQ(0, Socket::Address(cntl._single_server_id, &s));
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::C_HELLO_WAIT, RdmaTransportOf(s));
 
-    butil::fd_guard acc_fd(accept(sockfd, nullptr, nullptr));
-    ASSERT_TRUE(acc_fd >= 0);
+    butil::fd_guard acc_fd;
+    ASSERT_NO_FATAL_FAILURE(AcceptV2Peer(sockfd, &acc_fd));
     uint8_t data[rdma::HELLO_V2_MSG_LEN_MIN];
-    ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, read(acc_fd, data, 
rdma::HELLO_V2_MSG_LEN_MIN));
+    ASSERT_TRUE(ReadAll(acc_fd, data, sizeof(data)));
     // Half a magic and then EOF. TCP keeps the order, so the client always 
sees
     // the two bytes first and then the close, which is what this test is 
about.
     ASSERT_TRUE(WriteAll(acc_fd, "RD", 2));
@@ -1128,10 +1164,10 @@ TEST_F(RdmaTest, server_hello_invalid_magic_str) {
     ASSERT_EQ(0, Socket::Address(cntl._single_server_id, &s));
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::C_HELLO_WAIT, RdmaTransportOf(s));
 
-    butil::fd_guard acc_fd(accept(sockfd, nullptr, nullptr));
-    ASSERT_TRUE(acc_fd >= 0);
+    butil::fd_guard acc_fd;
+    ASSERT_NO_FATAL_FAILURE(AcceptV2Peer(sockfd, &acc_fd));
     uint8_t data[rdma::HELLO_V2_MSG_LEN_MIN];
-    ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, read(acc_fd, data, 
rdma::HELLO_V2_MSG_LEN_MIN));
+    ASSERT_TRUE(ReadAll(acc_fd, data, sizeof(data)));
     ASSERT_EQ(4, write(acc_fd, "ABCD", 4));
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::FAILED, RdmaTransportOf(s));
     bthread_id_join(cntl.call_id());
@@ -1147,7 +1183,7 @@ TEST_F(RdmaTest, server_miss_during_hello_msg) {
     ChannelOptions chan_options;
     chan_options.socket_mode = SOCKET_MODE_RDMA;
     chan_options.connect_timeout_ms = 500;
-    chan_options.timeout_ms = 500;
+    chan_options.timeout_ms = 5000;
     chan_options.max_retry = 0;
     ASSERT_EQ(0, channel.Init(g_ep, &chan_options));
 
@@ -1162,15 +1198,15 @@ TEST_F(RdmaTest, server_miss_during_hello_msg) {
     ASSERT_EQ(0, Socket::Address(cntl._single_server_id, &s));
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::C_HELLO_WAIT, RdmaTransportOf(s));
 
-    butil::fd_guard acc_fd(accept(sockfd, nullptr, nullptr));
-    ASSERT_TRUE(acc_fd >= 0);
+    butil::fd_guard acc_fd;
+    ASSERT_NO_FATAL_FAILURE(AcceptV2Peer(sockfd, &acc_fd));
     uint8_t data[rdma::HELLO_V2_MSG_LEN_MIN];
-    ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, read(acc_fd, data, 
rdma::HELLO_V2_MSG_LEN_MIN));
+    ASSERT_TRUE(ReadAll(acc_fd, data, sizeof(data)));
     ASSERT_EQ(4, write(acc_fd, "RDMA", 4));
     ASSERT_EQ(2, write(acc_fd, "00", 2));
     bthread_id_join(cntl.call_id());
 
-    ASSERT_EQ(ERPCTIMEDOUT, cntl.ErrorCode());
+    ASSERT_EQ(ETIMEDOUT, cntl.ErrorCode());
 }
 
 TEST_F(RdmaTest, server_close_during_hello_msg) {
@@ -1196,13 +1232,13 @@ TEST_F(RdmaTest, server_close_during_hello_msg) {
     ASSERT_EQ(0, Socket::Address(cntl._single_server_id, &s));
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::C_HELLO_WAIT, RdmaTransportOf(s));
 
-    butil::fd_guard acc_fd(accept(sockfd, nullptr, nullptr));
-    ASSERT_TRUE(acc_fd >= 0);
+    butil::fd_guard acc_fd;
+    ASSERT_NO_FATAL_FAILURE(AcceptV2Peer(sockfd, &acc_fd));
     uint8_t data[rdma::HELLO_V2_MSG_LEN_MIN];
-    ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, read(acc_fd, data, 
rdma::HELLO_V2_MSG_LEN_MIN));
+    ASSERT_TRUE(ReadAll(acc_fd, data, sizeof(data)));
     ASSERT_EQ(4, write(acc_fd, "RDMA", 4));
     ASSERT_EQ(2, write(acc_fd, "00", 2));
-    close(acc_fd);
+    acc_fd.reset(-1);
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::FAILED, RdmaTransportOf(s));
     bthread_id_join(cntl.call_id());
 
@@ -1232,10 +1268,10 @@ TEST_F(RdmaTest, server_hello_invalid_msg_len) {
     ASSERT_EQ(0, Socket::Address(cntl._single_server_id, &s));
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::C_HELLO_WAIT, RdmaTransportOf(s));
 
-    butil::fd_guard acc_fd(accept(sockfd, nullptr, nullptr));
-    ASSERT_TRUE(acc_fd >= 0);
+    butil::fd_guard acc_fd;
+    ASSERT_NO_FATAL_FAILURE(AcceptV2Peer(sockfd, &acc_fd));
     uint8_t data[rdma::HELLO_V2_MSG_LEN_MIN];
-    ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, read(acc_fd, data, 
rdma::HELLO_V2_MSG_LEN_MIN));
+    ASSERT_TRUE(ReadAll(acc_fd, data, sizeof(data)));
     memcpy(data, "RDMA", 4);
     uint16_t len = butil::HostToNet16(35);
     memcpy(data + 4, &len, 2);
@@ -1270,10 +1306,10 @@ TEST_F(RdmaTest, server_hello_invalid_version) {
     ASSERT_EQ(0, Socket::Address(cntl._single_server_id, &s));
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::C_HELLO_WAIT, RdmaTransportOf(s));
 
-    butil::fd_guard acc_fd(accept(sockfd, nullptr, nullptr));
-    ASSERT_TRUE(acc_fd >= 0);
+    butil::fd_guard acc_fd;
+    ASSERT_NO_FATAL_FAILURE(AcceptV2Peer(sockfd, &acc_fd));
     uint8_t data[rdma::HELLO_V2_MSG_LEN_MIN];
-    ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, read(acc_fd, data, 
rdma::HELLO_V2_MSG_LEN_MIN));
+    ASSERT_TRUE(ReadAll(acc_fd, data, sizeof(data)));
     memcpy(data, "RDMA", 4);
     uint16_t len = butil::HostToNet16(rdma::HELLO_V2_MSG_LEN_MIN);
     memcpy(data + 4, &len, 2);
@@ -1311,10 +1347,10 @@ TEST_F(RdmaTest, server_hello_invalid_sq_rq_size) {
     ASSERT_EQ(0, Socket::Address(cntl._single_server_id, &s));
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::C_HELLO_WAIT, RdmaTransportOf(s));
 
-    butil::fd_guard acc_fd(accept(sockfd, nullptr, nullptr));
-    ASSERT_TRUE(acc_fd >= 0);
+    butil::fd_guard acc_fd;
+    ASSERT_NO_FATAL_FAILURE(AcceptV2Peer(sockfd, &acc_fd));
     uint8_t data[rdma::HELLO_V2_MSG_LEN_MIN];
-    ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, read(acc_fd, data, 
rdma::HELLO_V2_MSG_LEN_MIN));
+    ASSERT_TRUE(ReadAll(acc_fd, data, sizeof(data)));
 
     rdma::v2_wire::HelloMessage msg{};
     msg.msg_len = rdma::HELLO_V2_MSG_LEN_MIN;
@@ -1361,10 +1397,10 @@ TEST_F(RdmaTest, server_miss_after_ack) {
     ASSERT_EQ(0, Socket::Address(cntl._single_server_id, &s));
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::C_HELLO_WAIT, RdmaTransportOf(s));
 
-    butil::fd_guard acc_fd(accept(sockfd, nullptr, nullptr));
-    ASSERT_TRUE(acc_fd >= 0);
+    butil::fd_guard acc_fd;
+    ASSERT_NO_FATAL_FAILURE(AcceptV2Peer(sockfd, &acc_fd));
     uint8_t data[rdma::HELLO_V2_MSG_LEN_MIN];
-    ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, read(acc_fd, data, 
rdma::HELLO_V2_MSG_LEN_MIN));
+    ASSERT_TRUE(ReadAll(acc_fd, data, sizeof(data)));
 
     rdma::v2_wire::HelloMessage msg{};
     msg.msg_len = rdma::HELLO_V2_MSG_LEN_MIN;
@@ -1411,10 +1447,10 @@ TEST_F(RdmaTest, server_close_after_ack) {
     ASSERT_EQ(0, Socket::Address(cntl._single_server_id, &s));
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::C_HELLO_WAIT, RdmaTransportOf(s));
 
-    butil::fd_guard acc_fd(accept(sockfd, nullptr, nullptr));
-    ASSERT_TRUE(acc_fd >= 0);
+    butil::fd_guard acc_fd;
+    ASSERT_NO_FATAL_FAILURE(AcceptV2Peer(sockfd, &acc_fd));
     uint8_t data[rdma::HELLO_V2_MSG_LEN_MIN];
-    ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, read(acc_fd, data, 
rdma::HELLO_V2_MSG_LEN_MIN));
+    ASSERT_TRUE(ReadAll(acc_fd, data, sizeof(data)));
 
     rdma::v2_wire::HelloMessage msg{};
     msg.msg_len = rdma::HELLO_V2_MSG_LEN_MIN;
@@ -1433,7 +1469,7 @@ TEST_F(RdmaTest, server_close_after_ack) {
     ASSERT_EQ(4, read(acc_fd, data, 4));
     uint32_t* tmp = (uint32_t*)data;
     ASSERT_EQ(1, butil::NetToHost32(*tmp));
-    close(acc_fd);
+    acc_fd.reset(-1);
     bthread_id_join(cntl.call_id());
 
     ASSERT_EQ(EEOF, cntl.ErrorCode());
@@ -1462,10 +1498,10 @@ TEST_F(RdmaTest, server_send_data_on_tcp_after_ack) {
     ASSERT_EQ(0, Socket::Address(cntl._single_server_id, &s));
     ASSERT_RDMA_STATE(rdma::RdmaEndpoint::C_HELLO_WAIT, RdmaTransportOf(s));
 
-    butil::fd_guard acc_fd(accept(sockfd, nullptr, nullptr));
-    ASSERT_TRUE(acc_fd >= 0);
+    butil::fd_guard acc_fd;
+    ASSERT_NO_FATAL_FAILURE(AcceptV2Peer(sockfd, &acc_fd));
     uint8_t data[rdma::HELLO_V2_MSG_LEN_MIN];
-    ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, read(acc_fd, data, 
rdma::HELLO_V2_MSG_LEN_MIN));
+    ASSERT_TRUE(ReadAll(acc_fd, data, sizeof(data)));
 
     rdma::v2_wire::HelloMessage msg{};
     msg.msg_len = rdma::HELLO_V2_MSG_LEN_MIN;
@@ -1510,11 +1546,11 @@ TEST_F(RdmaTest, v2_client_hello_bytes_baseline) {
     SocketUniquePtr s;
     ASSERT_EQ(0, Socket::Address(cntl._single_server_id, &s));
 
-    butil::fd_guard acc_fd(accept(sockfd, nullptr, nullptr));
-    ASSERT_TRUE(acc_fd >= 0);
+    butil::fd_guard acc_fd;
+    ASSERT_NO_FATAL_FAILURE(AcceptV2Peer(sockfd, &acc_fd));
 
     uint8_t data[rdma::HELLO_V2_MSG_LEN_MIN];
-    ASSERT_EQ(rdma::HELLO_V2_MSG_LEN_MIN, read(acc_fd, data, 
rdma::HELLO_V2_MSG_LEN_MIN));
+    ASSERT_TRUE(ReadAll(acc_fd, data, sizeof(data)));
 
     // [0..4) magic
     ASSERT_EQ(0, memcmp(data, "RDMA", 4));
@@ -1682,22 +1718,6 @@ TEST_F(RdmaTest, v2_server_rejects_oversized_msg_len) {
     StopServer();
 }
 
-// RAII for FLAGS_rdma_client_handshake_version: lets us flip the
-// client-side handshake version for a single test and restore it on
-// scope exit so subsequent tests stay on the v2 default.
-class HandshakeVersionFlag {
-public:
-    explicit HandshakeVersionFlag(int v)
-        : _saved(rdma::FLAGS_rdma_client_handshake_version) {
-        rdma::FLAGS_rdma_client_handshake_version = v;
-    }
-    ~HandshakeVersionFlag() {
-        rdma::FLAGS_rdma_client_handshake_version = _saved;
-    }
-private:
-    int _saved;
-};
-
 // Build a v3 wire packet from an RdmaHello: "RDM3" + pb_size_be + body.
 std::string MakeV3Packet(const rdma::RdmaHello& msg) {
     std::string body;
@@ -1731,10 +1751,431 @@ rdma::RdmaHello MakeValidV3Hello() {
     return msg;
 }
 
+// A raw peer can reject v3 exactly as an old server would, without adding a
+// server-side version flag or requiring RDMA hardware.
+class RdmaHandshakeRetryTest : public RdmaTest {
+protected:
+    RdmaHandshakeRetryTest() : _started(false) {}
 
-TEST_F(RdmaTest, v3_client_hello_bytes_baseline) {
-    HandshakeVersionFlag _hsv(3);
+    void SetUp() override {
+        _listener.reset(butil::tcp_listen(butil::EndPoint(g_ep.ip, 0)));
+        ASSERT_GE(_listener, 0);
+        ASSERT_EQ(0, butil::get_local_side(_listener, &g_ep));
+    }
 
+    void TearDown() override {
+        if (_socket && !_socket->Failed()) {
+            _socket->SetFailed(ECANCELED, "Handshake test finished");
+        }
+        if (_started) {
+            bthread_id_join(_cntl.call_id());
+        }
+        RdmaTest::TearDown();
+    }
+
+    void StartRpc(int connect_timeout_ms = 2000) {
+        ChannelOptions options;
+        options.socket_mode = SOCKET_MODE_RDMA;
+        options.connection_type = CONNECTION_TYPE_SINGLE;
+        options.connect_timeout_ms = connect_timeout_ms;
+        options.timeout_ms = 5000;
+        options.max_retry = 0;
+        ASSERT_EQ(0, _channel.Init(g_ep, &options));
+        _req.set_message("handshake retry");
+        _started = true;
+        test::EchoService::Stub(&_channel).Echo(&_cntl, &_req, &_res, 
DoNothing());
+        ASSERT_EQ(0, Socket::Address(_cntl._single_server_id, &_socket));
+    }
+
+    int AcceptPeer() {
+        return ::AcceptPeer(_listener);
+    }
+
+    void ExpectNoConnection() {
+        pollfd pfd = {_listener, POLLIN, 0};
+        ASSERT_EQ(0, poll(&pfd, 1, 0));
+    }
+
+    void ReplyV2(int fd, bool use_rdma) {
+        rdma::v2_wire::HelloMessage hello{};
+        hello.msg_len = rdma::HELLO_V2_MSG_LEN_MIN;
+        hello.hello_ver = use_rdma ? rdma::HELLO_V2_VERSION : 0;
+        hello.impl_ver = rdma::IMPL_V2_VERSION;
+        hello.block_size = 8192;
+        hello.sq_size = 16;
+        hello.rq_size = 16;
+        hello.gid = rdma::GetRdmaGid();
+        char packet[rdma::HELLO_V2_MSG_LEN_MIN];
+        memcpy(packet, "RDMA", 4);
+        hello.Serialize(packet + 4);
+        ASSERT_TRUE(WriteAll(fd, packet, sizeof(packet)));
+    }
+
+    void ReadAck(int fd, bool use_rdma) {
+        uint32_t ack_be;
+        ASSERT_TRUE(ReadAll(fd, &ack_be, sizeof(ack_be)));
+        ASSERT_EQ(use_rdma ? rdma::HELLO_ACK_RDMA_OK : 0,
+                  butil::NetToHost32(ack_be));
+    }
+
+    // Complete the queued RPC over TCP after an unnegotiable v2/v3 hello.
+    // Reading its request proves the retry retained the original WriteRequest.
+    void CompleteRpc(int fd) {
+        uint32_t header[3];
+        ASSERT_TRUE(ReadAll(fd, header, sizeof(header)));
+        ASSERT_EQ(0, memcmp(header, "PRPC", 4));
+        uint32_t body_size = butil::NetToHost32(header[1]);
+        uint32_t meta_size = butil::NetToHost32(header[2]);
+        ASSERT_LE(meta_size, body_size);
+        ASSERT_GT(body_size, 0u);
+        ASSERT_LT(body_size, 1024u * 1024u);
+        std::string body(body_size, '\0');
+        ASSERT_TRUE(ReadAll(fd, &body[0], body.size()));
+        policy::RpcMeta request_meta;
+        ASSERT_TRUE(request_meta.ParseFromArray(body.data(), meta_size));
+        test::EchoRequest request;
+        ASSERT_TRUE(request.ParseFromArray(body.data() + meta_size,
+                                           body_size - meta_size));
+        ASSERT_EQ(_req.message(), request.message());
+
+        policy::RpcMeta response_meta;
+        response_meta.set_correlation_id(request_meta.correlation_id());
+        response_meta.mutable_response()->set_error_code(0);
+        test::EchoResponse response;
+        response.set_message(request.message());
+        std::string meta = response_meta.SerializeAsString();
+        std::string payload = response.SerializeAsString();
+        header[1] = butil::HostToNet32(meta.size() + payload.size());
+        header[2] = butil::HostToNet32(meta.size());
+        ASSERT_TRUE(WriteAll(fd, header, sizeof(header)));
+        ASSERT_TRUE(WriteAll(fd, meta.data(), meta.size()));
+        ASSERT_TRUE(WriteAll(fd, payload.data(), payload.size()));
+        bthread_id_join(_cntl.call_id());
+        ASSERT_FALSE(_cntl.Failed()) << _cntl.ErrorText();
+        ASSERT_EQ(_req.message(), _res.message());
+        ASSERT_EQ(0, _cntl.retried_count());
+    }
+
+    void CheckPooledTimeoutConversion(bool retry_v2) {
+        GFLAGS_NAMESPACE::FlagSaver saved_flags;
+        FLAGS_connect_timeout_as_unreachable = 2;
+        Channel channel;
+        ChannelOptions options;
+        options.socket_mode = SOCKET_MODE_RDMA;
+        options.connection_type = CONNECTION_TYPE_POOLED;
+        options.connect_timeout_ms = 500;
+        options.timeout_ms = 5000;
+        options.max_retry = 0;
+        ASSERT_EQ(0, channel.Init(g_ep, &options));
+        SocketUniquePtr main_socket;
+        for (int i = 0; i < 2; ++i) {
+            Controller cntl;
+            test::EchoRequest req;
+            test::EchoResponse res;
+            req.set_message("pooled handshake timeout");
+            test::EchoService::Stub(&channel).Echo(&cntl, &req, &res, 
DoNothing());
+            auto join_rpc = butil::MakeScopeGuard([&cntl] {
+                bthread_id_join(cntl.call_id());
+            });
+            if (!main_socket) {
+                ASSERT_EQ(0, Socket::Address(cntl._single_server_id, 
&main_socket));
+            }
+            butil::fd_guard peer(AcceptPeer());
+            ASSERT_GE(peer, 0);
+            ASSERT_NO_FATAL_FAILURE(ReadHello(peer, 3));
+            if (retry_v2) {
+                peer.reset(-1);
+                peer.reset(AcceptPeer());
+                ASSERT_GE(peer, 0);
+                ASSERT_NO_FATAL_FAILURE(ReadHello(peer, 2));
+            }
+            // Leave the hello unanswered until the original connect deadline.
+            bthread_id_join(cntl.call_id());
+            EXPECT_EQ(i == 0 ? ETIMEDOUT : ENETUNREACH, cntl.ErrorCode());
+            EXPECT_EQ(i != 0, main_socket->Failed());
+            EXPECT_EQ(0, cntl.retried_count());
+        }
+    }
+
+    bool _started;
+    butil::fd_guard _listener;
+    Channel _channel;
+    Controller _cntl;
+    test::EchoRequest _req;
+    test::EchoResponse _res;
+    SocketUniquePtr _socket;
+};
+
+TEST_F(RdmaHandshakeRetryTest, eof_retries_v2_rdma) {
+    ASSERT_NO_FATAL_FAILURE(StartRpc());
+    butil::fd_guard first(AcceptPeer());
+    ASSERT_GE(first, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(first, 3));
+    first.reset(-1);
+
+    butil::fd_guard second(AcceptPeer());
+    ASSERT_GE(second, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(second, 2));
+    ASSERT_NO_FATAL_FAILURE(ReplyV2(second, true));
+    ASSERT_NO_FATAL_FAILURE(ReadAck(second, true));
+    ASSERT_RDMA_STATE(rdma::RdmaEndpoint::ESTABLISHED, 
RdmaTransportOf(_socket));
+    EXPECT_EQ(2, RdmaTransportOf(_socket)->_rdma_ep->_handshake_version);
+    
EXPECT_FALSE(RdmaTransportOf(_socket)->_rdma_ep->_outgoing_ece.has_value());
+    EXPECT_FALSE(_socket->Failed());
+}
+
+TEST_F(RdmaHandshakeRetryTest, reset_retries_v2_and_completes_rpc) {
+    ASSERT_NO_FATAL_FAILURE(StartRpc());
+    butil::fd_guard first(AcceptPeer());
+    ASSERT_GE(first, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(first, 3));
+    linger reset = {1, 0};
+    ASSERT_EQ(0, setsockopt(first, SOL_SOCKET, SO_LINGER, &reset, 
sizeof(reset)));
+    first.reset(-1);
+
+    butil::fd_guard second(AcceptPeer());
+    ASSERT_GE(second, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(second, 2));
+    ASSERT_NO_FATAL_FAILURE(ReplyV2(second, false));
+    ASSERT_NO_FATAL_FAILURE(ReadAck(second, false));
+    ASSERT_NO_FATAL_FAILURE(CompleteRpc(second));
+    EXPECT_EQ(2, RdmaTransportOf(_socket)->_rdma_ep->_handshake_version);
+    ASSERT_NO_FATAL_FAILURE(ExpectNoConnection());
+}
+
+TEST_F(RdmaHandshakeRetryTest, v2_reply_retries_on_new_connection) {
+    ASSERT_NO_FATAL_FAILURE(StartRpc());
+    butil::fd_guard first(AcceptPeer());
+    ASSERT_GE(first, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(first, 3));
+    ASSERT_NO_FATAL_FAILURE(ReplyV2(first, false));
+
+    butil::fd_guard second(AcceptPeer());
+    ASSERT_GE(second, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(second, 2));
+    ASSERT_NO_FATAL_FAILURE(ReplyV2(second, false));
+    ASSERT_NO_FATAL_FAILURE(ReadAck(second, false));
+    ASSERT_NO_FATAL_FAILURE(CompleteRpc(second));
+}
+
+TEST_F(RdmaHandshakeRetryTest, v3_reply_establishes_without_retry) {
+    ASSERT_NO_FATAL_FAILURE(StartRpc());
+    butil::fd_guard peer(AcceptPeer());
+    ASSERT_GE(peer, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(peer, 3));
+    std::string packet = MakeV3Packet(MakeValidV3Hello());
+    ASSERT_TRUE(WriteAll(peer, packet.data(), packet.size()));
+    ASSERT_NO_FATAL_FAILURE(ReadAck(peer, true));
+    ASSERT_RDMA_STATE(rdma::RdmaEndpoint::ESTABLISHED, 
RdmaTransportOf(_socket));
+    EXPECT_EQ(3, RdmaTransportOf(_socket)->_rdma_ep->_handshake_version);
+    ASSERT_NO_FATAL_FAILURE(ExpectNoConnection());
+}
+
+TEST_F(RdmaHandshakeRetryTest, deprecated_version_flag_does_not_select_v2) {
+    GFLAGS_NAMESPACE::FlagSaver saved_flags;
+    ASSERT_FALSE(GFLAGS_NAMESPACE::SetCommandLineOption(
+        "rdma_client_handshake_version", "2").empty());
+    ASSERT_EQ(2, rdma::FLAGS_rdma_client_handshake_version);
+    ASSERT_NO_FATAL_FAILURE(StartRpc());
+    butil::fd_guard peer(AcceptPeer());
+    ASSERT_GE(peer, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(peer, 3));
+    std::string packet = MakeV3Packet(MakeValidV3Hello());
+    ASSERT_TRUE(WriteAll(peer, packet.data(), packet.size()));
+    ASSERT_NO_FATAL_FAILURE(ReadAck(peer, true));
+    ASSERT_RDMA_STATE(rdma::RdmaEndpoint::ESTABLISHED, 
RdmaTransportOf(_socket));
+    EXPECT_EQ(3, RdmaTransportOf(_socket)->_rdma_ep->_handshake_version);
+    ASSERT_NO_FATAL_FAILURE(ExpectNoConnection());
+}
+
+TEST_F(RdmaHandshakeRetryTest, v3_tcp_fallback_does_not_retry) {
+    ASSERT_NO_FATAL_FAILURE(StartRpc());
+    butil::fd_guard peer(AcceptPeer());
+    ASSERT_GE(peer, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(peer, 3));
+    rdma::RdmaHello hello = MakeValidV3Hello();
+    hello.set_block_size(0);
+    std::string packet = MakeV3Packet(hello);
+    ASSERT_TRUE(WriteAll(peer, packet.data(), packet.size()));
+    ASSERT_NO_FATAL_FAILURE(ReadAck(peer, false));
+    ASSERT_NO_FATAL_FAILURE(CompleteRpc(peer));
+    EXPECT_EQ(3, RdmaTransportOf(_socket)->_rdma_ep->_handshake_version);
+    ASSERT_NO_FATAL_FAILURE(ExpectNoConnection());
+}
+
+TEST_F(RdmaHandshakeRetryTest, malformed_v3_reply_is_not_retried) {
+    ASSERT_NO_FATAL_FAILURE(StartRpc());
+    butil::fd_guard peer(AcceptPeer());
+    ASSERT_GE(peer, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(peer, 3));
+    ASSERT_TRUE(WriteAll(peer, "RDM3\0\0\0\0", 8));
+    bthread_id_join(_cntl.call_id());
+    EXPECT_EQ(EPROTO, _cntl.ErrorCode());
+    ASSERT_NO_FATAL_FAILURE(ExpectNoConnection());
+}
+
+TEST_F(RdmaHandshakeRetryTest, truncated_v3_reply_is_not_retried) {
+    ASSERT_NO_FATAL_FAILURE(StartRpc());
+    butil::fd_guard peer(AcceptPeer());
+    ASSERT_GE(peer, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(peer, 3));
+    ASSERT_TRUE(WriteAll(peer, "RDM3", 4));
+    peer.reset(-1);
+    bthread_id_join(_cntl.call_id());
+    EXPECT_EQ(EEOF, _cntl.ErrorCode());
+    ASSERT_NO_FATAL_FAILURE(ExpectNoConnection());
+}
+
+TEST_F(RdmaHandshakeRetryTest, reconnect_failure_finishes_rpc) {
+    ASSERT_NO_FATAL_FAILURE(StartRpc());
+    butil::fd_guard peer(AcceptPeer());
+    ASSERT_GE(peer, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(peer, 3));
+    _listener.reset(-1);
+    peer.reset(-1);
+    bthread_id_join(_cntl.call_id());
+    EXPECT_EQ(ECONNREFUSED, _cntl.ErrorCode());
+    ASSERT_RDMA_STATE(rdma::RdmaEndpoint::FAILED, RdmaTransportOf(_socket));
+}
+
+TEST_F(RdmaHandshakeRetryTest, v2_failure_finishes_without_another_retry) {
+    ASSERT_NO_FATAL_FAILURE(StartRpc());
+    butil::fd_guard first(AcceptPeer());
+    ASSERT_GE(first, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(first, 3));
+    first.reset(-1);
+    butil::fd_guard second(AcceptPeer());
+    ASSERT_GE(second, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(second, 2));
+    second.reset(-1);
+    bthread_id_join(_cntl.call_id());
+    EXPECT_EQ(EEOF, _cntl.ErrorCode());
+    EXPECT_EQ(0, _cntl.retried_count());
+    ASSERT_NO_FATAL_FAILURE(ExpectNoConnection());
+}
+
+TEST_F(RdmaHandshakeRetryTest, retry_keeps_original_deadline) {
+    ASSERT_NO_FATAL_FAILURE(StartRpc(500));
+    butil::fd_guard first(AcceptPeer());
+    ASSERT_GE(first, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(first, 3));
+    int64_t deadline_us = _socket->_connect_deadline_us;
+    bthread_usleep(100000);
+    first.reset(-1);
+    butil::fd_guard second(AcceptPeer());
+    ASSERT_GE(second, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(second, 2));
+    EXPECT_EQ(deadline_us, _socket->_connect_deadline_us);
+    bthread_id_join(_cntl.call_id());
+    EXPECT_EQ(ETIMEDOUT, _cntl.ErrorCode());
+    ASSERT_RDMA_STATE(rdma::RdmaEndpoint::FAILED, RdmaTransportOf(_socket));
+    ASSERT_NO_FATAL_FAILURE(ExpectNoConnection());
+}
+
+TEST_F(RdmaHandshakeRetryTest, silent_v3_peer_obeys_connect_timeout) {
+    ASSERT_NO_FATAL_FAILURE(StartRpc(200));
+    butil::fd_guard peer(AcceptPeer());
+    ASSERT_GE(peer, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(peer, 3));
+    bthread_id_join(_cntl.call_id());
+    EXPECT_EQ(ETIMEDOUT, _cntl.ErrorCode());
+    ASSERT_NO_FATAL_FAILURE(ExpectNoConnection());
+}
+
+TEST_F(RdmaHandshakeRetryTest, 
pooled_handshake_timeout_marks_main_unreachable) {
+    ASSERT_NO_FATAL_FAILURE(CheckPooledTimeoutConversion(false));
+}
+
+TEST_F(RdmaHandshakeRetryTest, pooled_retry_timeout_marks_main_unreachable) {
+    ASSERT_NO_FATAL_FAILURE(CheckPooledTimeoutConversion(true));
+}
+
+TEST_F(RdmaHandshakeRetryTest, failed_socket_stops_handshake_without_deadline) 
{
+    ASSERT_NO_FATAL_FAILURE(StartRpc(0));
+    butil::fd_guard peer(AcceptPeer());
+    ASSERT_GE(peer, 0);
+    ASSERT_NO_FATAL_FAILURE(ReadHello(peer, 3));
+    _socket->SetFailed(ECANCELED, "Cancel during handshake");
+    bthread_id_join(_cntl.call_id());
+    EXPECT_EQ(ECANCELED, _cntl.ErrorCode());
+    ASSERT_RDMA_STATE(rdma::RdmaEndpoint::FAILED, RdmaTransportOf(_socket));
+    ASSERT_NO_FATAL_FAILURE(ExpectNoConnection());
+}
+
+class RdmaHandshakeFdReplacementTest : public RdmaTest {
+protected:
+    void SetUp() override {
+        int fds[2];
+        ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
+        butil::fd_guard original(fds[0]);
+        _old_peer.reset(fds[1]);
+        _old_fd = original;
+
+        ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
+        _new_fd.reset(fds[0]);
+        _new_peer.reset(fds[1]);
+
+        SocketOptions options;
+        options.fd = original.release();
+        options.socket_mode = SOCKET_MODE_RDMA;
+        // Exercise both removal of the old consumer and registration of the
+        // replacement. No peer sends data, so the callback stays idle.
+        options.on_edge_triggered_events = [](Socket*) {};
+        SocketId id;
+        ASSERT_EQ(0, Socket::Create(options, &id));
+        ASSERT_EQ(0, Socket::Address(id, &_socket));
+    }
+
+    void TearDown() override {
+        if (_socket) {
+            _socket->SetFailed(ECANCELED, "Replacement test finished");
+            _socket.reset();
+        }
+        RdmaTest::TearDown();
+    }
+
+    void CheckRejectedReplacement(int expected_error) {
+        int new_fd = _new_fd.release();
+        int rc = 
RdmaTransportOf(_socket)->GetRdmaEp()->ReplaceHandshakeFd(new_fd);
+        int error = errno;
+        EXPECT_EQ(-1, rc);
+        EXPECT_EQ(expected_error, error);
+        EXPECT_EQ(_old_fd, _socket->fd());
+        EXPECT_NE(-1, fcntl(_old_fd, F_GETFD));
+        EXPECT_EQ(-1, fcntl(new_fd, F_GETFD));
+        EXPECT_EQ(EBADF, errno);
+        char byte;
+        EXPECT_EQ(0, recv(_new_peer, &byte, sizeof(byte), MSG_DONTWAIT));
+
+        // A caller may retain the failed Socket; only the replacement must
+        // close immediately. Recycling must still close the original fd.
+        _socket->SetFailed(ECANCELED, "Replacement test finished");
+        _socket.reset();
+        EXPECT_EQ(0, recv(_old_peer, &byte, sizeof(byte), MSG_DONTWAIT));
+    }
+
+    int _old_fd = -1;
+    butil::fd_guard _old_peer;
+    butil::fd_guard _new_fd;
+    butil::fd_guard _new_peer;
+    SocketUniquePtr _socket;
+};
+
+TEST_F(RdmaHandshakeFdReplacementTest, failed_socket_rejects_replacement) {
+    // Model SetFailed after ReconnectForHandshake's last pre-check and before
+    // fd replacement, without relying on thread scheduling to hit the window.
+    ASSERT_EQ(0, _socket->SetFailed(ECANCELED, "Cancel before replacement"));
+    CheckRejectedReplacement(ECANCELED);
+}
+
+TEST_F(RdmaHandshakeFdReplacementTest, expired_deadline_rejects_replacement) {
+    // The same window also permits the original connection deadline to expire.
+    _socket->_connect_deadline_us = butil::gettimeofday_us() - 1;
+    CheckRejectedReplacement(ETIMEDOUT);
+}
+
+
+TEST_F(RdmaTest, v3_client_hello_bytes_baseline) {
     butil::fd_guard sockfd(butil::tcp_listen(g_ep));
     EXPECT_TRUE(sockfd >= 0);
 
@@ -2408,8 +2849,6 @@ TEST_F(RdmaTest, v3_server_rejects_invalid_mtu) {
 
 // Verify the client includes its local MTU in the v3 hello.
 TEST_F(RdmaTest, v3_client_hello_includes_mtu) {
-    HandshakeVersionFlag _hsv(3);
-
     butil::fd_guard sockfd(butil::tcp_listen(g_ep));
     EXPECT_TRUE(sockfd >= 0);
 
@@ -2640,7 +3079,7 @@ static void SendEchoRpcsInRounds(Channel& channel) {
     }
 }
 
-TEST_P(RdmaRpcTest, rdma_client_to_rdma_server) {
+TEST_F(RdmaRpcTest, rdma_client_to_rdma_server) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -2659,7 +3098,7 @@ TEST_P(RdmaRpcTest, rdma_client_to_rdma_server) {
     StopServer();
 }
 
-TEST_P(RdmaRpcTest, tcp_client_to_tcp_server) {
+TEST_F(RdmaRpcTest, tcp_client_to_tcp_server) {
     StartServer(false);
 
     Channel channel;
@@ -2673,7 +3112,7 @@ TEST_P(RdmaRpcTest, tcp_client_to_tcp_server) {
     StopServer();
 }
 
-TEST_P(RdmaRpcTest, tcp_client_to_rdma_server) {
+TEST_F(RdmaRpcTest, tcp_client_to_rdma_server) {
     StartServer();
 
     Channel channel;
@@ -2687,7 +3126,7 @@ TEST_P(RdmaRpcTest, tcp_client_to_rdma_server) {
     StopServer();
 }
 
-TEST_P(RdmaRpcTest, rdma_client_to_tcp_server) {
+TEST_F(RdmaRpcTest, rdma_client_to_tcp_server) {
     StartServer(false);
 
     Channel channel;
@@ -2702,7 +3141,7 @@ TEST_P(RdmaRpcTest, rdma_client_to_tcp_server) {
     StopServer();
 }
 
-TEST_P(RdmaRpcTest, tcp_client_to_rdma_server_short_connection) {
+TEST_F(RdmaRpcTest, tcp_client_to_rdma_server_short_connection) {
     StartServer();
 
     Channel channel;
@@ -2725,7 +3164,7 @@ static const int CHURN_ROUND_NUM = 16;
 static const int CHURN_RPC_NUM = 64;
 static const size_t CHURN_ATTACH_SIZE = 32 * 1024;
 
-TEST_P(RdmaRpcTest, rdma_server_survives_connection_churn) {
+TEST_F(RdmaRpcTest, rdma_server_survives_connection_churn) {
     StartServer();
 
     ChannelOptions chan_options;
@@ -2774,7 +3213,7 @@ void DumpRdmaEndpointInfo(Socket* client, Socket* server) 
{
     
static_cast<RdmaTransport*>(server->_transport.get())->_rdma_ep->DebugInfo(std::cout);
 }
 
-TEST_P(RdmaRpcTest, send_rpcs_in_one_qp) {
+TEST_F(RdmaRpcTest, send_rpcs_in_one_qp) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -2857,7 +3296,7 @@ TEST_P(RdmaRpcTest, send_rpcs_in_one_qp) {
     StopServer();
 }
 
-TEST_P(RdmaRpcTest, send_rpc_in_many_qp) {
+TEST_F(RdmaRpcTest, send_rpc_in_many_qp) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -2926,7 +3365,7 @@ TEST_P(RdmaRpcTest, send_rpc_in_many_qp) {
     }
 }
 
-TEST_P(RdmaRpcTest, send_rpcs_as_pooled_connection) {
+TEST_F(RdmaRpcTest, send_rpcs_as_pooled_connection) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -2967,7 +3406,7 @@ TEST_P(RdmaRpcTest, send_rpcs_as_pooled_connection) {
     StopServer();
 }
 
-TEST_P(RdmaRpcTest, send_rpcs_as_short_connection) {
+TEST_F(RdmaRpcTest, send_rpcs_as_short_connection) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -3008,7 +3447,7 @@ TEST_P(RdmaRpcTest, send_rpcs_as_short_connection) {
     StopServer();
 }
 
-TEST_P(RdmaRpcTest, server_stop_during_rpc) {
+TEST_F(RdmaRpcTest, server_stop_during_rpc) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -3046,7 +3485,7 @@ TEST_P(RdmaRpcTest, server_stop_during_rpc) {
     }
 }
 
-TEST_P(RdmaRpcTest, server_close_during_rpc) {
+TEST_F(RdmaRpcTest, server_close_during_rpc) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -3088,7 +3527,7 @@ TEST_P(RdmaRpcTest, server_close_during_rpc) {
     StopServer();
 }
 
-TEST_P(RdmaRpcTest, client_close_during_rpc) {
+TEST_F(RdmaRpcTest, client_close_during_rpc) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -3128,7 +3567,7 @@ TEST_P(RdmaRpcTest, client_close_during_rpc) {
     StopServer();
 }
 
-TEST_P(RdmaRpcTest, rdma_client_close_during_rpc_repeatedly) {
+TEST_F(RdmaRpcTest, rdma_client_close_during_rpc_repeatedly) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -3168,7 +3607,7 @@ TEST_P(RdmaRpcTest, 
rdma_client_close_during_rpc_repeatedly) {
     StopServer();
 }
 
-TEST_P(RdmaRpcTest, verbs_error_handling) {
+TEST_F(RdmaRpcTest, verbs_error_handling) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -3214,7 +3653,7 @@ TEST_P(RdmaRpcTest, verbs_error_handling) {
     StopServer();
 }
 
-TEST_P(RdmaRpcTest, rdma_use_parallel_channel) {
+TEST_F(RdmaRpcTest, rdma_use_parallel_channel) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -3246,7 +3685,7 @@ TEST_P(RdmaRpcTest, rdma_use_parallel_channel) {
     StopServer();
 }
 
-TEST_P(RdmaRpcTest, rdma_use_selective_channel) {
+TEST_F(RdmaRpcTest, rdma_use_selective_channel) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -3278,7 +3717,7 @@ TEST_P(RdmaRpcTest, rdma_use_selective_channel) {
 
 static void MockFree(void* buf) { }
 
-TEST_P(RdmaRpcTest, send_rpcs_with_user_defined_iobuf) {
+TEST_F(RdmaRpcTest, send_rpcs_with_user_defined_iobuf) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -3342,7 +3781,7 @@ TEST_P(RdmaRpcTest, send_rpcs_with_user_defined_iobuf) {
     StopServer();
 }
 
-TEST_P(RdmaRpcTest, try_memory_pool_empty) {
+TEST_F(RdmaRpcTest, try_memory_pool_empty) {
     if (!FLAGS_rdma_test_enable) {
         return;
     }
@@ -3381,19 +3820,6 @@ TEST_P(RdmaRpcTest, try_memory_pool_empty) {
     StopServer();
 }
 
-// Run every TEST_P(RdmaRpcTest, ...) above twice: once with the
-// client-side handshake forced to v2 ("RDMA" magic + fixed-layout
-// HelloMessage), once with v3 ("RDM3" magic + protobuf RdmaHello).
-// The server always accepts both via magic-byte dispatch, so this
-// proves the upper-layer RPC paths behave identically under either
-// wire format.
-INSTANTIATE_TEST_SUITE_P(
-    HandshakeVersion, RdmaRpcTest,
-    ::testing::Values(2, 3),
-    [](const ::testing::TestParamInfo<int>& info) {
-        return std::string("v") + std::to_string(info.param);
-    });
-
 static const uint32_t MELLANOX = 0x02c9;
 static const uint32_t INTEL = 0x8086;
 static const uint32_t ALIBABA = 0x1ded;
diff --git a/test/brpc_socket_unittest.cpp b/test/brpc_socket_unittest.cpp
index ec4d1e156..4f60825ef 100644
--- a/test/brpc_socket_unittest.cpp
+++ b/test/brpc_socket_unittest.cpp
@@ -21,9 +21,11 @@
 
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <poll.h>
 #include <unistd.h>
 #include <fcntl.h>  // F_GETFD
 #include <thread>
+#include <vector>
 #include <gtest/gtest.h>
 #include <gflags/gflags.h>
 #include "gperftools_helper.h"
@@ -369,6 +371,109 @@ private:
     bool _called_start_connect; 
 };
 
+class PausedAppConnect : public brpc::AppConnect {
+public:
+    void StartConnect(const brpc::Socket*,
+                      void (*callback)(int, void*), void* arg) override {
+        done = callback;
+        data = arg;
+        started.signal();
+    }
+    void StopConnect(brpc::Socket*) override {}
+
+    static void Complete(int error_code, void* arg) {
+        auto self = static_cast<PausedAppConnect*>(arg);
+        ++self->completed;
+        self->error = error_code;
+        auto callback = self->done;
+        self->done = nullptr;
+        if (callback) {
+            callback(error_code, self->data);
+        }
+    }
+
+    bthread::CountdownEvent started;
+    void (*done)(int, void*) = nullptr;
+    void* data = nullptr;
+    int completed = 0;
+    int error = 0;
+};
+
+TEST_F(SocketTest, failed_socket_before_handshake_releases_queued_writes) {
+    std::vector<brpc::SocketMode> modes;
+#if BRPC_WITH_RDMA
+    modes.push_back(brpc::SOCKET_MODE_RDMA);
+#endif
+#if BRPC_WITH_URMA
+    modes.push_back(brpc::SOCKET_MODE_URMA);
+#endif
+#if BRPC_WITH_UBRING
+    modes.push_back(brpc::SOCKET_MODE_UBRING);
+#endif
+    if (modes.empty()) {
+        GTEST_SKIP() << "Requires RDMA, URMA or UBRING transport";
+    }
+    for (auto mode : modes) {
+        SCOPED_TRACE(mode);
+        butil::EndPoint point;
+        butil::fd_guard listener;
+        ASSERT_NO_FATAL_FAILURE(ListenOnFreePort(&point, &listener));
+        int connected_fd = -1;
+        {
+            brpc::SocketOptions options;
+            options.socket_mode = mode;
+            options.remote_side = point;
+            brpc::SocketId id;
+            ASSERT_EQ(0, brpc::Socket::Create(options, &id));
+            brpc::SocketUniquePtr socket;
+            ASSERT_EQ(0, brpc::Socket::Address(id, &socket));
+            auto connect = socket->_app_connect;
+            auto paused = std::make_shared<PausedAppConnect>();
+            socket->_app_connect = paused;
+            WaitData results[2];
+            auto cleanup = butil::MakeScopeGuard([&] {
+                socket->SetFailed();
+                if (paused->done) {
+                    PausedAppConnect::Complete(brpc::EFAILEDSOCKET, 
paused.get());
+                }
+            });
+
+            timespec deadline = butil::seconds_from_now(5);
+            for (int i = 0; i < 2; ++i) {
+                brpc::Socket::WriteOptions write_options;
+                write_options.abstime = &deadline;
+                ASSERT_EQ(0, bthread_id_create2(&write_options.id_wait,
+                                               &results[i], OnWaitIdReset));
+                butil::IOBuf request;
+                request.append("queued handshake request");
+                ASSERT_EQ(0, socket->Write(&request, &write_options));
+                if (i == 0) {
+                    ASSERT_EQ(0, paused->started.timed_wait(deadline));
+                }
+            }
+            connected_fd = socket->fd();
+            ASSERT_GE(connected_fd, 0);
+            ASSERT_NE(nullptr, 
socket->_write_head.load(butil::memory_order_acquire));
+
+            // Reproduce failure after ResetFileDescriptor but before the
+            // transport handshake takes its own reference with Address().
+            ASSERT_EQ(0, socket->SetFailed(ECANCELED, "Canceled before 
handshake"));
+            connect->StartConnect(socket.get(), PausedAppConnect::Complete, 
paused.get());
+            EXPECT_EQ(1, paused->completed);
+            EXPECT_EQ(brpc::EFAILEDSOCKET, paused->error);
+            EXPECT_EQ(nullptr, 
socket->_write_head.load(butil::memory_order_acquire));
+            for (auto& result : results) {
+                EXPECT_EQ(ECANCELED, result.error_code);
+            }
+            for (int i = 0; i < 100 && socket->nref() != 1; ++i) {
+                bthread_usleep(1000);
+            }
+            EXPECT_EQ(1, socket->nref());
+        }
+        ASSERT_NO_FATAL_FAILURE(WaitForClosedFd(connected_fd));
+    }
+}
+
 TEST_F(SocketTest, single_threaded_connect_and_write) {
     // FIXME(gejun): Messenger has to be new otherwise quitting may crash.
     // It is intentionally never deleted; mark it so it is not a reported leak.
@@ -552,6 +657,94 @@ TEST_F(SocketTest, fail_to_connect) {
     ASSERT_EQ(-1, brpc::Socket::Address(id, &ptr));
 }
 
+// A full socketpair makes EPOLLOUT stay silent without relying on a dropped
+// TCP SYN or the host's routing configuration.
+class SocketConnectCancellationTest : public SocketTest {
+protected:
+    void SetUp() override {
+        int fds[2];
+        ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
+        _writer.reset(fds[0]);
+        _reader.reset(fds[1]);
+        ASSERT_EQ(0, butil::make_non_blocking(_writer));
+        char buf[4096] = {};
+        while (write(_writer, buf, sizeof(buf)) > 0) {}
+        ASSERT_EQ(EAGAIN, errno);
+        pollfd pfd = {_writer, POLLOUT, 0};
+        ASSERT_EQ(0, poll(&pfd, 1, 0));
+
+        brpc::SocketOptions options;
+        brpc::SocketId id;
+        ASSERT_EQ(0, brpc::Socket::Create(options, &id));
+        ASSERT_EQ(0, brpc::Socket::Address(id, &_socket));
+    }
+
+    void TearDown() override {
+        if (_socket) {
+            _socket->SetFailed(ECANCELED, "Cancellation test finished");
+        }
+    }
+
+    static void* WaitWithoutDeadline(void* arg) {
+        auto* test = static_cast<SocketConnectCancellationTest*>(arg);
+        test->_rc = test->_socket->WaitEpollOut(test->_writer, false, nullptr, 
true);
+        test->_error = errno;
+        test->_finished.signal();
+        return nullptr;
+    }
+
+    butil::fd_guard _writer;
+    butil::fd_guard _reader;
+    brpc::SocketUniquePtr _socket;
+    bthread::CountdownEvent _finished;
+    int _rc = 0;
+    int _error = 0;
+};
+
+TEST_F(SocketConnectCancellationTest, cancel_before_wait_snapshot) {
+    // Reproduce cancellation after the caller's pre-check but before the wait
+    // takes its butex snapshot. A timeout only bounds failures of this test.
+    ASSERT_FALSE(_socket->Failed());
+    ASSERT_EQ(0, _socket->SetFailed(ECANCELED, "Cancel before wait"));
+    timespec deadline = butil::milliseconds_from_now(200);
+    ASSERT_EQ(-1, _socket->WaitEpollOut(_writer, false, &deadline, true));
+    EXPECT_EQ(ECANCELED, errno);
+}
+
+TEST_F(SocketConnectCancellationTest, cancel_wait_without_deadline) {
+    bthread_t tid;
+    ASSERT_EQ(0, bthread_start_background(
+        &tid, nullptr, WaitWithoutDeadline, this));
+    auto join_waiter = butil::MakeScopeGuard([&] {
+        // Also unblock a broken implementation before joining, so this test
+        // cannot leave an indefinitely parked bthread on a failed assertion.
+        _reader.reset(-1);
+        bthread_join(tid, nullptr);
+    });
+    ASSERT_EQ(ETIMEDOUT, 
_finished.timed_wait(butil::milliseconds_from_now(10)));
+    ASSERT_EQ(0, _socket->SetFailed(ECANCELED, "Cancel pending wait"));
+    ASSERT_EQ(0, _finished.timed_wait(butil::seconds_from_now(5)));
+    EXPECT_EQ(-1, _rc);
+    EXPECT_EQ(ECANCELED, _error);
+}
+
+TEST_F(SocketTest, synchronous_health_connect_on_failed_socket) {
+    butil::EndPoint point;
+    butil::fd_guard listener;
+    ASSERT_NO_FATAL_FAILURE(ListenOnFreePort(&point, &listener));
+    brpc::SocketOptions options;
+    options.remote_side = point;
+    brpc::SocketId id;
+    ASSERT_EQ(0, brpc::Socket::Create(options, &id));
+    brpc::SocketUniquePtr s;
+    ASSERT_EQ(0, brpc::Socket::Address(id, &s));
+    ASSERT_EQ(0, s->SetFailed(ECANCELED, "Health check on a failed socket"));
+    timespec deadline = butil::seconds_from_now(5);
+    butil::fd_guard fd(s->Connect(&deadline, nullptr, nullptr));
+    ASSERT_GE(fd, 0) << berror();
+    EXPECT_TRUE(s->Failed());
+}
+
 TEST_F(SocketTest, not_health_check_when_nref_hits_0) {
     brpc::SocketId id = 8888;
     butil::EndPoint point/*not listened*/;


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

Reply via email to