note 62188 added to function.flock

From: Date: Wed, 22 Feb 2006 02:19:21 +0000
Subject: note 62188 added to function.flock
Groups: php.notes 
Request: Send a blank email to php-notes+get-104188@lists.php.net to get a copy of this message
jbr at ya-right dot com has the wrong idea of what flock is supposed to do. flock is *supposed* to "hang" until the other lock is released. This is the intended behavior on ALL systems, not just Windows. If this behavior is not acceptable, flock already has a mechanism to deal with this: LOCK_NB. Just add LOCK_NB to the second argument, and flock will not "hang." For example: <?php flock($fp, LOCK_EX+LOCK_NB, $wouldblock) ?> and flock will return TRUE or FALSE immediately, setting $wouldblock to 1 if the call would have blocked. You DO NOT need to muck about with while loops, usleep, "clever" md5 + time hashing tricks, random numbers, or any of this nonsense. These techniques defeat the entire point of using flock in the first place. What's worse about jbr's code is that it introduces a bug that flock was meant to fix in the first place! It's called a "race condition" - here's an example: 1. Program A checks that file "fakelock" exists. It doesn't. 2. Processor switches to Program B. 3. Program B checks that file "fakelock" exists. It doesn't. 4. Program A writes its unique key, "A" to the file, and checks that its key is correct. It is. 5. Program B writes its unique key "B" to the file, and because its file pointer was pointing at the beginning of the file, it overwrites "A" with "B". It checks that its key is correct. It is. 6. Now Program A and Program B can both write to file "$fl_file.", and both overwrite the file that was to be protected. There are many other situations in which this code will fail as well. To the best of my knowledge, YOU CANNOT MAKE YOUR OWN FLOCK USING JUST PHP NO MATTER HOW HARD YOU TRY. YOU MUST USE FLOCK. If you have Windows ISAPI, you may have trouble with flock working correctly because it is multithreaded, but this kind of fix will not correct the situation. ---- Server IP: 210.138.109.182 Probable Submitter: 61.195.156.129 ---- Manual Page -- http://www.php.net/manual/en/function.flock.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+62188 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62188&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62188&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62188&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62188&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62188&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62188&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62188&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+62188&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#104188) next »