On Wed, Sep  5, 2012 at 10:35:26PM -0400, Andrew Dunstan wrote:
> >>Icky. I wish there were some nice portable flock() mechanism we could use.
> >>
> >>I just re-ran the test on the same machine, same code, same
> >>everything as the reporte3d failure, and it passed, so it definitely
> >>looks like it's a timing issue.
> >>
> >>I'd be inclined to put a loop around that fopen() to try it once
> >>every second for, say, 5 seconds.
> >Yes, good idea.
> >
> 
> Suggested patch attached.
> 
> cheers
> 
> andrew
> 

> diff --git a/contrib/pg_upgrade/exec.c b/contrib/pg_upgrade/exec.c
> index 99f5006..f84d857 100644
> --- a/contrib/pg_upgrade/exec.c
> +++ b/contrib/pg_upgrade/exec.c
> @@ -63,7 +63,25 @@ exec_prog(const char *log_file, const char *opt_log_file,
>       if (written >= MAXCMDLEN)
>               pg_log(PG_FATAL, "command too long\n");
>  
> -     if ((log = fopen_priv(log_file, "a")) == NULL)
> +#ifdef WIN32
> +     {
> +             /* 
> +              * Try to open the log file a few times in case the
> +              * server takes a bit longer than we'd like to release it.
> +              */
> +             int iter;
> +             for (iter = 0; iter < 5; iter++)
> +             {
> +                     log = fopen_priv(log_file, "a");
> +                     if (log != NULL || iter == 4)
> +                             break;
> +                     sleep(1);
> +             }
> +     }
> +#else
> +     log = fopen_priv(log_file, "a");
> +#endif
> +     if (log == NULL)
>               pg_log(PG_FATAL, "cannot write to log file %s\n", log_file);
>  #ifdef WIN32
>       fprintf(log, "\n\n");

I would like to see a more verbose comment, so we don't forget why we
did this.  I think my inability to quickly discover the cause of the
previous log write problem is that I didn't document which file
descriptors are kept open on Windows.  I suggest for a comment:

        /* 
         * "pg_ctl -w stop" might have reported that the server has
         * stopped because the postmaster.pid file has been removed,
         * but "pg_ctl -w start" might still be in the process of
         * closing and might still be holding its stdout and -l log
         * file descriptors open.  Therefore, try to open the log 
         * file a few times.
         */

Anyway, we can easily adjust the comment post-9.2.0.

-- 
  Bruce Momjian  <br...@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +


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

Reply via email to