Fix parallel pg_dump/pg_restore for failure to create worker processes.
authorTom Lane <[email protected]>
Fri, 31 Jan 2020 19:41:49 +0000 (14:41 -0500)
committerTom Lane <[email protected]>
Fri, 31 Jan 2020 19:41:49 +0000 (14:41 -0500)
If we failed to fork a worker process, or create a communication pipe
for one, WaitForTerminatingWorkers would suffer an assertion failure
if assert-enabled, otherwise crash or go into an infinite loop.  This
was a consequence of not accounting for the startup condition where
we've not yet forked all the workers.

The original bug was that ParallelBackupStart would set workerStatus to
WRKR_IDLE before it had successfully forked a worker.  I made things
worse in commit b7b8cc0cf by not understanding the undocumented fact
that the WRKR_TERMINATED state was also meant to represent the case
where a worker hadn't been started yet: I changed enum T_WorkerStatus
so that *all* the worker slots were initially in WRKR_IDLE state.  But
this wasn't any more broken in practice, since even one slot in the
wrong state would keep WaitForTerminatingWorkers from terminating.

In v10 and later, introduce an explicit T_WorkerStatus value for
worker-not-started, in hopes of preventing future oversights of the
same ilk.  Before that, just document that WRKR_TERMINATED is supposed
to cover that case (partly because it wasn't actively broken, and
partly because the enum is exposed outside parallel.c in those branches,
so there's microscopically more risk involved in changing it).
In all branches, introduce a WORKER_IS_RUNNING status test macro
to hide which T_WorkerStatus values mean that, and be more careful
not to access ParallelSlot fields till we're sure they're valid.

Per report from Vignesh C, though this is my patch not his.
Back-patch to all supported branches.

Discussion: https://postgr.es/m/CALDaNm1Luv-E3sarR+-unz-BjchquHHyfP+YC+2FS2pt_J+wxg@mail.gmail.com

src/bin/pg_dump/parallel.c
src/bin/pg_dump/parallel.h

index de776a91891ba1421dfe5e17ffc3f410b0282612..12f714374ca30fe4143b850021bab1248d401146 100644 (file)
@@ -50,7 +50,7 @@
  *     WRKR_IDLE: it's waiting for a command
  *     WRKR_WORKING: it's been sent a command
  *     WRKR_FINISHED: it's returned a result
- *     WRKR_TERMINATED: process ended
+ *     WRKR_TERMINATED: process ended (or not started yet)
  * The FINISHED state indicates that the worker is idle, but we've not yet
  * dealt with the status code it returned from the prior command.
  * ReapWorkerStatus() extracts the unhandled command status value and sets
@@ -380,7 +380,9 @@ ShutdownWorkersHard(ParallelState *pstate)
 
    /*
     * Close our write end of the sockets so that any workers waiting for
-    * commands know they can exit.
+    * commands know they can exit.  (Note: some of the pipeWrite fields might
+    * still be zero, if we failed to initialize all the workers.  Hence, just
+    * ignore errors here.)
     */
    for (i = 0; i < pstate->numWorkers; i++)
        closesocket(pstate->parallelSlot[i].pipeWrite);
@@ -454,7 +456,7 @@ WaitForTerminatingWorkers(ParallelState *pstate)
 
        for (j = 0; j < pstate->numWorkers; j++)
        {
-           if (pstate->parallelSlot[j].workerStatus != WRKR_TERMINATED)
+           if (WORKER_IS_RUNNING(pstate->parallelSlot[j].workerStatus))
            {
                lpHandles[nrun] = (HANDLE) pstate->parallelSlot[j].hThread;
                nrun++;
@@ -889,6 +891,7 @@ ParallelBackupStart(ArchiveHandle *AH)
    if (AH->public.numWorkers == 1)
        return pstate;
 
+   /* Create status array, being sure to initialize all fields to 0 */
    pstate->parallelSlot = (ParallelSlot *) pg_malloc(slotSize);
    memset((void *) pstate->parallelSlot, 0, slotSize);
 
@@ -930,17 +933,16 @@ ParallelBackupStart(ArchiveHandle *AH)
        int         pipeMW[2],
                    pipeWM[2];
 
+       slot->args = (ParallelArgs *) pg_malloc(sizeof(ParallelArgs));
+       slot->args->AH = NULL;
+       slot->args->te = NULL;
+
        /* Create communication pipes for this worker */
        if (pgpipe(pipeMW) < 0 || pgpipe(pipeWM) < 0)
            exit_horribly(modulename,
                          "could not create communication channels: %s\n",
                          strerror(errno));
 
-       slot->workerStatus = WRKR_IDLE;
-       slot->args = (ParallelArgs *) pg_malloc(sizeof(ParallelArgs));
-       slot->args->AH = NULL;
-       slot->args->te = NULL;
-
        /* master's ends of the pipes */
        slot->pipeRead = pipeWM[PIPE_READ];
        slot->pipeWrite = pipeMW[PIPE_WRITE];
@@ -958,6 +960,7 @@ ParallelBackupStart(ArchiveHandle *AH)
        handle = _beginthreadex(NULL, 0, (void *) &init_spawned_worker_win32,
                                wi, 0, &(slot->threadId));
        slot->hThread = handle;
+       slot->workerStatus = WRKR_IDLE;
 #else                          /* !WIN32 */
        pid = fork();
        if (pid == 0)
@@ -1002,6 +1005,7 @@ ParallelBackupStart(ArchiveHandle *AH)
 
        /* In Master after successful fork */
        slot->pid = pid;
+       slot->workerStatus = WRKR_IDLE;
 
        /* close read end of Master -> Worker */
        closesocket(pipeMW[PIPE_READ]);
@@ -1118,7 +1122,7 @@ GetIdleWorker(ParallelState *pstate)
 }
 
 /*
- * Return true iff every worker is in the WRKR_TERMINATED state.
+ * Return true iff no worker is running.
  */
 static bool
 HasEveryWorkerTerminated(ParallelState *pstate)
@@ -1127,7 +1131,7 @@ HasEveryWorkerTerminated(ParallelState *pstate)
 
    for (i = 0; i < pstate->numWorkers; i++)
    {
-       if (pstate->parallelSlot[i].workerStatus != WRKR_TERMINATED)
+       if (WORKER_IS_RUNNING(pstate->parallelSlot[i].workerStatus))
            return false;
    }
    return true;
@@ -1527,7 +1531,7 @@ getMessageFromWorker(ParallelState *pstate, bool do_wait, int *worker)
    FD_ZERO(&workerset);
    for (i = 0; i < pstate->numWorkers; i++)
    {
-       if (pstate->parallelSlot[i].workerStatus == WRKR_TERMINATED)
+       if (!WORKER_IS_RUNNING(pstate->parallelSlot[i].workerStatus))
            continue;
        FD_SET(pstate->parallelSlot[i].pipeRead, &workerset);
        if (pstate->parallelSlot[i].pipeRead > maxFd)
@@ -1552,6 +1556,8 @@ getMessageFromWorker(ParallelState *pstate, bool do_wait, int *worker)
    {
        char       *msg;
 
+       if (!WORKER_IS_RUNNING(pstate->parallelSlot[i].workerStatus))
+           continue;
        if (!FD_ISSET(pstate->parallelSlot[i].pipeRead, &workerset))
            continue;
 
index 53013a60f9ddeda20cf09f159b9431a89e949a32..f62c8d71b1e4ae0f135a57a8d890af5c7b56b724 100644 (file)
@@ -29,6 +29,9 @@ typedef enum
    WRKR_FINISHED
 } T_WorkerStatus;
 
+#define WORKER_IS_RUNNING(workerStatus) \
+   ((workerStatus) != WRKR_TERMINATED)
+
 /* Arguments needed for a worker process */
 typedef struct ParallelArgs
 {