|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68509 Garbage collection of file pointers releases flock() locks
Submitted: 2014-11-26 21:17 UTC Modified: 2021-07-26 18:22 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: cmb (profile)
Status: Closed Package: Filesystem function related
PHP Version: master-Git-2014-11-26 (Git) OS: Unix
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: markamery at btinternet dot com
New email:
PHP Version: OS:


 [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.
 [2021-07-26 18:21 UTC]
-Assigned To: +Assigned To: cmb
 [2021-07-26 18:21 UTC]
man 2 flock[1]:

| 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.

That's basically the same on Windows which uses LockFileEx[2]:

| If a process terminates with a portion of a file locked or
| closes a file that has outstanding locks, the locks are unlocked
| by the operating system.

Non-Windows systems which do not support flock(2), basically use

| File locks are released as soon as the process holding the locks
| closes some file descriptor for the file.

I have no idea why that "prior to PHP 5.3.2" is there.

[1] <>
[2] <>
[3] <>
 [2021-07-26 18:22 UTC]
-Summary: Garbage collection of file pointers releases flock() locks erroneously +Summary: Garbage collection of file pointers releases flock() locks
 [2021-07-26 18:27 UTC]
Automatic comment on behalf of cmb69
Log: Fix #68509: Garbage collection of file pointers releases flock() locks
 [2021-07-26 18:27 UTC]
-Status: Verified +Status: Closed
 [2021-07-27 03:15 UTC]
Automatic comment on behalf of mumumu
Log: Fix #68509: Garbage collection of file pointers releases flock() locks
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jul 14 07:01:29 2024 UTC