I am using LOCK_EX and LOCK_SH with PHP and they look to work pretty good. But there is one problem that looks to be a bug (if this is the case, I will report after you guys tell me your opinion).
Save the simple code below to the file test.php
, for example.
<?php
$arquivo_lock = fopen("test.txt","a");
if (flock($arquivo_lock,LOCK_SH|LOCK_NB)) {
echo "Yes";
}
else{
echo "No";
}
sleep(10);
fclose($arquivo_lock);
?>
Now, in less than 10 seconds apart, open 2 (or more) tabs on your browser calling the file test.php
and you will see every tab will show you Yes
after some seconds. It should show Yes
only on the first tab, all the other should return immediatly with No
because I am using LOCK_NB
flag – this should be the expected result, however using or not using LOCK_NB
makes no change.
I am running this code in CentOS Stream with PHP 8 but I also got this bug to happen on Windows 10 and 11. I also tried opening one tab in Chrome and another tab in Firefox just to make sure the browser was not the curlprit waiting for the firt response to finish to start the second request.