Bug #51969 [Opn]: fwrite returns 1 when trying to write to a locked file
| From: | fa@php.net | Date: | Thu, 10 Jun 2010 12:50:20 +0000 |
| Subject: | Bug #51969 [Opn]: fwrite returns 1 when trying to write to a locked file | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-151292@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=51969&edit=1
ID: 51969
Updated by: fa@php.net
Reported by: scarduzio at gmail dot com
Summary: fwrite returns 1 when trying to write to a locked file
Status: Open
Type: Bug
Package: Filesystem function related
Operating System: Windows 7 x86
PHP Version: 5.3.2
New Comment:
Ah, ok, thank you for the clarification.
Tried to reproduce now, indeed fwrite in the else clause ignores the
flock() - but then again you already checked if there is a lock, that is
exactly why you are in the else code path, so I'm not exactly sure how
flock *should* behave ...
Trying to open the locked file gives a windows warning that the file is
indeed locked, though.
Same result with PHP 5.3.2 (cli) on Debian lenny, btw - minus the
warning on accessing the file directly.
Previous Comments:
------------------------------------------------------------------------
[2010-06-10 12:26:50] scarduzio at gmail dot com
Hi, thanks for replying.
The OS in use is Windows 7 x86 genuine with all windows updates.
The path with forward slash that I use in my code is unix style because
I
noticed
PHP always translates it correctly into "C:\cache\lockable.txt", and
it's always
been fine. Also this time, the file is always found.
About the manual line stating LOCK_NB bitmask is not supported: as
evinced from
my tests, including this one, the use of LOCK_NB is behaving perfectly
since it
detects file is locked, and executes my "else".
I also do not think LOCK_NB is the core of this problem since I could
split this
script in two (one with the flock+sleep and the other with the "else"
content)
and reproduce the same bug.
I later tested this in linux environment, fwrite will return 5 (not 1).
Which is
still funny, since writing in a locked file should in my opinion return
false.
I look forward for your further analysis.
BR,
Simone
------------------------------------------------------------------------
[2010-06-10 12:04:33] fa@php.net
From the manual:
"It is also possible to add LOCK_NB as a bitmask to one of the above
operations if you don't want flock() to block while locking. (not
supported on Windows)"
Then again path "/cache/lockable.txt" doesn't look like windows - so
what OS are you using?
And while I know that's possibly not directly related to your problem -
please clarify.
------------------------------------------------------------------------
[2010-06-01 23:45:32] scarduzio at gmail dot com
Description:
------------
When I try to write any length string with fwrite using a file handle I
previously
locked with flock, fwrite is of course unable to write, but will return
unexplicable value 1 instead of false.
Test script:
---------------
/*
Run two instances of this script in a rapid sequence
*/
$fp1 = fopen("/cache/lockable.txt","w");
if(flock($fp1, LOCK_EX | LOCK_NB)){
echo "locked fp1\n";
sleep(20);
}else{
$res = fwrite($fp1, "ABCDE");
if(false === $res){
echo "failed the write\n";
}
echo "I managed! fwrite returned: "; print_r($res);
}
Expected result:
----------------
I expect to read "failed to write" when a second instance of the script
is run,
since fwrite should return false as documentation says when fwrite is
not able to
write.
Actual result:
--------------
fwrite returns integer 1.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=51969&edit=1