Ensure that we retry rather than erroring out when send() or recv() return
authorTom Lane <[email protected]>
Sun, 16 Jul 2006 18:17:23 +0000 (18:17 +0000)
committerTom Lane <[email protected]>
Sun, 16 Jul 2006 18:17:23 +0000 (18:17 +0000)
EINTR; the stats code was failing to do this and so were a couple of places
in the postmaster.  The stats code assumed that recv() could not return EINTR
if a preceding select() showed the socket to be read-ready, but this is
demonstrably false with our Windows implementation of recv(), and it may
not be the case on all Unix variants either.  I think this explains the
intermittent stats regression test failures we've been seeing, as well
as reports of stats collector instability under high load on Windows.

Backpatch as far as 8.0.

src/backend/postmaster/pgstat.c
src/backend/postmaster/postmaster.c

index 1f04f0f683e6d306f4606899926a62a5e4ca9f79..02b109a897343f5e74b38f5307338bd05868341d 100644 (file)
@@ -339,8 +339,12 @@ pgstat_init(void)
                 * rules prevent it).
                 */
                test_byte = TESTBYTEVAL;
+
+retry1:
                if (send(pgStatSock, &test_byte, 1, 0) != 1)
                {
+                       if (errno == EINTR)
+                               goto retry1;    /* if interrupted, just retry */
                        ereport(LOG,
                                        (errcode_for_socket_access(),
                                         errmsg("could not send test message on socket for statistics collector: %m")));
@@ -391,8 +395,11 @@ pgstat_init(void)
 
                test_byte++;                    /* just make sure variable is changed */
 
+retry2:
                if (recv(pgStatSock, &test_byte, 1, 0) != 1)
                {
+                       if (errno == EINTR)
+                               goto retry2;    /* if interrupted, just retry */
                        ereport(LOG,
                                        (errcode_for_socket_access(),
                                         errmsg("could not receive test message on socket for statistics collector: %m")));
@@ -1431,17 +1438,23 @@ pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
 static void
 pgstat_send(void *msg, int len)
 {
+       int                     rc;
+
        if (pgStatSock < 0)
                return;
 
        ((PgStat_MsgHdr *) msg)->m_size = len;
 
+       /* We'll retry after EINTR, but ignore all other failures */
+       do
+       {
+               rc = send(pgStatSock, msg, len, 0);
+       } while (rc < 0 && errno == EINTR);
+
 #ifdef USE_ASSERT_CHECKING
-       if (send(pgStatSock, msg, len, 0) < 0)
+       /* In debug builds, log send failures ... */
+       if (rc < 0)
                elog(LOG, "could not send to statistics collector: %m");
-#else
-       send(pgStatSock, msg, len, 0);
-       /* We deliberately ignore any error from send() */
 #endif
 }
 
@@ -1972,9 +1985,13 @@ pgstat_recvbuffer(void)
                        len = recv(pgStatSock, (char *) &input_buffer,
                                           sizeof(PgStat_Msg), 0);
                        if (len < 0)
+                       {
+                               if (errno == EINTR)
+                                       continue;
                                ereport(ERROR,
                                                (errcode_for_socket_access(),
                                                 errmsg("could not read statistics message: %m")));
+                       }
 
                        /*
                         * We ignore messages that are smaller than our common header
index 43ddc0aa2f67420bddfaa92e02093150e85cab47..7236ae30c0dd135d1d326257491a21f57e3c2ed7 100644 (file)
@@ -1408,8 +1408,12 @@ ProcessStartupPacket(Port *port, bool SSLdone)
 #else
                SSLok = 'N';                    /* No support for SSL */
 #endif
+
+retry1:
                if (send(port->sock, &SSLok, 1, 0) != 1)
                {
+                       if (errno == EINTR)
+                               goto retry1;    /* if interrupted, just retry */
                        ereport(COMMERROR,
                                        (errcode_for_socket_access(),
                                         errmsg("failed to send SSL negotiation response: %m")));
@@ -2545,6 +2549,7 @@ static void
 report_fork_failure_to_client(Port *port, int errnum)
 {
        char            buffer[1000];
+       int                     rc;
 
        /* Format the error message packet (always V2 protocol) */
        snprintf(buffer, sizeof(buffer), "E%s%s\n",
@@ -2555,7 +2560,11 @@ report_fork_failure_to_client(Port *port, int errnum)
        if (!pg_set_noblock(port->sock))
                return;
 
-       send(port->sock, buffer, strlen(buffer) + 1, 0);
+       /* We'll retry after EINTR, but ignore all other failures */
+       do
+       {
+               rc = send(port->sock, buffer, strlen(buffer) + 1, 0);
+       } while (rc < 0 && errno == EINTR);
 }