pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #14199: The pg_ctl status check on server start is not compatible with the silent_mode=on
5+ messages / 4 participants
[nested] [flat]

* BUG #14199: The pg_ctl status check on server start is not compatible with the silent_mode=on
@ 2016-06-18 04:28  sobomax@freebsd.org
  0 siblings, 1 reply; 5+ messages in thread

From: sobomax@freebsd.org @ 2016-06-18 04:28 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      14199
Logged by:          Maksym Sobolyev
Email address:      sobomax@freebsd.org
PostgreSQL version: 9.1.22
Operating system:   FreeBSD 10.3-RELEASE amd64
Description:        

There is a problem with pg_ctl when it tries to start server with the
"silent_mode=on" option enabled in postgresql.conf. Specifically, this
option causes postgres to fork once more after start. There are two problems
caused by that:

1. The pm_pid recorded by the pg_ctl when doing fork+execve no longer
matches the PID in the postmaster.pid file. This causes pg_ctl bail out
immediately.

2. Method that pg_ctl uses to poll if postgres exited prematurely no longer
works. In POSIX "child of my child is not my child", therefore it is
impossible to waitpid() on that process even if we use a correct PID from
the postmaster.pid file, while waitpid() on the original process would cause
race condition, since that process just does fork() and exit, so by the time
when real postgres has a chance to fully populate postmaster.pid it might
already be gone.

Attached patch fixes that issue by changing the way pg_ctl polls on the
child status. Instead of using waitpid(), which as described above could not
work even in principle for the "grand-children" processes, we create a
socketpair (i.e. pipe) one end of which is then passed into forked pg_ctl
and hence inherited by the postgres itself after execve.

In the unlikely event of postgres exiting prematurely that pipe would get
closed by the kernel and so that the pg_ctl would get EOF trying to do
non-blocking read on its own end, thereby being able to bail out quickly
instead of waiting for the timeout to happen. This should work nicely no
matter how many times child forks after execve().

This problem is exposed by the fact that FreeBSD port has that option set in
its default server configuration file for the version 9.1 .

https://svnweb.freebsd.org/ports/head/databases/postgresql91-server/files/patch-src%3Abackend%3Autil...

For some reason it was not a problem until recently. I think it might be
brought into the limelight by some internal changes in the PG's handling of
the said option.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* BUG #14199: The pg_ctl status check on server start is not compatible with the silent_mode=on
@ 2016-06-18 04:33  Maxim Sobolev <sobomax@freebsd.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Maxim Sobolev @ 2016-06-18 04:33 UTC (permalink / raw)
  To: pgsql-bugs

Proposed patch to resolve mentioned deficiency is attached.

-Max

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

patch-src_bin_pg__ctl_pg__ctl.c (text/x-csrc, 3.3K) download $FreeBSD$ --- src/bin/pg_ctl/pg_ctl.c.orig +++ src/bin/pg_ctl/pg_ctl.c @@ -27,6 +27,7 @@ #include <signal.h> #include <time.h> #include <sys/types.h> +#include <sys/socket.h> #include <sys/stat.h> #include <sys/wait.h> #include <unistd.h> @@ -155,10 +156,10 @@ static pgpid_t get_pgpid(void); static char **readfile(const char *path); -static pgpid_t start_postmaster(void); +static pgpid_t start_postmaster(int *spair); static void read_post_opts(void); -static PGPing test_postmaster_connection(pgpid_t pm_pid, bool do_checkpoint); +static PGPing test_postmaster_connection(pgpid_t pm_pid, int *spair, bool do_checkpoint); static bool postmaster_is_alive(pid_t pid); #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE) @@ -414,7 +415,7 @@ * "postmasterProcess", which the caller should close when done with it. */ static pgpid_t -start_postmaster(void) +start_postmaster(int *spair) { char cmd[MAXPGPATH]; @@ -425,6 +426,11 @@ fflush(stdout); fflush(stderr); + if (socketpair(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, spair) == -1) { + write_stderr(_("%s: could not start server: %s\n"), + progname, strerror(errno)); + exit(1); + } pm_pid = fork(); if (pm_pid < 0) { @@ -436,10 +442,12 @@ if (pm_pid > 0) { /* fork succeeded, in parent */ + close(spair[0]); return pm_pid; } /* fork succeeded, in child */ + close(spair[1]); /* * Since there might be quotes to handle here, it is easier simply to pass @@ -509,7 +517,7 @@ * manager checkpoint, it's got nothing to do with database checkpoints!! */ static PGPing -test_postmaster_connection(pgpid_t pm_pid, bool do_checkpoint) +test_postmaster_connection(pgpid_t pm_pid, int *spair, bool do_checkpoint) { PGPing ret = PQPING_NO_RESPONSE; char connstr[MAXPGPATH * 2 + 256]; @@ -580,12 +588,7 @@ pmpid = atol(optlines[LOCK_FILE_LINE_PID - 1]); pmstart = atol(optlines[LOCK_FILE_LINE_START_TIME - 1]); if (pmstart >= start_time - 2 && -#ifndef WIN32 - pmpid == pm_pid -#else - /* Windows can only reject standalone-backend PIDs */ pmpid > 0 -#endif ) { /* @@ -675,10 +678,11 @@ */ #ifndef WIN32 { - int exitstatus; + char fake_buf[16]; - if (waitpid((pid_t) pm_pid, &exitstatus, WNOHANG) == (pid_t) pm_pid) + if (read(spair[1], fake_buf, sizeof(fake_buf)) == 0) { return PQPING_NO_RESPONSE; + } } #else if (WaitForSingleObject(postmasterProcess, 0) == WAIT_OBJECT_0) @@ -847,6 +851,9 @@ { pgpid_t old_pid = 0; pgpid_t pm_pid; + int pm_spair[2]; + + pm_spair[0] = pm_spair[1] = -1; if (ctl_command != RESTART_COMMAND) { @@ -886,13 +893,13 @@ } #endif - pm_pid = start_postmaster(); + pm_pid = start_postmaster(pm_spair); if (do_wait) { print_msg(_("waiting for server to start...")); - switch (test_postmaster_connection(pm_pid, false)) + switch (test_postmaster_connection(pm_pid, pm_spair, false)) { case PQPING_OK: print_msg(_(" done\n")); @@ -1528,7 +1535,7 @@ if (do_wait) { write_eventlog(EVENTLOG_INFORMATION_TYPE, _("Waiting for server startup...\n")); - if (test_postmaster_connection(postmasterPID, true) != PQPING_OK) + if (test_postmaster_connection(postmasterPID, NULL, true) != PQPING_OK) { write_eventlog(EVENTLOG_ERROR_TYPE, _("Timed out waiting for server startup\n")); pgwin32_SetServiceStatus(SERVICE_STOPPED);
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: [BUGS] BUG #14199: The pg_ctl status check on server start is not compatible with the silent_mode=on
@ 2016-06-18 15:51  Tom Lane <tgl@sss.pgh.pa.us>
  parent: sobomax@freebsd.org
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2016-06-18 15:51 UTC (permalink / raw)
  To: sobomax@freebsd.org; +Cc: pgsql-bugs; pgsql-hackers@postgresql.org

sobomax@freebsd.org writes:
> There is a problem with pg_ctl when it tries to start server with the
> "silent_mode=on" option enabled in postgresql.conf. Specifically, this
> option causes postgres to fork once more after start. There are two problems
> caused by that:
> 1. The pm_pid recorded by the pg_ctl when doing fork+execve no longer
> matches the PID in the postmaster.pid file. This causes pg_ctl bail out
> immediately.
> 2. Method that pg_ctl uses to poll if postgres exited prematurely no longer
> works.

> For some reason it was not a problem until recently.

After reviewing recent commits, I realize that this was caused by
https://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=c869a7d5b
While generally that was a good thing, in hindsight it's obvious that
it doesn't work with silent_mode.  That's a non-issue in 9.2 and later
since silent_mode is gone anyway, but it is an issue for 9.1.

> Attached patch fixes that issue by changing the way pg_ctl polls on the
> child status.

There is no need of this complication in >= 9.2.  We could maybe apply it
in the 9.1 branch only, but I am quite loath to accept such a nontrivial
and portability-sensitive change that way.  The main reason being that
9.1 is almost EOL: its next minor release might well be its last.  If
there's anything wrong with this approach, we may not find out about it
until after 9.1 is out of support and won't get patched anymore.

What seems like a more conservative answer to me is to revert c869a7d5b
in 9.1 only, and address the buildfarm stability issue it sought to
resolve by increasing the fixed timeout from 5 seconds to, say, 10.

Thoughts?

			regards, tom lane


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: [BUGS] BUG #14199: The pg_ctl status check on server start is not compatible with the silent_mode=on
@ 2016-06-19 10:57  Michael Paquier <michael.paquier@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Michael Paquier @ 2016-06-19 10:57 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs; PostgreSQL mailing lists <pgsql-hackers@postgresql.org>

On Sun, Jun 19, 2016 at 12:51 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> What seems like a more conservative answer to me is to revert c869a7d5b
> in 9.1 only, and address the buildfarm stability issue it sought to
> resolve by increasing the fixed timeout from 5 seconds to, say, 10.

+1 for doing that. Knowing that silent_mode will be out of community
support scope in a couple of months, that's the right answer to this
bug call.
-- 
Michael


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: [HACKERS] Re: BUG #14199: The pg_ctl status check on server start is not compatible with the silent_mode=on
@ 2016-06-19 18:01  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2016-06-19 18:01 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: pgsql-bugs; PostgreSQL mailing lists <pgsql-hackers@postgresql.org>

Michael Paquier <michael.paquier@gmail.com> writes:
> On Sun, Jun 19, 2016 at 12:51 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> What seems like a more conservative answer to me is to revert c869a7d5b
>> in 9.1 only, and address the buildfarm stability issue it sought to
>> resolve by increasing the fixed timeout from 5 seconds to, say, 10.

> +1 for doing that. Knowing that silent_mode will be out of community
> support scope in a couple of months, that's the right answer to this
> bug call.

Done that way.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2016-06-19 18:01 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-06-18 04:28 BUG #14199: The pg_ctl status check on server start is not compatible with the silent_mode=on sobomax@freebsd.org
2016-06-18 15:51 ` Tom Lane <tgl@sss.pgh.pa.us>
2016-06-19 10:57   ` Michael Paquier <michael.paquier@gmail.com>
2016-06-19 18:01     ` Tom Lane <tgl@sss.pgh.pa.us>
2016-06-18 04:33 BUG #14199: The pg_ctl status check on server start is not compatible with the silent_mode=on Maxim Sobolev <sobomax@freebsd.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox