Fix kqueue support under debugger on macOS.
authorThomas Munro <[email protected]>
Tue, 17 Mar 2020 23:43:05 +0000 (12:43 +1300)
committerThomas Munro <[email protected]>
Wed, 18 Mar 2020 00:05:07 +0000 (13:05 +1300)
While running under a debugger, macOS's getppid() can return the
debugger's PID.  That could cause a backend to exit because it falsely
believed that the postmaster had died, since commit 815c2f09.

Continue to use getppid() as a fast postmaster check after adding the
postmaster's PID to a kqueue, to close a PID-reuse race, but double
check that it actually exited by trying to read the pipe.  The new check
isn't reached in the common case.

Reported-by: Alexander Korotkov <[email protected]>
Discussion: https://postgr.es/m/CA%2BhUKGKhAxJ8V8RVwCo6zJaeVrdOG1kFBHGZOOjf6DzW_omeMA%40mail.gmail.com

src/backend/storage/ipc/latch.c

index 046ca5c6c7edb84fd622c8c65ff67c5e07f87b6d..d4c6c65baa3db01e1a711d0eeda6349f47f2ce9c 100644 (file)
@@ -1094,8 +1094,17 @@ WaitEventAdjustKqueue(WaitEventSet *set, WaitEvent *event, int old_events)
                     errmsg("%s failed: %m",
                            "kevent()")));
    }
-   else if (event->events == WL_POSTMASTER_DEATH && PostmasterPid != getppid())
+   else if (event->events == WL_POSTMASTER_DEATH &&
+            PostmasterPid != getppid() &&
+            !PostmasterIsAlive())
+   {
+       /*
+        * The extra PostmasterIsAliveInternal() check prevents false alarms on
+        * systems that give a different value for getppid() while being traced
+        * by a debugger.
+        */
        set->report_postmaster_not_running = true;
+   }
 }
 
 #endif