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

Commitdfe572d

Browse files
committed
Fix "pg_ctl start -w" to test child process status directly.
pg_ctl start with -w previously relied on a heuristic that the postmasterwould surely always manage to create postmaster.pid within five seconds.Unfortunately, that fails much more often than we would like on some of theslower, more heavily loaded buildfarm members.We have known for quite some time that we could remove the need for thatheuristic on Unix by using fork/exec instead of system() to launch thepostmaster. This allows us to know the exact PID of the postmaster, whichallows near-certain verification that the postmaster.pid file is the onewe want and not a leftover, and it also lets us use waitpid() to detectreliably whether the child postmaster has exited or not.What was blocking this change was not wanting to rewrite the Windowsversion of start_postmaster() to avoid use of CMD.EXE. That's doablein theory but would require fooling about with stdout/stderr redirection,and getting the handling of quote-containing postmaster switches tostay the same might be rather ticklish. However, we realized thatwe don't have to do that to fix the problem, because we can testwhether the shell process has exited as a proxy for whether thepostmaster is still alive. That doesn't allow an exact check of thePID in postmaster.pid, but we're no worse off than before in thatrespect; and we do get to get rid of the heuristic about how long thepostmaster might take to create postmaster.pid.On Unix, this change means that a second "pg_ctl start -w" immediatelyafter another such command will now reliably fail, whereas previouslyit would succeed if done within two seconds of the earlier command.Since that's a saner behavior anyway, it's fine. On Windows, the case canstill succeed within the same time window, since pg_ctl can't tell that theearlier postmaster's postmaster.pid isn't the pidfile it is looking for.To ensure stable test results on Windows, we can insert a short sleep intothe test script for pg_ctl, ensuring that the existing pidfile looks stale.This hack can be removed if we ever do rewrite start_postmaster(), but thatno longer seems like a high-priority thing to do.Back-patch to all supported versions, both because the current behavioris buggy and because we must do that if we want the buildfarm failuresto go away.Tom Lane and Michael Paquier
1 parentf844f0e commitdfe572d

File tree

1 file changed

+107
-78
lines changed

1 file changed

+107
-78
lines changed

‎src/bin/pg_ctl/pg_ctl.c

Lines changed: 107 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include<time.h>
2929
#include<sys/types.h>
3030
#include<sys/stat.h>
31+
#include<sys/wait.h>
3132
#include<unistd.h>
3233

3334
#ifdefHAVE_SYS_RESOURCE_H
@@ -154,10 +155,10 @@ static intCreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo,
154155

155156
staticpgpid_tget_pgpid(void);
156157
staticchar**readfile(constchar*path);
157-
staticintstart_postmaster(void);
158+
staticpgpid_tstart_postmaster(void);
158159
staticvoidread_post_opts(void);
159160

160-
staticPGPingtest_postmaster_connection(bool);
161+
staticPGPingtest_postmaster_connection(pgpid_tpm_pid,booldo_checkpoint);
161162
staticboolpostmaster_is_alive(pid_tpid);
162163

163164
#if defined(HAVE_GETRLIMIT)&& defined(RLIMIT_CORE)
@@ -374,36 +375,73 @@ readfile(const char *path)
374375
* start/test/stop routines
375376
*/
376377

377-
staticint
378+
/*
379+
* Start the postmaster and return its PID.
380+
*
381+
* Currently, on Windows what we return is the PID of the shell process
382+
* that launched the postmaster (and, we trust, is waiting for it to exit).
383+
* So the PID is usable for "is the postmaster still running" checks,
384+
* but cannot be compared directly to postmaster.pid.
385+
*
386+
* On Windows, we also save aside a handle to the shell process in
387+
* "postmasterProcess", which the caller should close when done with it.
388+
*/
389+
staticpgpid_t
378390
start_postmaster(void)
379391
{
380392
charcmd[MAXPGPATH];
381393

382394
#ifndefWIN32
395+
pgpid_tpm_pid;
396+
397+
/* Flush stdio channels just before fork, to avoid double-output problems */
398+
fflush(stdout);
399+
fflush(stderr);
400+
401+
pm_pid=fork();
402+
if (pm_pid<0)
403+
{
404+
/* fork failed */
405+
write_stderr(_("%s: could not start server: %s\n"),
406+
progname,strerror(errno));
407+
exit(1);
408+
}
409+
if (pm_pid>0)
410+
{
411+
/* fork succeeded, in parent */
412+
returnpm_pid;
413+
}
414+
415+
/* fork succeeded, in child */
383416

384417
/*
385418
* Since there might be quotes to handle here, it is easier simply to pass
386-
* everything to a shell to process them.
387-
*
388-
* XXX it would be better to fork and exec so that we would know the child
389-
* postmaster's PID directly; then test_postmaster_connection could use
390-
* the PID without having to rely on reading it back from the pidfile.
419+
* everything to a shell to process them. Use exec so that the postmaster
420+
* has the same PID as the current child process.
391421
*/
392422
if (log_file!=NULL)
393-
snprintf(cmd,MAXPGPATH,SYSTEMQUOTE"\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1 &"SYSTEMQUOTE,
423+
snprintf(cmd,MAXPGPATH,"exec\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1",
394424
exec_path,pgdata_opt,post_opts,
395425
DEVNULL,log_file);
396426
else
397-
snprintf(cmd,MAXPGPATH,SYSTEMQUOTE"\"%s\" %s%s < \"%s\" 2>&1 &"SYSTEMQUOTE,
427+
snprintf(cmd,MAXPGPATH,"exec\"%s\" %s%s < \"%s\" 2>&1",
398428
exec_path,pgdata_opt,post_opts,DEVNULL);
399429

400-
returnsystem(cmd);
430+
(void)execl("/bin/sh","/bin/sh","-c",cmd, (char*)NULL);
431+
432+
/* exec failed */
433+
write_stderr(_("%s: could not start server: %s\n"),
434+
progname,strerror(errno));
435+
exit(1);
436+
437+
return0;/* keep dumb compilers quiet */
438+
401439
#else/* WIN32 */
402440

403441
/*
404-
*On win32 we don't use system(). So we don't need to use& (which would
405-
*be START /B on win32). However, we still call the shell (CMD.EXE) with
406-
*ittohandle redirection etc.
442+
*As with the Unix case, it's easiest to usethe shell (CMD.EXE) to
443+
*handle redirection etc. UnfortunatelyCMD.EXE lacks any equivalent of
444+
*"exec", so we don't gettofind out the postmaster's PID immediately.
407445
*/
408446
PROCESS_INFORMATIONpi;
409447

@@ -415,10 +453,15 @@ start_postmaster(void)
415453
exec_path,pgdata_opt,post_opts,DEVNULL);
416454

417455
if (!CreateRestrictedProcess(cmd,&pi, false))
418-
returnGetLastError();
419-
CloseHandle(pi.hProcess);
456+
{
457+
write_stderr(_("%s: could not start server: error code %lu\n"),
458+
progname, (unsigned long)GetLastError());
459+
exit(1);
460+
}
461+
/* Don't close command process handle here; caller must do so */
462+
postmasterProcess=pi.hProcess;
420463
CloseHandle(pi.hThread);
421-
return0;
464+
returnpi.dwProcessId;/* Shell's PID, not postmaster's! */
422465
#endif/* WIN32 */
423466
}
424467

@@ -427,15 +470,21 @@ start_postmaster(void)
427470
/*
428471
* Find the pgport and try a connection
429472
*
473+
* On Unix, pm_pid is the PID of the just-launched postmaster. On Windows,
474+
* it may be the PID of an ancestor shell process, so we can't check the
475+
* contents of postmaster.pid quite as carefully.
476+
*
477+
* On Windows, the static variable postmasterProcess is an implicit argument
478+
* to this routine; it contains a handle to the postmaster process or an
479+
* ancestor shell process thereof.
480+
*
430481
* Note that the checkpoint parameter enables a Windows service control
431482
* manager checkpoint, it's got nothing to do with database checkpoints!!
432483
*/
433484
staticPGPing
434-
test_postmaster_connection(booldo_checkpoint)
485+
test_postmaster_connection(pgpid_tpm_pid,booldo_checkpoint)
435486
{
436487
PGPingret=PQPING_NO_RESPONSE;
437-
boolfound_stale_pidfile= false;
438-
pgpid_tpm_pid=0;
439488
charconnstr[MAXPGPATH*2+256];
440489
inti;
441490

@@ -490,29 +539,27 @@ test_postmaster_connection(bool do_checkpoint)
490539
optlines[5]!=NULL)
491540
{
492541
/* File is complete enough for us, parse it */
493-
longpmpid;
542+
pgpid_tpmpid;
494543
time_tpmstart;
495544

496545
/*
497-
* Make sanity checks. If it's for a standalone backend
498-
* (negative PID), or the recorded start time is before
499-
* pg_ctl started, then either we are looking at the wrong
500-
* data directory, or this is a pre-existing pidfile that
501-
* hasn't (yet?) been overwritten by our child postmaster.
502-
* Allow 2 seconds slop for possible cross-process clock
503-
* skew.
546+
* Make sanity checks. If it's for the wrong PID, or the
547+
* recorded start time is before pg_ctl started, then
548+
* either we are looking at the wrong data directory, or
549+
* this is a pre-existing pidfile that hasn't (yet?) been
550+
* overwritten by our child postmaster. Allow 2 seconds
551+
* slop for possible cross-process clock skew.
504552
*/
505553
pmpid=atol(optlines[LOCK_FILE_LINE_PID-1]);
506554
pmstart=atol(optlines[LOCK_FILE_LINE_START_TIME-1]);
507-
if (pmpid <=0||pmstart<start_time-2)
508-
{
509-
/*
510-
* Set flag to report stale pidfile if it doesn't get
511-
* overwritten before we give up waiting.
512-
*/
513-
found_stale_pidfile= true;
514-
}
515-
else
555+
if (pmstart >=start_time-2&&
556+
#ifndefWIN32
557+
pmpid==pm_pid
558+
#else
559+
/* Windows can only reject standalone-backend PIDs */
560+
pmpid>0
561+
#endif
562+
)
516563
{
517564
/*
518565
* OK, seems to be a valid pidfile from our child.
@@ -522,9 +569,6 @@ test_postmaster_connection(bool do_checkpoint)
522569
char*hostaddr;
523570
charhost_str[MAXPGPATH];
524571

525-
found_stale_pidfile= false;
526-
pm_pid= (pgpid_t)pmpid;
527-
528572
/*
529573
* Extract port number and host string to use. Prefer
530574
* using Unix socket if available.
@@ -583,37 +627,23 @@ test_postmaster_connection(bool do_checkpoint)
583627
}
584628

585629
/*
586-
* The postmaster should create postmaster.pid very soon after being
587-
* started. If it's not there after we've waited 5 or more seconds,
588-
* assume startup failed and give up waiting. (Note this covers both
589-
* cases where the pidfile was never created, and where it was created
590-
* and then removed during postmaster exit.) Also, if there *is* a
591-
* file there but it appears stale, issue a suitable warning and give
592-
* up waiting.
630+
* Check whether the child postmaster process is still alive. This
631+
* lets us exit early if the postmaster fails during startup.
632+
*
633+
* On Windows, we may be checking the postmaster's parent shell, but
634+
* that's fine for this purpose.
593635
*/
594-
if (i >=5)
636+
#ifndefWIN32
595637
{
596-
structstatstatbuf;
597-
598-
if (stat(pid_file,&statbuf)!=0)
599-
returnPQPING_NO_RESPONSE;
638+
intexitstatus;
600639

601-
if (found_stale_pidfile)
602-
{
603-
write_stderr(_("\n%s: this data directory appears to be running a pre-existing postmaster\n"),
604-
progname);
640+
if (waitpid((pid_t)pm_pid,&exitstatus,WNOHANG)== (pid_t)pm_pid)
605641
returnPQPING_NO_RESPONSE;
606-
}
607642
}
608-
609-
/*
610-
* If we've been able to identify the child postmaster's PID, check
611-
* the process is still alive. This covers cases where the postmaster
612-
* successfully created the pidfile but then crashed without removing
613-
* it.
614-
*/
615-
if (pm_pid>0&& !postmaster_is_alive((pid_t)pm_pid))
643+
#else
644+
if (WaitForSingleObject(postmasterProcess,0)==WAIT_OBJECT_0)
616645
returnPQPING_NO_RESPONSE;
646+
#endif
617647

618648
/* No response, or startup still in process; wait */
619649
#if defined(WIN32)
@@ -776,7 +806,7 @@ static void
776806
do_start(void)
777807
{
778808
pgpid_told_pid=0;
779-
intexitcode;
809+
pgpid_tpm_pid;
780810

781811
if (ctl_command!=RESTART_COMMAND)
782812
{
@@ -816,19 +846,13 @@ do_start(void)
816846
}
817847
#endif
818848

819-
exitcode=start_postmaster();
820-
if (exitcode!=0)
821-
{
822-
write_stderr(_("%s: could not start server: exit code was %d\n"),
823-
progname,exitcode);
824-
exit(1);
825-
}
849+
pm_pid=start_postmaster();
826850

827851
if (do_wait)
828852
{
829853
print_msg(_("waiting for server to start..."));
830854

831-
switch (test_postmaster_connection(false))
855+
switch (test_postmaster_connection(pm_pid,false))
832856
{
833857
casePQPING_OK:
834858
print_msg(_(" done\n"));
@@ -854,6 +878,12 @@ do_start(void)
854878
}
855879
else
856880
print_msg(_("server starting\n"));
881+
882+
#ifdefWIN32
883+
/* Now we don't need the handle to the shell process anymore */
884+
CloseHandle(postmasterProcess);
885+
postmasterProcess=INVALID_HANDLE_VALUE;
886+
#endif
857887
}
858888

859889

@@ -1495,7 +1525,7 @@ pgwin32_ServiceMain(DWORD argc, LPTSTR *argv)
14951525
if (do_wait)
14961526
{
14971527
write_eventlog(EVENTLOG_INFORMATION_TYPE,_("Waiting for server startup...\n"));
1498-
if (test_postmaster_connection(true)!=PQPING_OK)
1528+
if (test_postmaster_connection(postmasterPID,true)!=PQPING_OK)
14991529
{
15001530
write_eventlog(EVENTLOG_ERROR_TYPE,_("Timed out waiting for server startup\n"));
15011531
pgwin32_SetServiceStatus(SERVICE_STOPPED);
@@ -1516,10 +1546,9 @@ pgwin32_ServiceMain(DWORD argc, LPTSTR *argv)
15161546
{
15171547
/*
15181548
* status.dwCheckPoint can be incremented by
1519-
* test_postmaster_connection(true), so it might not
1520-
* start from 0.
1549+
* test_postmaster_connection(), so it might not start from 0.
15211550
*/
1522-
intmaxShutdownCheckPoint=status.dwCheckPoint+12;;
1551+
intmaxShutdownCheckPoint=status.dwCheckPoint+12;
15231552

15241553
kill(postmasterPID,SIGINT);
15251554

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp