php / php/php-src

PHP runner does exit if script file is locked

Open
#18,272 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug OS: Windows Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

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

  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]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the a.php script using php -n on Windows, running it simultaneously from two command prompts. Start at the PHP CLI and Windows file-locking behavior; done means the second process prints "Start script" and reaches the flock branch that reports the existing lock instead of exiting silently.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
cli, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.