From 9e1308c8f208612dd0928449f558e5219a5b202b Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Fri, 20 Dec 2019 09:31:09 +1300
Subject: [PATCH] Don't call ResetLatch() in ConditionVariablePrepareToSleep().

It reset the latch without calling CHECK_FOR_INTERRUPTS().  Let's
just leave the latch set if it's set, so that the next wait loop
(mostly likely ConditionVariableSleep()) sees it and handles it.

One consequence of this bug was that a SIGTERM delivered in a very
narrow timing window could leave a parallel worker process waiting
forever for a condition variable that will never be signaled, after
an error was raised in other process.

Reported-by: Tomas Vondra
Discussion: https://postgr.es/m/20191217232124.3dtrycatgfm6oxxb%40development
---
 src/backend/storage/lmgr/condition_variable.c | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/src/backend/storage/lmgr/condition_variable.c b/src/backend/storage/lmgr/condition_variable.c
index e08507f0cc..663db95aa8 100644
--- a/src/backend/storage/lmgr/condition_variable.c
+++ b/src/backend/storage/lmgr/condition_variable.c
@@ -93,12 +93,6 @@ ConditionVariablePrepareToSleep(ConditionVariable *cv)
 	/* Record the condition variable on which we will sleep. */
 	cv_sleep_target = cv;
 
-	/*
-	 * Reset my latch before adding myself to the queue, to ensure that we
-	 * don't miss a wakeup that occurs immediately.
-	 */
-	ResetLatch(MyLatch);
-
 	/* Add myself to the wait queue. */
 	SpinLockAcquire(&cv->mutex);
 	proclist_push_tail(&cv->wakeup, pgprocno, cvWaitLink);
-- 
2.23.0

