Skip to content

Commit 9f1c640

Browse files
committed
Fix waitpid() emulation on Windows.
Our waitpid() emulation didn't prevent a PID from being recycled by the OS before the call to waitpid(). The postmaster could finish up tracking more than one child process with the same PID, and confuse them. Fix, by moving the guts of pgwin32_deadchild_callback() into waitpid(), so that resources are released synchronously. The process and PID continue to exist until we close the process handle, which only happens once we're ready to adjust our book-keeping of running children. This seems to explain a couple of failures on CI. It had never been reported before, despite the code being as old as the Windows port. Perhaps Windows started recycling PIDs more rapidly, or perhaps timing changes due to commit 7389aad made it more likely to break. Thanks to Alexander Lakhin for analysis and Andres Freund for tracking down the root cause. Back-patch to all supported branches. Reported-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/20230208012852.bvkn2am4h4iqjogq%40awork3.anarazel.de
1 parent 386a260 commit 9f1c640

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4901,7 +4901,7 @@ internal_forkexec(int argc, char *argv[], Port *port)
49014901
(errmsg_internal("could not register process for wait: error code %lu",
49024902
GetLastError())));
49034903

4904-
/* Don't close pi.hProcess here - the wait thread needs access to it */
4904+
/* Don't close pi.hProcess here - waitpid() needs access to it */
49054905

49064906
CloseHandle(pi.hThread);
49074907

@@ -6561,36 +6561,21 @@ ShmemBackendArrayRemove(Backend *bn)
65616561
static pid_t
65626562
waitpid(pid_t pid, int *exitstatus, int options)
65636563
{
6564+
win32_deadchild_waitinfo *childinfo;
6565+
DWORD exitcode;
65646566
DWORD dwd;
65656567
ULONG_PTR key;
65666568
OVERLAPPED *ovl;
65676569

6568-
/*
6569-
* Check if there are any dead children. If there are, return the pid of
6570-
* the first one that died.
6571-
*/
6572-
if (GetQueuedCompletionStatus(win32ChildQueue, &dwd, &key, &ovl, 0))
6570+
/* Try to consume one win32_deadchild_waitinfo from the queue. */
6571+
if (!GetQueuedCompletionStatus(win32ChildQueue, &dwd, &key, &ovl, 0))
65736572
{
6574-
*exitstatus = (int) key;
6575-
return dwd;
6573+
errno = EAGAIN;
6574+
return -1;
65766575
}
65776576

6578-
return -1;
6579-
}
6580-
6581-
/*
6582-
* Note! Code below executes on a thread pool! All operations must
6583-
* be thread safe! Note that elog() and friends must *not* be used.
6584-
*/
6585-
static void WINAPI
6586-
pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
6587-
{
6588-
win32_deadchild_waitinfo *childinfo = (win32_deadchild_waitinfo *) lpParameter;
6589-
DWORD exitcode;
6590-
6591-
if (TimerOrWaitFired)
6592-
return; /* timeout. Should never happen, since we use
6593-
* INFINITE as timeout value. */
6577+
childinfo = (win32_deadchild_waitinfo *) key;
6578+
pid = childinfo->procId;
65946579

65956580
/*
65966581
* Remove handle from wait - required even though it's set to wait only
@@ -6606,13 +6591,11 @@ pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
66066591
write_stderr("could not read exit code for process\n");
66076592
exitcode = 255;
66086593
}
6609-
6610-
if (!PostQueuedCompletionStatus(win32ChildQueue, childinfo->procId, (ULONG_PTR) exitcode, NULL))
6611-
write_stderr("could not post child completion status\n");
6594+
*exitstatus = exitcode;
66126595

66136596
/*
6614-
* Handle is per-process, so we close it here instead of in the
6615-
* originating thread
6597+
* Close the process handle. Only after this point can the PID can be
6598+
* recycled by the kernel.
66166599
*/
66176600
CloseHandle(childinfo->procHandle);
66186601

@@ -6622,7 +6605,34 @@ pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
66226605
*/
66236606
free(childinfo);
66246607

6625-
/* Queue SIGCHLD signal */
6608+
return pid;
6609+
}
6610+
6611+
/*
6612+
* Note! Code below executes on a thread pool! All operations must
6613+
* be thread safe! Note that elog() and friends must *not* be used.
6614+
*/
6615+
static void WINAPI
6616+
pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
6617+
{
6618+
/* Should never happen, since we use INFINITE as timeout value. */
6619+
if (TimerOrWaitFired)
6620+
return;
6621+
6622+
/*
6623+
* Post the win32_deadchild_waitinfo object for waitpid() to deal with. If
6624+
* that fails, we leak the object, but we also leak a whole process and
6625+
* get into an unrecoverable state, so there's not much point in worrying
6626+
* about that. We'd like to panic, but we can't use that infrastructure
6627+
* from this thread.
6628+
*/
6629+
if (!PostQueuedCompletionStatus(win32ChildQueue,
6630+
0,
6631+
(ULONG_PTR) lpParameter,
6632+
NULL))
6633+
write_stderr("could not post child completion status\n");
6634+
6635+
/* Queue SIGCHLD signal. */
66266636
pg_queue_signal(SIGCHLD);
66276637
}
66286638
#endif /* WIN32 */

0 commit comments

Comments
 (0)