Copilot commented on code in PR #3745:
URL: https://github.com/apache/brpc/pull/3745#discussion_r4576456067
##########
src/brpc/rdma/rdma_endpoint.cpp:
##########
@@ -449,29 +481,62 @@ 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;
+ }
+ 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;
+ }
+ new_fd.release();
Review Comment:
`SetFailed()` can race after the final `GetHandshakeWaitTime()` check and
before `ResetFileDescriptor()`. In that window the socket is already marked
failed, but this code still installs and registers `new_fd`, then continues the
v2 handshake on a failed socket (and closes the old descriptor). Please
serialize the descriptor replacement with the failure path, or otherwise make
the swap abort and close `new_fd` when the socket becomes failed.
--
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]