Skip to content

PHP runner does exit if script file is locked #18272

Open
@DarkSide666

Description

@DarkSide666

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]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions