|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-12-31 06:41 UTC] ian at snork dot net
Description:
------------
This code works in PHP 4.3.0, but does not in PHP 5.0.3.
When invoked, it will try to lock /tmp/test1, /tmp/test2
or /tmp/test3 for exclusive use, whichever is free. If
none are, it will wait a second before cycling through
with another attempt.
When running concurrently, the first invocation correctly
picks up /tmp/test1; the second and subsequent ones just
cycle forever.
For some reason, the non-blocking flock() only sets $block
the first time.
Reproduce code:
---------------
while (!sleep(1))
foreach (array("/tmp/test1", "/tmp/test2", "/tmp/test3") as $path)
if (flock($handle = fopen($path, "w"), LOCK_EX | LOCK_NB, $block) and !$block)
{
echo "Got $path\n";
sleep(10);
echo "Releasing $path\n";
exit;
}
Expected result:
----------------
$ php5 flock_test.php &
Got /tmp/test1
$ php5 flock_test.php &
Got /tmp/test2
$ php5 flock_test.php &
Got /tmp/test3
$ php5 flock_test.php &
Releasing /tmp/test1
Got /tmp/test1
$
Actual result:
--------------
$ php5 flock_test.php &
Got /tmp/test1
$ php5 flock_test.php &
$ php5 flock_test.php &
$ php5 flock_test.php &
$
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 15:00:01 2025 UTC |
I had the same problem. It seems that errno is not reset to zero in the flock implementation. Therefore the last errno will be used. In case no other errno-modifying call is made before retrying the flock() call, the call will conclude in an EWOULDBLOCK situation (my conclusion after a very quick scan of the code). Looking with truss on Solaris to the process shows that the first fcntl call (during which I have already locked the file) returns an EAGAIN error, meaning that the file was already locked. The second fnctl call (on the now unlocked file) does not return an error. So the call is successfull. But the call from the php code does set the $wouldblock parameter to a true value. For me a workaround for now is something like this (no error checking on flock and a bit of pseudo-coded, but the workaround is the important part): for (;;) { flock($fd, LOCK_EX|LOCK_NB, $wouldblock); if (! $wouldblock) break; fopen("/this/is/no/real/file", "r"); } The fopen() call sets errno to the file not found error, which makes the flock() call act like the first time it is called. This is not great coding, but it does work for me till a real fix is implemented in PHP.The cause looks to be two separate bugs; here's a patch to fix both. *** php5-200504050030.orig/ext/standard/file.c Sun Mar 27 16:30:05 2005 --- php5-200504050030/ext/standard/file.c Tue Apr 5 15:26:08 2005 *************** *** 339,351 **** /* flock_values contains all possible actions if (operation & 4) we won't block on the lock */ act = flock_values[act - 1] | (operation & 4 ? LOCK_NB : 0); ! if (!php_stream_lock(stream, act)) { if (operation && errno == EWOULDBLOCK && arg3 && PZVAL_IS_REF(arg3)) { Z_LVAL_P(arg3) = 1; } ! RETURN_TRUE; } ! RETURN_FALSE; } /* }}} */ --- 339,351 ---- /* flock_values contains all possible actions if (operation & 4) we won't block on the lock */ act = flock_values[act - 1] | (operation & 4 ? LOCK_NB : 0); ! if (php_stream_lock(stream, act)) { if (operation && errno == EWOULDBLOCK && arg3 && PZVAL_IS_REF(arg3)) { Z_LVAL_P(arg3) = 1; } ! RETURN_FALSE; } ! RETURN_TRUE; } /* }}} */ *** php5-200504050030.orig/main/streams/plain_wrapper.c Thu Oct 28 06:35:19 2004 --- php5-200504050030/main/streams/plain_wrapper.c Tue Apr 5 15:45:51 2005 *************** *** 578,584 **** return 0; } ! if (!flock(fd, value) || (errno == EWOULDBLOCK && value & LOCK_NB)) { data->lock_flag = value; return 0; } else { --- 578,584 ---- return 0; } ! if (!flock(fd, value)) { data->lock_flag = value; return 0; } else {