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
Submitted: 2014-11-26 21:17 UTC Modified: 2021-07-26 18:22 UTC
Votes:1
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
 [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.
 [2021-07-26 18:21 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2021-07-26 18:21 UTC] cmb@php.net
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
lockf(3)[3]:

| 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] <https://linux.die.net/man/2/flock>
[2] <https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex>
[3] <https://linux.die.net/man/3/lockf>
 [2021-07-26 18:22 UTC] cmb@php.net
-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] git@php.net
Automatic comment on behalf of cmb69
Revision: https://github.com/php/doc-en/commit/0bfb0eb957e0468912af86bba09fae588010c1df
Log: Fix #68509: Garbage collection of file pointers releases flock() locks
 [2021-07-26 18:27 UTC] git@php.net
-Status: Verified +Status: Closed
 [2021-07-27 03:15 UTC] git@php.net
Automatic comment on behalf of mumumu
Revision: https://github.com/php/doc-ja/commit/ed3df60341f41d6d2c256f313a4356e80ad4bf06
Log: Fix #68509: Garbage collection of file pointers releases flock() locks
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Jul 28 18:01:25 2021 UTC