Skip to content

Commit

Permalink
FIX: wait_for_fd in epoll should atleast put into sleep for a tiny mo…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
Coldwings committed Sep 18, 2024
1 parent e951a93 commit f275c04
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 3 additions & 1 deletion io/epoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ ok: entry.interests |= eint;
return rm_interest({fd, EVENT_RWE| ONE_SHOT, 0}); // remove fd from epoll
int ret = add_interest({fd, interest | ONE_SHOT, CURRENT});
if (ret < 0) LOG_ERROR_RETURN(0, -1, "failed to add event interest");
ret = thread_usleep(timeout);
// if timeout is just simple 0, wait for a tiny little moment
// so that events can be collect.
ret = thread_usleep(timeout.timeout() ? timeout : Timeout(10));
ERRNO err;
if (ret == -1 && err.no == EOK) {
return 0; // Event arrived
Expand Down
9 changes: 5 additions & 4 deletions net/pooled_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ class TCPSocketPool : public ForwardSocketClient {
photon::Timer timer;

// all fd < 0 treated as socket not based on fd
// and always alive. Using such socket needs user
// and always reuseable. Using such socket needs user
// to check if connected socket is still usable.
bool stream_alive(int fd) {
// if there still have unread bytes in strema, it should be closed.
bool stream_reusable(int fd) {
return (fd < 0) || (wait_for_fd_readable(fd, 0) != 0);
}

Expand Down Expand Up @@ -196,7 +197,7 @@ class TCPSocketPool : public ForwardSocketClient {
if (!stream) {
stream = m_underlay->connect(remote, local);
if (!stream) return nullptr;
} else if (!stream_alive(stream->get_underlay_fd())) {
} else if (!stream_reusable(stream->get_underlay_fd())) {
delete stream;
goto again;
}
Expand Down Expand Up @@ -231,7 +232,7 @@ class TCPSocketPool : public ForwardSocketClient {
bool release(const EndPoint& ep, ISocketStream* stream) {
auto fd = stream->get_underlay_fd();
ERRNO err;
if (!stream_alive(fd)) return false;
if (!stream_reusable(fd)) return false;
auto node = new StreamListNode(ep, stream, fd, TTL_us);
push_into_pool(node);
errno = err.no;
Expand Down

0 comments on commit f275c04

Please sign in to comment.