Fix race condition in ConditionVariablePrepareToSleep.
authorRobert Haas <rhaas@postgresql.org>
Thu, 9 Feb 2017 19:42:32 +0000 (14:42 -0500)
committerRobert Haas <rhaas@postgresql.org>
Thu, 9 Feb 2017 19:42:32 +0000 (14:42 -0500)
Thomas Munro

src/backend/storage/lmgr/condition_variable.c

index 67fe177a7ddd6fc64c8d08849a02304470211904..6f1ef0b7e5518bad55f4af1e4849699c4fc7edbe 100644 (file)
@@ -71,14 +71,17 @@ ConditionVariablePrepareToSleep(ConditionVariable *cv)
                          &MyProc->procLatch, NULL);
    }
 
+   /*
+    * Reset my latch before adding myself to the queue and before entering
+    * the caller's predicate loop.
+    */
+   ResetLatch(&MyProc->procLatch);
+
    /* Add myself to the wait queue. */
    SpinLockAcquire(&cv->mutex);
    if (!proclist_contains(&cv->wakeup, pgprocno, cvWaitLink))
        proclist_push_tail(&cv->wakeup, pgprocno, cvWaitLink);
    SpinLockRelease(&cv->mutex);
-
-   /* Reset my latch before entering the caller's predicate loop. */
-   ResetLatch(&MyProc->procLatch);
 }
 
 /*--------------------------------------------------------------------------