php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68509 Garbage collection of file pointers releases flock() locks erroneously
Submitted: 2014-11-26 21:17 UTC Modified: 2016-08-26 13:34 UTC
From: markamery at btinternet dot com Assigned:
Status: Verified Package: Filesystem function related
PHP Version: master-Git-2014-11-26 (Git) OS: Unix
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: markamery at btinternet dot com
New email:
PHP Version: OS:

 

 [2014-11-26 21:17 UTC] markamery at btinternet dot com
Description:
------------
If you acquire a file pointer with fopen() from within a function, then take out a lock on it with flock(), the lock gets released when the function returns unless you keep a reference to the file pointer in scope to prevent it from being garbage collected.

This seems wrong on two counts:

* It's unintuitive to the user that implicit garbage collection could be affecting their locks; the user should be protected from such concerns. In any case, this interaction between garbage collection and locks is undocumented.
* It appears to contradict the documentation at http://php.net/manual/en/function.flock.php which claims that:
    
      > The automatic unlocking when the file's resource handle is closed was removed. Unlocking now always has to be done manually.

  In fact, it is the case that both explicit fclose() calls and the file pointer being garbage collected result in the lock being released, contrary to the documentation.

This should probably be fixed by tracking whether a file pointer has been locked with flock(), and suppressing the garbage collection of file pointers for which there are active flock() locks.

Related: I have previously posted a question and answer on Stack Overflow about this issue: http://stackoverflow.com/questions/24351769/flock-call-within-function-always-succeeds-ignoring-previous-lock

Test script:
---------------
<?php

    function acquire_lock () {
        $file_handle = fopen('mylock.lock', 'w');
        $got_lock_successfully = flock($file_handle, LOCK_EX);
        if (!$got_lock_successfully) {
            throw new Exception("Unexpected failure to acquire lock.");
        }
    }

    acquire_lock();
    echo "Acquired lock\n";
    sleep(10);
    echo "terminating\n";

?>

Expected result:
----------------
When starting the test script above in two terminals in rapid succession, the second terminal should not display the "Acquired lock" message until the first terminal has shown the "terminating" message.

Actual result:
--------------
The second terminal shows the "Acquired lock" message as soon as the script is started, before the first script has shown the "terminating" message.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-26 13:34 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2016-08-26 13:34 UTC] cmb@php.net
I can confirm the behavior (mode "w" might not necessarily work,
but I also get the behavior with "c" and "r") on Windows.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC