Open
Description
Description
The following code:
<?php
echo "Start script\n";
$fp = fopen(__FILE__, 'r+');
if (flock($fp, LOCK_EX, $block)) {
echo "Job running\n";
sleep(5);
echo "Job finished\n";
flock($fp, LOCK_UN);
} else {
if ($block) {
echo "Could not get the lock! Other script in run\n";
} else {
echo "Some other problem\n";
}
}
How to reproduce
- Save script above in some test file, for example,
a.php
- Open 2 command prompts on Windows
- Run script in first command prompt
php -n a.php
- While script is still running in first command prompt run the same in second command prompt window
php -n a.php
Result
- In first window script get lock and echo "Start script", "Job running" and after 5 seconds "Job finished"
- In second window script is not even run as do not echo even "Start script" and exits immediately without any error message or something
Expected output
- In second command prompt window script should execute and if condition with flock set $block=1 and echo "Could not get the lock! Other script in run"
PHP Version
PHP 8.2.5 (cli) (built: Apr 12 2023 08:41:53) (ZTS Visual C++ 2019 x86)
Copyright (c) The PHP Group
Zend Engine v4.2.5, Copyright (c) Zend Technologies
with Zend OPcache v8.2.5, Copyright (c), by Zend Technologies
Tested also on PHP 7.4 and that's the same there.
Operating System
Microsoft Windows [Version 10.0.19045.5679]