Gordon Henriksen
Fri, 03 Oct 2008 07:24:50 -0700
On 2008-10-03, at 02:40, Tim Bray wrote:
I'm developing a module which accepts a large number of POST requests, receives the request bodies, and persists them into ordinary files in the filesystem. On OS X, there are occasions where the file is (rarely) transiently not there. The idiom looks like thisSame code running in -X single-threaded mode, no problem. Same code on Solaris, no problem. (Well, yes, there are other weird Solaris problems with mutexes, but we'll get to those).
Sounds like a race. I'd suggest you replace this:
status = apr_file_open(&fp, tempname, APR_FOPEN_WRITE |
APR_FOPEN_CREATE,
PERMS, pool);
with
apr_status_t status;
do {
... generate random filename ...
status = apr_file_open(..., APR_FOPEN_WRITE|APR_FOPEN_CREATE|
APR_FOPEN_EXCL, ...);
} while (APR_IS_EEXIST(status));in order to hedge against another thread operating on the same filename concurrently. apr_file_mktemp will handle this detail transparently for you.
— Gordon