Re: Possible race condition in pg_mkdir_p()? - Mailing list pgsql-hackers

From Michael Paquier
Subject Re: Possible race condition in pg_mkdir_p()?
Date
Msg-id 20190731040440.GJ1577@paquier.xyz
Whole thread Raw
In response to Re: Possible race condition in pg_mkdir_p()?  (Ning Yu <nyu@pivotal.io>)
Responses Re: Possible race condition in pg_mkdir_p()?  (Ning Yu <nyu@pivotal.io>)
List pgsql-hackers
On Tue, Jul 30, 2019 at 06:22:59PM +0800, Ning Yu wrote:
> In fact personally I'm thinking that whether could we replace all uses of
> MakePGDirectory() with pg_mkdir_p(), so we could simplify
> TablespaceCreateDbspace() and PathNameCreateTemporaryDir() and other callers
> significantly.

I would still keep the wrapper, but I think that as a final result we
should be able to get the code in PathNameCreateTemporaryDir() shaped
in such a way that there are no multiple attempts at calling
MakePGDirectory() on EEXIST.  This has been introduced by dc6c4c9d to
allow sharing temporary files between backends, which is rather recent
but a fixed set of two retries is not a deterministic method of
resolution.

> Well, we should have fixed problem 2, this is our major purpose of the patch
> 0001, it performs sanity check with stat() after mkdir() at each part of the
> path.

I just reuse the script presented at the top of the thread with n=2,
and I get that:
pgdata/logs/1.log:creating directory
pgdata/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/1
... initdb: error: could not create directory "pgdata/a": File exists

But the result expected is that all the paths should be created with
no complains about the parents existing, no?  This reproduces on my
Debian box 100% of the time, for different sub-paths.  So something
looks wrong in your solution.  The code comes originally from FreeBSD,
how do things happen there.  Do we get failures if doing something
like that?  I would expect this sequence to not fail:
for i in `seq 1 100`; do mkdir -p b/c/d/f/g/h/j/$i; done
--
Michael

Attachment

pgsql-hackers by date:

Previous
From: Kohei KaiGai
Date:
Subject: How to retain lesser paths at add_path()?
Next
From: Amit Kapila
Date:
Subject: Re: SegFault on 9.6.14