Skip to content

PHP runner does exit if script file is locked #18272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
DarkSide666 opened this issue Apr 7, 2025 · 5 comments
Open

PHP runner does exit if script file is locked #18272

DarkSide666 opened this issue Apr 7, 2025 · 5 comments

Comments

@DarkSide666
Copy link

DarkSide666 commented Apr 7, 2025

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

  1. Save script above in some test file, for example, a.php
  2. Open 2 command prompts on Windows
  3. Run script in first command prompt php -n a.php
  4. 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]

@iluuu1994
Copy link
Member

Can't reproduce on Linux. Maybe @cmb69 can confirm?

@mvorisek
Copy link
Contributor

mvorisek commented Apr 7, 2025

I can reproduce this on my Windows machine as well.

Minimal repro:

<?php

$handle = fopen(__FILE__, 'r');
var_dump(flock($handle, LOCK_EX));

sleep(5);

echo "done\n";

Steps to reproduce:

  1. run php -n test.php
  2. in another console, run the same command before the 1st run exits
  3. notice, the 2nd run exists immediately without any output nor error message

Image

@DarkSide666
Copy link
Author

Tried @mvorisek script on Ubuntu 24.04.2 LTS and there 2nd script stops on flock line and waits while 1st release the lock and when lock is released then continue executing.
This is what I expected to happen in Windows too.

@mvorisek
Copy link
Contributor

mvorisek commented Apr 8, 2025

When opcache is loaded & enabled for CLI, ie. the repro script is run like

php -n -d extension_dir=ext -d zend_extension=opcache -d opcache.enable_cli=1 test.php

the problem does not exist even on Windows.

@mvorisek
Copy link
Contributor

mvorisek commented Apr 8, 2025

Here is explanation of this issue:

repro:

<?php

$handle = fopen(__FILE__, 'r');
var_dump(flock($handle, LOCK_EX));
var_dump(file_get_contents(__FILE__));

which outputs on Windows:

bool(true)
string(0) ""

If file is locked, even from the same php process, it is read as empty string /wo any failure!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants