Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d56a5f9

Browse files
committed
While waiting for a condition variable, detect postmaster death.
The general assumption for postmaster child processes is that they should just exit(1), reasonably promptly, if the postmaster disappears. condition_variable.c neglected this consideration and could be left waiting forever, if the counterpart process it is waiting for has done the right thing and exited. We had some discussion of adjusting the WaitEventSet API to make it harder to make this type of mistake in future; but for the moment, and for v10, let's make this narrow fix. Discussion: https://postgr.es/m/20412.1515456143@sss.pgh.pa.us
1 parent 1f5adbd commit d56a5f9

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/backend/storage/lmgr/condition_variable.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ ConditionVariablePrepareToSleep(ConditionVariable *cv)
6262
{
6363
WaitEventSet *new_event_set;
6464

65-
new_event_set = CreateWaitEventSet(TopMemoryContext, 1);
65+
new_event_set = CreateWaitEventSet(TopMemoryContext, 2);
6666
AddWaitEventToSet(new_event_set, WL_LATCH_SET, PGINVALID_SOCKET,
6767
MyLatch, NULL);
68+
AddWaitEventToSet(new_event_set, WL_POSTMASTER_DEATH, PGINVALID_SOCKET,
69+
NULL, NULL);
6870
/* Don't set cv_wait_event_set until we have a correct WES. */
6971
cv_wait_event_set = new_event_set;
7072
}
@@ -141,11 +143,20 @@ ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
141143
CHECK_FOR_INTERRUPTS();
142144

143145
/*
144-
* Wait for latch to be set. We don't care about the result because
145-
* our contract permits spurious returns.
146+
* Wait for latch to be set. (If we're awakened for some other
147+
* reason, the code below will cope anyway.)
146148
*/
147149
WaitEventSetWait(cv_wait_event_set, -1, &event, 1, wait_event_info);
148150

151+
if (event.events & WL_POSTMASTER_DEATH)
152+
{
153+
/*
154+
* Emergency bailout if postmaster has died. This is to avoid the
155+
* necessity for manual cleanup of all postmaster children.
156+
*/
157+
exit(1);
158+
}
159+
149160
/* Reset latch before testing whether we can return. */
150161
ResetLatch(MyLatch);
151162

0 commit comments

Comments
 (0)