From 31bc6268c7cde8a837490c5a63dee891b23cb62a Mon Sep 17 00:00:00 2001 From: Liu Jiang Date: Mon, 23 Mar 2020 21:24:45 +0800 Subject: [PATCH] Unregister fd from epoll_fd when closing a connection When closing a http connection, the corresponding file descriptor should be unregistered from the epoll fd. Signed-off-by: Liu Jiang --- src/server.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/server.rs b/src/server.rs index 657371a..e915b97 100644 --- a/src/server.rs +++ b/src/server.rs @@ -367,8 +367,16 @@ impl HttpServer { } // Remove dead connections. - self.connections - .retain(|_, client_connection| !client_connection.is_done()); + let epoll_fd = self.epoll_fd; + self.connections.retain(|rawfd, client_connection| { + if client_connection.is_done() { + // The rawfd should have been registered to the epoll fd. + Self::epoll_del(epoll_fd, *rawfd).unwrap(); + false + } else { + true + } + }); Ok(parsed_requests) } @@ -518,6 +526,17 @@ impl HttpServer { ) .map_err(ServerError::IOError) } + + /// Removes a stream to the `epoll` notification structure. + fn epoll_del(epoll_fd: RawFd, stream_fd: RawFd) -> Result<()> { + epoll::ctl( + epoll_fd, + epoll::ControlOptions::EPOLL_CTL_DEL, + stream_fd, + epoll::Event::new(epoll::Events::EPOLLIN, stream_fd as u64), + ) + .map_err(ServerError::IOError) + } } #[cfg(test)]