#47227 [Bgs]: children forked with pcntl_fork release flock()s created by parent
| From: | murphyk at yahoo-inc dot com | Date: | Wed, 28 Jan 2009 15:13:09 +0000 |
| Subject: | #47227 [Bgs]: children forked with pcntl_fork release flock()s created by parent | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-133268@lists.php.net to get a copy of this message | ||
ID: 47227
User updated by: murphyk at yahoo-inc dot com
Reported By: murphyk at yahoo-inc dot com
Status: Bogus
Bug Type: PCNTL related
Operating System: RHEL 4.4
PHP Version: 5.2.8
New Comment:
I have RTFM, thanks.
From the man 2 flock:
"Locks created by flock() are associated with a file, or, more
precisely, 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."
"by an explicit LOCK_UN" - my child process did NOT call flock() in my
user code.
"or when all such descriptors have been closed" - my parent process
still has an open copy of the the filehandle.
To be clear, the "expected result" output is what happens in php4 - the
child closes the filehandle, but doesn't unlock the flock().
php5 appears to be explicitly unlocking all held flocks() during
shutdown. If this is intentional, fine, but it's not documented, and
it's a change in behavior between php4 and php5 which breaks previously
running code.
Previous Comments:
------------------------------------------------------------------------
[2009-01-28 08:44:50] jani@php.net
RTFM:
man 2 flock
man 2 fork
The behaviour is exactly how it should be and is.
------------------------------------------------------------------------
[2009-01-27 22:07:47] murphyk at yahoo-inc dot com
Description:
------------
The behavior of flock()ed files across forks appears to have changed
between php 4.4.8 and php 5.2.8.
Previously, children would inherit filehandles from their parent, and
would close() their copies of the filehandles on exit, but this would
not clear flock()s on the files locked in the parent.
Now, it appears that if any child exits(), this will clear flock()s on
any filehandles inherited from the parent.
Reproduce code:
---------------
<?php
$fp = fopen("/tmp/lock.txt", "w+");
if (flock($fp, LOCK_EX + LOCK_NB )) {
print "Got lock.\n";
} else {
print "Couldn't lock the file.\n";
exit();
}
$pid = pcntl_fork();
if ($pid) {
pcntl_waitpid($pid,$status);
}
else {
exit();
}
sleep(100);
?>
Expected result:
----------------
$ php flock.php &
[1] 22054
$ Got lock.
$ php flock.php
Couldn't lock the file.
Actual result:
--------------
$php flock.php &
[1] 31085
$Got lock.
$php flock.php
Got lock.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=47227&edit=1