Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8362884

Browse files
committed
Fix waitpid() emulation on Windows.
Our waitpid() emulation didn't prevent a PID from being recycled by theOS before the call to waitpid(). The postmaster could finish uptracking more than one child process with the same PID, and confusethem.Fix, by moving the guts of pgwin32_deadchild_callback() into waitpid(),so that resources are released synchronously. The process and PIDcontinue to exist until we close the process handle, which only happensonce we're ready to adjust our book-keeping of running children.This seems to explain a couple of failures on CI. It had never beenreported before, despite the code being as old as the Windows port.Perhaps Windows started recycling PIDs more rapidly, or perhaps timingchanges due to commit7389aad made it more likely to break.Thanks to Alexander Lakhin for analysis and Andres Freund for trackingdown 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 parent6d3a9a6 commit8362884

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
staticpid_t
65306530
waitpid(pid_tpid,int*exitstatus,intoptions)
65316531
{
6532+
win32_deadchild_waitinfo*childinfo;
6533+
DWORDexitcode;
65326534
DWORDdwd;
65336535
ULONG_PTRkey;
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-
returndwd;
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-
staticvoidWINAPI
6554-
pgwin32_deadchild_callback(PVOIDlpParameter,BOOLEANTimerOrWaitFired)
6555-
{
6556-
win32_deadchild_waitinfo*childinfo= (win32_deadchild_waitinfo*)lpParameter;
6557-
DWORDexitcode;
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 inthe
6583-
*originating thread
6565+
*Close theprocess handle. Only after this point canthe 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+
returnpid;
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+
staticvoidWINAPI
6584+
pgwin32_deadchild_callback(PVOIDlpParameter,BOOLEANTimerOrWaitFired)
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)

[8]ページ先頭

©2009-2025 Movatter.jp