note 41321 deleted from function.flock by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 00:46:50 +0000 |
| Subject: | note 41321 deleted from function.flock by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155428@lists.php.net to get a copy of this message | ||
Note Submitter:
----
file locking with PHP over NFS shares simply does not work. Any attempt to use it will fail under
any race condition.
The only atomic file operation which is usable over NFS is mkdir() which will either create a folder
or return a error status...
So on NFS servers, forget flock(), use
@mkdir()
test the result, and if it fails, wait and retry.
Shamely, PHP does not always have short sleeping periods and sleep(1) is too long for most PHP
servers that limit execution time (expect to have complaints from users experimenting many 500
errors if the script is too long...)
The bad thing about mkdir() is that it requires you to perform yourself a rmdir() when you're
finished with the lock. If the rmdir() is not executed (because of execution time limitation), then
the script will be aborted, leaving the locking directory present, and locking your site
permanently.
So you also need to check for the date status of the created folder, in a separate NFS operation, to
see if the lock is not stale and to clean it (but when this maximum life of the lock has occured),
race conditions will happen if several threads or PHP servers will want to drop the staled
lock-directory.
There's no simple solution, unless PHP releases a locking function based on mkdir() with
AUTOMATIC rmdir() invokation when the PHP script engine terminates after a process or thread
interrupt (unless it is killed -9, but a Web Server would normally not kill a script engine with a
so brutal kill option).
PHP is most often deployed by free hosting providers on parallel servers over NFS. Using MySQL to
coordonate concurrent threads or process is overkill and lacks performance for some pages that have
frequent accesses, notably above 500 requests per hour.
PHP developers should really think about providing a working solution that will work on NFS, using
mkdir() and rmdir() internally which is the only "magic" atomic operation allwoed there,
and that is also performing the best (without requiring any complex client accesses to a SQL
server...)