From f275c048ec0f0d639c311c06f6dc54270a0275b8 Mon Sep 17 00:00:00 2001 From: Coldwings Date: Mon, 9 Sep 2024 15:57:11 +0800 Subject: [PATCH] FIX: wait_for_fd in epoll should atleast put into sleep for a tiny moment --- io/epoll.cpp | 4 +++- net/pooled_socket.cpp | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/io/epoll.cpp b/io/epoll.cpp index a45ec09f..794fd066 100644 --- a/io/epoll.cpp +++ b/io/epoll.cpp @@ -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 diff --git a/net/pooled_socket.cpp b/net/pooled_socket.cpp index 541c44d9..668798b7 100644 --- a/net/pooled_socket.cpp +++ b/net/pooled_socket.cpp @@ -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); } @@ -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; } @@ -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;