|  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
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
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
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-11-26 21:17 UTC] markamery at btinternet dot com
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 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:

Test script:

    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.");

    echo "Acquired lock\n";
    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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-26 13:34 UTC]
-Status: Open +Status: Verified
 [2016-08-26 13:34 UTC]
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-2021 The PHP Group
All rights reserved.
Last updated: Tue May 11 02:01:23 2021 UTC