[前][次][番号順一覧][スレッド一覧]

ruby-changes:51256

From: normal <ko1@a...>
Date: Fri, 18 May 2018 17:01:15 +0900 (JST)
Subject: [ruby-changes:51256] normal:r63462 (trunk): thread.c (rb_wait_for_single_fd): do not leak EINTR on timeout

normal	2018-05-18 17:01:07 +0900 (Fri, 18 May 2018)

  New Revision: 63462

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63462

  Log:
    thread.c (rb_wait_for_single_fd): do not leak EINTR on timeout
    
    We must not leak EINTR to users in case a signal hits a
    ppoll/select caller right when (or just before) the timeout
    expires.  In other words, the timeout should take precedence
    over the -1 result from ppoll or select.
    
    We also try one more time in case of EINTR with a zero timeout,
    since technically the syscall finished before timing out if
    it returns EINTR.
    
    Regression appeared in r62457
    ("thread.c (update_timespec): use timespec_update_expire",
     commit e6bf0128add103730d8c96727f3ed4dab95088e4)
    and is not in any stable release of Ruby.

  Modified files:
    trunk/thread.c
Index: thread.c
===================================================================
--- thread.c	(revision 63461)
+++ thread.c	(revision 63462)
@@ -3783,28 +3783,38 @@ rb_fd_set(int fd, rb_fdset_t *set) https://github.com/ruby/ruby/blob/trunk/thread.c#L3783
 #define rb_fd_no_init(fds) (void)(fds)
 #endif
 
-static inline int
-retryable(int e)
+static int
+wait_retryable(int *result, int errnum, struct timespec *timeout,
+                const struct timespec *end)
 {
-    if (e == EINTR) return TRUE;
+    if (*result < 0) {
+        switch (errnum) {
+          case EINTR:
 #ifdef ERESTART
-    if (e == ERESTART) return TRUE;
+          case ERESTART:
 #endif
+            *result = 0;
+            if (timeout && timespec_update_expire(timeout, end)) {
+                timeout->tv_sec = 0;
+                timeout->tv_nsec = 0;
+            }
+            return TRUE;
+        }
+        return FALSE;
+    }
+    else if (*result == 0) {
+	/* check for spurious wakeup */
+        if (timeout) {
+            return !timespec_update_expire(timeout, end);
+        }
+        return TRUE;
+    }
     return FALSE;
 }
 
 #define restore_fdset(fds1, fds2) \
     ((fds1) ? rb_fd_dup(fds1, fds2) : (void)0)
 
-static inline int
-update_timespec(struct timespec *timeout, const struct timespec *end)
-{
-    if (timeout) {
-        return !timespec_update_expire(timeout, end);
-    }
-    return TRUE;
-}
-
 static int
 do_select(int n, rb_fdset_t *const readfds, rb_fdset_t *const writefds,
 	  rb_fdset_t *const exceptfds, struct timeval *timeout)
@@ -3827,7 +3837,7 @@ do_select(int n, rb_fdset_t *const readf https://github.com/ruby/ruby/blob/trunk/thread.c#L3837
     (restore_fdset(readfds, &orig_read), \
      restore_fdset(writefds, &orig_write), \
      restore_fdset(exceptfds, &orig_except), \
-     update_timespec(tsp, &end))
+     TRUE)
 
     if (timeout) {
         getclockofday(&end);
@@ -3852,13 +3862,16 @@ do_select(int n, rb_fdset_t *const readf https://github.com/ruby/ruby/blob/trunk/thread.c#L3862
 	}, ubf_select, th, FALSE);
 
 	RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
-    } while (result < 0 && retryable(errno = lerrno) && do_select_update());
+    } while (wait_retryable(&result, lerrno, tsp, &end) && do_select_update());
 
 #define fd_term(f) if (f##fds) rb_fd_term(&orig_##f)
     fd_term(read);
     fd_term(write);
     fd_term(except);
 #undef fd_term
+    if (result < 0) {
+	errno = lerrno;
+    }
 
     return result;
 }
@@ -3992,9 +4005,11 @@ rb_wait_for_single_fd(int fd, int events https://github.com/ruby/ruby/blob/trunk/thread.c#L4005
         }, ubf_select, th, FALSE);
 
         RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
-    } while (result < 0 && retryable(errno = lerrno) &&
-            update_timespec(tsp, &end));
-    if (result < 0) return -1;
+    } while (wait_retryable(&result, lerrno, tsp, &end));
+    if (result < 0) {
+	errno = lerrno;
+	return -1;
+    }
 
     if (fds.revents & POLLNVAL) {
 	errno = EBADF;

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]