From 2f8275009854599ec7de68dbef795e82b6b5fb30 Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Tue, 11 Jul 2023 14:11:25 -0600 Subject: [PATCH] unix: match kqueue and epoll code (#4091) Match the implementation for linux.c to kqueue.c in the code around the calls to kevent and epoll. In linux.c the code was made more DRY by moving the nfds check up (including a comment of why it's possible) and combining two if checks into one. In kqueue.c the assert to check the timeout when nfds == 0 has been moved to be called directly after the EINTR check. Since it should always be true regardless. Ref: https://github.com/libuv/libuv/pull/3893 Ref: https://github.com/nodejs/node/issues/48490 --- src/unix/kqueue.c | 5 +++-- src/unix/linux.c | 37 ++++++++----------------------------- 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/src/unix/kqueue.c b/src/unix/kqueue.c index b78242d3be4..6cb0ef2aa75 100644 --- a/src/unix/kqueue.c +++ b/src/unix/kqueue.c @@ -262,6 +262,9 @@ void uv__io_poll(uv_loop_t* loop, int timeout) { if (nfds == -1) assert(errno == EINTR); + else if (nfds == 0) + /* Unlimited timeout should only return with events or signal. */ + assert(timeout != -1); if (pset != NULL) pthread_sigmask(SIG_UNBLOCK, pset, NULL); @@ -286,8 +289,6 @@ void uv__io_poll(uv_loop_t* loop, int timeout) { timeout = user_timeout; reset_timeout = 0; } else if (nfds == 0) { - /* Reached the user timeout value. */ - assert(timeout != -1); return; } diff --git a/src/unix/linux.c b/src/unix/linux.c index eac18e86b04..1b2dbe0b95c 100644 --- a/src/unix/linux.c +++ b/src/unix/linux.c @@ -1395,41 +1395,20 @@ void uv__io_poll(uv_loop_t* loop, int timeout) { */ SAVE_ERRNO(uv__update_time(loop)); - if (nfds == 0) { + if (nfds == -1) + assert(errno == EINTR); + else if (nfds == 0) + /* Unlimited timeout should only return with events or signal. */ assert(timeout != -1); + if (nfds == 0 || nfds == -1) { if (reset_timeout != 0) { timeout = user_timeout; reset_timeout = 0; + } else if (nfds == 0) { + return; } - if (timeout == -1) - continue; - - if (timeout == 0) - break; - - /* We may have been inside the system call for longer than |timeout| - * milliseconds so we need to update the timestamp to avoid drift. - */ - goto update_timeout; - } - - if (nfds == -1) { - if (errno != EINTR) - abort(); - - if (reset_timeout != 0) { - timeout = user_timeout; - reset_timeout = 0; - } - - if (timeout == -1) - continue; - - if (timeout == 0) - break; - /* Interrupted by a signal. Update timeout and poll again. */ goto update_timeout; } @@ -1540,13 +1519,13 @@ void uv__io_poll(uv_loop_t* loop, int timeout) { break; } +update_timeout: if (timeout == 0) break; if (timeout == -1) continue; -update_timeout: assert(timeout > 0); real_timeout -= (loop->time - base);