Blame SOURCES/make-4.2.1-nonblocking-reads.patch

Pablo Greco 75b106
From b552b05251980f693c729e251f93f5225b400714 Mon Sep 17 00:00:00 2001
Pablo Greco 75b106
From: Paul Smith <psmith@gnu.org>
Pablo Greco 75b106
Date: Sat, 3 Jun 2017 16:20:51 -0400
Pablo Greco 75b106
Subject: [SV 51159] Use a non-blocking read with pselect to avoid hangs.
Pablo Greco 75b106
Pablo Greco 75b106
* posixos.c (set_blocking): Set blocking on a file descriptor.
Pablo Greco 75b106
(jobserver_setup): Set non-blocking on the jobserver read side.
Pablo Greco 75b106
(jobserver_parse_auth): Ditto.
Pablo Greco 75b106
(jobserver_acquire_all): Set blocking to avoid a busy-wait loop.
Pablo Greco 75b106
(jobserver_acquire): If the non-blocking read() returns without
Pablo Greco 75b106
taking a token then try again.
Pablo Greco 75b106
Pablo Greco 75b106
diff --git a/posixos.c b/posixos.c
Pablo Greco 75b106
index e642d7f..dbafa51 100644
Pablo Greco 75b106
--- a/posixos.c
Pablo Greco 75b106
+++ b/posixos.c
Pablo Greco 75b106
@@ -62,6 +62,24 @@ make_job_rfd (void)
Pablo Greco 75b106
 #endif
Pablo Greco 75b106
 }
Pablo Greco 75b106
 
Pablo Greco 75b106
+static void
Pablo Greco 75b106
+set_blocking (int fd, int blocking)
Pablo Greco 75b106
+{
Pablo Greco 75b106
+  // If we're not using pselect() don't change the blocking
Pablo Greco 75b106
+#ifdef HAVE_PSELECT
Pablo Greco 75b106
+  int flags;
Pablo Greco 75b106
+  EINTRLOOP (flags, fcntl (fd, F_GETFL));
Pablo Greco 75b106
+  if (flags >= 0)
Pablo Greco 75b106
+    {
Pablo Greco 75b106
+      int r;
Pablo Greco 75b106
+      flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
Pablo Greco 75b106
+      EINTRLOOP (r, fcntl (fd, F_SETFL, flags));
Pablo Greco 75b106
+      if (r < 0)
Pablo Greco 75b106
+        pfatal_with_name ("fcntl(O_NONBLOCK)");
Pablo Greco 75b106
+    }
Pablo Greco 75b106
+#endif
Pablo Greco 75b106
+}
Pablo Greco 75b106
+
Pablo Greco 75b106
 unsigned int
Pablo Greco 75b106
 jobserver_setup (int slots)
Pablo Greco 75b106
 {
Pablo Greco 75b106
@@ -86,6 +104,9 @@ jobserver_setup (int slots)
Pablo Greco 75b106
         pfatal_with_name (_("init jobserver pipe"));
Pablo Greco 75b106
     }
Pablo Greco 75b106
 
Pablo Greco 75b106
+  /* When using pselect() we want the read to be non-blocking.  */
Pablo Greco 75b106
+  set_blocking (job_fds[0], 0);
Pablo Greco 75b106
+
Pablo Greco 75b106
   return 1;
Pablo Greco 75b106
 }
Pablo Greco 75b106
 
Pablo Greco 75b106
@@ -121,6 +142,9 @@ jobserver_parse_auth (const char *auth)
Pablo Greco 75b106
       return 0;
Pablo Greco 75b106
     }
Pablo Greco 75b106
 
Pablo Greco 75b106
+  /* When using pselect() we want the read to be non-blocking.  */
Pablo Greco 75b106
+  set_blocking (job_fds[0], 0);
Pablo Greco 75b106
+
Pablo Greco 75b106
   return 1;
Pablo Greco 75b106
 }
Pablo Greco 75b106
 
Pablo Greco 75b106
@@ -169,7 +193,10 @@ jobserver_acquire_all (void)
Pablo Greco 75b106
 {
Pablo Greco 75b106
   unsigned int tokens = 0;
Pablo Greco 75b106
 
Pablo Greco 75b106
-  /* Close the write side, so the read() won't hang.  */
Pablo Greco 75b106
+  /* Use blocking reads to wait for all outstanding jobs.  */
Pablo Greco 75b106
+  set_blocking (job_fds[0], 1);
Pablo Greco 75b106
+
Pablo Greco 75b106
+  /* Close the write side, so the read() won't hang forever.  */
Pablo Greco 75b106
   close (job_fds[1]);
Pablo Greco 75b106
   job_fds[1] = -1;
Pablo Greco 75b106
 
Pablo Greco 75b106
@@ -236,18 +263,12 @@ jobserver_pre_acquire (void)
Pablo Greco 75b106
 unsigned int
Pablo Greco 75b106
 jobserver_acquire (int timeout)
Pablo Greco 75b106
 {
Pablo Greco 75b106
-  sigset_t empty;
Pablo Greco 75b106
-  fd_set readfds;
Pablo Greco 75b106
   struct timespec spec;
Pablo Greco 75b106
   struct timespec *specp = NULL;
Pablo Greco 75b106
-  int r;
Pablo Greco 75b106
-  char intake;
Pablo Greco 75b106
+  sigset_t empty;
Pablo Greco 75b106
 
Pablo Greco 75b106
   sigemptyset (&empty);
Pablo Greco 75b106
 
Pablo Greco 75b106
-  FD_ZERO (&readfds);
Pablo Greco 75b106
-  FD_SET (job_fds[0], &readfds);
Pablo Greco 75b106
-
Pablo Greco 75b106
   if (timeout)
Pablo Greco 75b106
     {
Pablo Greco 75b106
       /* Alarm after one second (is this too granular?)  */
Pablo Greco 75b106
@@ -256,28 +277,52 @@ jobserver_acquire (int timeout)
Pablo Greco 75b106
       specp = &spe;;
Pablo Greco 75b106
     }
Pablo Greco 75b106
 
Pablo Greco 75b106
-  r = pselect (job_fds[0]+1, &readfds, NULL, NULL, specp, &empty);
Pablo Greco 75b106
-
Pablo Greco 75b106
-  if (r == -1)
Pablo Greco 75b106
+  while (1)
Pablo Greco 75b106
     {
Pablo Greco 75b106
-      /* Better be SIGCHLD.  */
Pablo Greco 75b106
-      if (errno != EINTR)
Pablo Greco 75b106
-        pfatal_with_name (_("pselect jobs pipe"));
Pablo Greco 75b106
-      return 0;
Pablo Greco 75b106
-    }
Pablo Greco 75b106
+      fd_set readfds;
Pablo Greco 75b106
+      int r;
Pablo Greco 75b106
+      char intake;
Pablo Greco 75b106
 
Pablo Greco 75b106
-  if (r == 0)
Pablo Greco 75b106
-    /* Timeout.  */
Pablo Greco 75b106
-    return 0;
Pablo Greco 75b106
+      FD_ZERO (&readfds);
Pablo Greco 75b106
+      FD_SET (job_fds[0], &readfds);
Pablo Greco 75b106
 
Pablo Greco 75b106
-  /* The read FD is ready: read it!  */
Pablo Greco 75b106
-  EINTRLOOP (r, read (job_fds[0], &intake, 1));
Pablo Greco 75b106
-  if (r < 0)
Pablo Greco 75b106
-    pfatal_with_name (_("read jobs pipe"));
Pablo Greco 75b106
+      r = pselect (job_fds[0]+1, &readfds, NULL, NULL, specp, &empty);
Pablo Greco 75b106
+      if (r < 0)
Pablo Greco 75b106
+        switch (errno)
Pablo Greco 75b106
+          {
Pablo Greco 75b106
+          case EINTR:
Pablo Greco 75b106
+            /* SIGCHLD will show up as an EINTR.  */
Pablo Greco 75b106
+            return 0;
Pablo Greco 75b106
+
Pablo Greco 75b106
+          case EBADF:
Pablo Greco 75b106
+            /* Someone closed the jobs pipe.
Pablo Greco 75b106
+               That shouldn't happen but if it does we're done.  */
Pablo Greco 75b106
+              O (fatal, NILF, _("job server shut down"));
Pablo Greco 75b106
 
Pablo Greco 75b106
-  /* What does it mean if read() returns 0?  It shouldn't happen because only
Pablo Greco 75b106
-     the master make can reap all the tokens and close the write side...??  */
Pablo Greco 75b106
-  return r > 0;
Pablo Greco 75b106
+          default:
Pablo Greco 75b106
+            pfatal_with_name (_("pselect jobs pipe"));
Pablo Greco 75b106
+          }
Pablo Greco 75b106
+
Pablo Greco 75b106
+      if (r == 0)
Pablo Greco 75b106
+        /* Timeout.  */
Pablo Greco 75b106
+        return 0;
Pablo Greco 75b106
+
Pablo Greco 75b106
+      /* The read FD is ready: read it!  This is non-blocking.  */
Pablo Greco 75b106
+      EINTRLOOP (r, read (job_fds[0], &intake, 1));
Pablo Greco 75b106
+
Pablo Greco 75b106
+      if (r < 0)
Pablo Greco 75b106
+        {
Pablo Greco 75b106
+          /* Someone sniped our token!  Try again.  */
Pablo Greco 75b106
+          if (errno == EAGAIN)
Pablo Greco 75b106
+            continue;
Pablo Greco 75b106
+
Pablo Greco 75b106
+          pfatal_with_name (_("read jobs pipe"));
Pablo Greco 75b106
+        }
Pablo Greco 75b106
+
Pablo Greco 75b106
+      /* read() should never return 0: only the master make can reap all the
Pablo Greco 75b106
+         tokens and close the write side...??  */
Pablo Greco 75b106
+      return r > 0;
Pablo Greco 75b106
+    }
Pablo Greco 75b106
 }
Pablo Greco 75b106
 
Pablo Greco 75b106
 #else