php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51969 fwrite returns 1 when trying to write to a locked file
Submitted: 2010-06-01 23:45 UTC Modified: 2015-04-12 21:19 UTC
Votes:7
Avg. Score:4.4 ± 0.9
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:1 (25.0%)
From: scarduzio at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Filesystem function related
PHP Version: 5.3.2 OS: Windows 7 x86
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: scarduzio at gmail dot com
New email:
PHP Version: OS:

 

 [2010-06-01 23:45 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-10 12:04 UTC] fa@php.net
-Status: Open +Status: Feedback
 [2010-06-10 12:04 UTC] 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-10 12:26 UTC] scarduzio at gmail dot com
-Status: Feedback +Status: Open
 [2010-06-10 12:26 UTC] 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 14:50 UTC] fa@php.net
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.
 [2015-04-12 21:19 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-04-12 21:19 UTC] cmb@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You can't lock a file that you're opening in "w" mode reliably. See
also the respective note in the flock man page[1] and the
documentation of the $mode parameter of fopen[2].

On Windows locking a file opened in "w" mode might work reliably.
However, testing on PHP 5.4.19 revealed that fwrite() returned 0 in
the test script.

[1] <http://php.net/manual/en/function.flock.php#refsect1-function.flock-notes>
[2] <http://php.net/manual/en/function.fopen.php>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Nov 19 09:00:01 2025 UTC