Skip to content

Commit 8362884

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 6d3a9a6 commit 8362884

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
@@ -4855,7 +4855,7 @@ internal_forkexec(int argc, char *argv[], Port *port)
48554855
(errmsg_internal("could not register process for wait: error code %lu",
48564856
GetLastError())));
48574857

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

48604860
CloseHandle(pi.hThread);
48614861

@@ -6529,36 +6529,21 @@ ShmemBackendArrayRemove(Backend *bn)
65296529
static pid_t
65306530
waitpid(pid_t pid, int *exitstatus, int options)
65316531
{
6532+
win32_deadchild_waitinfo *childinfo;
6533+
DWORD exitcode;
65326534
DWORD dwd;
65336535
ULONG_PTR key;
65346536
OVERLAPPED *ovl;
65356537

6536-
/*
6537-
* Check if there are any dead children. If there are, return the pid of
6538-
* the first one that died.
6539-
*/
6540-
if (GetQueuedCompletionStatus(win32ChildQueue, &dwd, &key, &ovl, 0))
6538+
/* Try to consume one win32_deadchild_waitinfo from the queue. */
6539+
if (!GetQueuedCompletionStatus(win32ChildQueue, &dwd, &key, &ovl, 0))
65416540
{
6542-
*exitstatus = (int) key;
6543-
return dwd;
6541+
errno = EAGAIN;
6542+
return -1;
65446543
}
65456544

6546-
return -1;
6547-
}
6548-
6549-
/*
6550-
* Note! Code below executes on a thread pool! All operations must
6551-
* be thread safe! Note that elog() and friends must *not* be used.
6552-
*/
6553-
static void WINAPI
6554-
pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
6555-
{
6556-
win32_deadchild_waitinfo *childinfo = (win32_deadchild_waitinfo *) lpParameter;
6557-
DWORD exitcode;
6558-
6559-
if (TimerOrWaitFired)
6560-
return; /* timeout. Should never happen, since we use
6561-
* INFINITE as timeout value. */
6545+
childinfo = (win32_deadchild_waitinfo *) key;
6546+
pid = childinfo->procId;
65626547

65636548
/*
65646549
* Remove handle from wait - required even though it's set to wait only
@@ -6574,13 +6559,11 @@ pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
65746559
write_stderr("could not read exit code for process\n");
65756560
exitcode = 255;
65766561
}
6577-
6578-
if (!PostQueuedCompletionStatus(win32ChildQueue, childinfo->procId, (ULONG_PTR) exitcode, NULL))
6579-
write_stderr("could not post child completion status\n");
6562+
*exitstatus = exitcode;
65806563

65816564
/*
6582-
* Handle is per-process, so we close it here instead of in the
6583-
* originating thread
6565+
* Close the process handle. Only after this point can the PID can be
6566+
* recycled by the kernel.
65846567
*/
65856568
CloseHandle(childinfo->procHandle);
65866569

@@ -6590,7 +6573,34 @@ pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
65906573
*/
65916574
free(childinfo);
65926575

6593-
/* Queue SIGCHLD signal */
6576+
return pid;
6577+
}
6578+
6579+
/*
6580+
* Note! Code below executes on a thread pool! All operations must
6581+
* be thread safe! Note that elog() and friends must *not* be used.
6582+
*/
6583+
static void WINAPI
6584+
pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
6585+
{
6586+
/* Should never happen, since we use INFINITE as timeout value. */
6587+
if (TimerOrWaitFired)
6588+
return;
6589+
6590+
/*
6591+
* Post the win32_deadchild_waitinfo object for waitpid() to deal with. If
6592+
* that fails, we leak the object, but we also leak a whole process and
6593+
* get into an unrecoverable state, so there's not much point in worrying
6594+
* about that. We'd like to panic, but we can't use that infrastructure
6595+
* from this thread.
6596+
*/
6597+
if (!PostQueuedCompletionStatus(win32ChildQueue,
6598+
0,
6599+
(ULONG_PTR) lpParameter,
6600+
NULL))
6601+
write_stderr("could not post child completion status\n");
6602+
6603+
/* Queue SIGCHLD signal. */
65946604
pg_queue_signal(SIGCHLD);
65956605
}
65966606
#endif /* WIN32 */

0 commit comments

Comments
 (0)