Initialize ListenSocket array earlier.
authorHeikki Linnakangas
Tue, 29 Aug 2023 06:09:40 +0000 (09:09 +0300)
committerHeikki Linnakangas
Tue, 29 Aug 2023 06:10:28 +0000 (09:10 +0300)
After commit b0bea38705, syslogger prints 63 warnings about failing to
close a listen socket at postmaster startup. That's because the
syslogger process forks before the ListenSockets array is initialized,
so ClosePostmasterPorts() calls "close(0)" 64 times. The first call
succeeds, because fd 0 is stdin.

This has been like this since commit 9a86f03b4e in version 13, which
moved the SysLogger_Start() call to before initializing ListenSockets.
We just didn't notice until commit b0bea38705 added the LOG message.

Reported by Michael Paquier and Jeff Janes.

Author: Michael Paquier
Discussion: https://www.postgresql.org/message-id/ZOvvuQe0rdj2slA9%40paquier.xyz
Discussion: https://www.postgresql.org/message-id/[email protected]#482670177eb4eaf4c9f03c1eed963e5f
Backpatch-through: 13

src/backend/postmaster/postmaster.c

index 4c49393fc5ad8657c3c1c834b9036d0b91f35333..b42aae41fce8386b44438bc73986e7ac1d433a03 100644 (file)
@@ -1143,6 +1143,17 @@ PostmasterMain(int argc, char *argv[])
                 errmsg("could not remove file \"%s\": %m",
                        LOG_METAINFO_DATAFILE)));
 
+   /*
+    * Initialize input sockets.
+    *
+    * Mark them all closed, and set up an on_proc_exit function that's
+    * charged with closing the sockets again at postmaster shutdown.
+    */
+   for (i = 0; i < MAXLISTEN; i++)
+       ListenSocket[i] = PGINVALID_SOCKET;
+
+   on_proc_exit(CloseServerPorts, 0);
+
    /*
     * If enabled, start up syslogger collection subprocess
     */
@@ -1177,15 +1188,7 @@ PostmasterMain(int argc, char *argv[])
 
    /*
     * Establish input sockets.
-    *
-    * First, mark them all closed, and set up an on_proc_exit function that's
-    * charged with closing the sockets again at postmaster shutdown.
     */
-   for (i = 0; i < MAXLISTEN; i++)
-       ListenSocket[i] = PGINVALID_SOCKET;
-
-   on_proc_exit(CloseServerPorts, 0);
-
    if (ListenAddresses)
    {
        char       *rawstring;