detect lost fds earlier
authorMarko Kreen <markokr@gmail.com>
Thu, 20 Dec 2007 16:25:05 +0000 (16:25 +0000)
committerMarko Kreen <markokr@gmail.com>
Thu, 20 Dec 2007 16:25:05 +0000 (16:25 +0000)
src/execute.c

index 31f48137645ab3e712bf06f38037b320e305d583..02361c1dee65f35e06becb3df3a00cf39c472d19 100644 (file)
@@ -206,6 +206,14 @@ check_old_conn(ProxyFunction *func, ProxyConnection *conn, struct timeval * now)
        if (PQstatus(conn->db) != CONNECTION_OK)
                return false;
 
+       fd = PQsocket(conn->db);
+       if (fd < 0)
+       {
+               elog(WARNING, "libpq socket lost: fd=%d, err=%s",
+                        fd, PQerrorMessage(conn->db));
+               return false;
+       }
+
        /* check if too old */
        if (cf->connection_lifetime > 0)
        {
@@ -224,7 +232,13 @@ check_old_conn(ProxyFunction *func, ProxyConnection *conn, struct timeval * now)
         * are events pending.  If there are drop the connection.
         */
 intr_loop:
-       fd = PQsocket(conn->db);
+       /* just in case detect if too many fds */
+#ifdef FD_SETSIZE
+       if (fd >= FD_SETSIZE)
+               plproxy_error(func, "Sorry, fd_set to select() too big: FD_SETSIZE=%d, fd=%d",
+                                         FD_SETSIZE, fd);
+#endif
+
        FD_ZERO(&fds);
        FD_SET(fd, &fds);
        res = select(fd + 1, &fds, NULL, NULL, &notimeout);
@@ -435,6 +449,9 @@ poll_conns(ProxyFunction *func, ProxyCluster *cluster)
                fd = PQsocket(conn->db);
                if (fd > fd_max)
                        fd_max = fd;
+               else if (fd < 0)
+                       plproxy_error(func, "libpq has lost its socket: fd=%d err=%s",
+                                                 fd, PQerrorMessage(conn->db));
                FD_SET(fd, cur_set);
        }
 
@@ -442,6 +459,13 @@ poll_conns(ProxyFunction *func, ProxyCluster *cluster)
        timeout.tv_sec = 1;
        timeout.tv_usec = 0;
 
+       /* just in case detect if too many fds */
+#ifdef FD_SETSIZE
+       if (fd_max > FD_SETSIZE)
+               plproxy_error(func, "Sorry, fd_set to select() too big: FD_SETSIZE=%d, fd_max=%d",
+                                         FD_SETSIZE, fd_max);
+#endif
+
        /* wait for events */
        res = select(fd_max + 1, &read_fds, &write_fds, NULL, &timeout);
        if (res == 0)
@@ -479,7 +503,10 @@ poll_conns(ProxyFunction *func, ProxyCluster *cluster)
 
                /* check */
                fd = PQsocket(conn->db);
-               if (FD_ISSET(fd, cur_set))
+               if (fd < 0)
+                       elog(WARNING, "libpq dropped socket: fd=%d err=%s",
+                                fd, PQerrorMessage(conn->db));
+               else if (FD_ISSET(fd, cur_set))
                        handle_conn(func, conn);
        }
        return 1;