php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #53316 Locks still do not have to be removed manually.
Submitted: 2010-11-15 11:57 UTC Modified: 2011-02-10 15:56 UTC
Votes:4
Avg. Score:5.0 ± 0.0
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:2 (50.0%)
From: DirectSoft1024 at mail dot ru Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.3.3 OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: DirectSoft1024 at mail dot ru
New email:
PHP Version: OS:

 

 [2010-11-15 11:57 UTC] DirectSoft1024 at mail dot ru
Description:
------------
Documentation at http://nl2.php.net/manual/en/function.flock.php contains phrase "The automatic unlocking when the file's resource handle is closed was removed. Unlocking now always has to be done manually."

Although it no longer calls flock(LOCK_UN) upon encountering fclose(), lock still becomes released by OS when file is closed. So, the phrase "unlocking now always has to be done manually" sounds confusing. Unlocking is still automatic just like it was bore. The only thing that changed is that now it's done not at PHP user side, but inside OS kernel.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-15 16:22 UTC] cataphract@php.net
Related: bug #51771.
 [2011-02-08 16:02 UTC] an0nym at narod dot ru
I've run against this issue as well. Reproduced on 
Linux 2.6.18-194.32.1.el5xen x86_64 GNU/Linux with PHP 5.3.5 (cli) (built: Jan 22 2011 10:27:18)
and FreeBSD 8.1-RELEASE amd64 with PHP 5.3.3 with Suhosin-Patch (cli) (built: Aug  4 2010 07:49:31). 


I insist this is not a documentation issue. Linux and FreeBSD mans strictly state 

Locks created by flock() are associated with an open file table entry. This means that duplicate file descriptors (created by, for example, fork(2) or dup(2)) refer to the same lock, and this lock may be modified or released using any of these descriptors. Furthermore, the lock is released either by an explicit LOCK_UN operation on any of these duplicate descriptors, or when all such descriptors have been closed.
If a process uses open(2) (or similar) to obtain more than one descriptor for the same file, these descriptors are treated independently by flock(). An attempt to lock the file using one of these file descriptors may be denied by a lock that the calling process has already placed via another descriptor. 

In other words, if multiple descriptors are open for the same file, flock must not be released until all of them are closed. Moreover, when all of them are closed, flock must be realesed automatically, not manually. 

How to reproduce
<?php
$h1 = fopen("test", "c");
var_dump(flock($h1, LOCK_EX | LOCK_NB, $wouldblock1));
var_dump($wouldblock1);
$h2 = fopen("test", "c");
var_dump(flock($h2, LOCK_EX | LOCK_NB, $wouldblock2));
var_dump($wouldblock2);
var_dump(fclose($h1));
$h3 = fopen("test", "c");
var_dump(flock($h3, LOCK_EX | LOCK_NB, $wouldblock3));
var_dump($wouldblock3);

Expected behaviour
bool(true)
int(0)
bool(false)
int(1)
bool(true)
bool(false)
int(1)

Real behaviour
bool(true)
int(0)
bool(false)
int(1)
bool(true)
bool(true)
int(0)
 [2011-02-08 16:08 UTC] DirectSoft1024 at mail dot ru
Ok, I forgot about fork() case. What I said is valid only for single-process environment without any duplicate handles.
 [2011-02-08 16:37 UTC] an0nym at narod dot ru
It seems that I'm not right. I've forgot about fork() case as well and that is why I misinterpreted the manual. 

The problem in PHP is that we cannot dup() file descriptor. fork() seems to do this internally, that is why in case of fork() with flock()ed open descriptor, fclose()ing file descriptor either by parent or by child does not release lock, until the second one closes this as well. 

This is neither a bug nor a documentation being wrong. It is a feature request (provide a way to dup() file descriptor in PHP) or documentation improvement request (it should tell more about the difference between dup() and open()).
 [2011-02-10 15:56 UTC] vrana@php.net
-Status: Open +Status: Bogus
 [2011-02-10 15:56 UTC] vrana@php.net
Fill-in a new bug report as Feature request for dup().
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 20:00:02 2026 UTC