php / php/php-src

BUG: locks not working as expected

Open
#14,592 22 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Category: Streams 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
$arquivo_lock = fopen("test.txt","a");

if (flock($arquivo_lock,LOCK_EX|LOCK_NB)) { 
    echo "Yes";
} else {
    echo "No";
}

sleep(10);

fclose($arquivo_lock);
?>

Save the simple code above to the file test.php, for example. 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 bug is pretty simple to reproduce and other people also confirmed it on Stackoverflow when I reported there.

PHP Version

8.x

Operating System

Confirmed at least on centos 9 stream and windows 11

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

Start by running the two-tab test.php reproduction with PHP 8.x on the reported CentOS 9 Stream and Windows 11 environments, focusing on flock with LOCK_EX|LOCK_NB. Trace the flock entry point and platform-specific locking behavior; done means the first request reports Yes while overlapping requests return No immediately, with coverage for the confirmed environments.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.