php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65272 flock() documentation says LOCK_NB is not available on Windows, although it is
Submitted: 2013-07-16 15:09 UTC Modified: 2015-02-21 13:20 UTC
From: benjamin dot morel at gmail dot com Assigned: ab (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: N/A
Private report: No CVE-ID: None
 [2013-07-16 15:09 UTC] benjamin dot morel at gmail dot com
Description:
------------
The flock() documentation:
http://php.net/manual/en/function.flock.php

Says:
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)

In my tests on Windows 7 x64, it does work. But what is true is that the failed 
flock() does not set &$wouldblock to 1.

Please see the test script below.
Both scripts must be run at the same time in PHP interactive mode or with a 
sleep() to avoid the lock being released automatically.

It would be interesting to test this behaviour on other versions of Windows 
(this might not be applicable to all of them), and update the documentation 
accordingly. Non-blocking locks are a really useful feature, and it would be 
nice to document this properly for Windows users.



Test script:
---------------

Script 1
--------

$f = fopen('file.txt', 'w');
var_export(flock($f, LOCK_EX | LOCK_NB, $wouldblock)); // true
var_export($wouldblock); // 0

Script 2
--------

$f = fopen('file.txt', 'w');
var_export(flock($f, LOCK_EX | LOCK_NB, $wouldblock)); // false
var_export($wouldblock); // 0 (1 on Linux)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-02 16:31 UTC] rdlowrey@php.net
Automatic comment on behalf of rdlowrey
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f2f467bd9609f5fad2491a4718380fadaa449470
Log: Fixed bug #65272: correctly set flock() out param in windows
 [2015-02-02 16:31 UTC] rdlowrey@php.net
-Status: Open +Status: Closed
 [2015-02-02 16:31 UTC] rdlowrey@php.net
Automatic comment on behalf of rdlowrey
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f2f467bd9609f5fad2491a4718380fadaa449470
Log: Fixed bug #65272: correctly set flock() out param in windows
 [2015-02-02 16:31 UTC] rdlowrey@php.net
Automatic comment on behalf of rdlowrey
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f2f467bd9609f5fad2491a4718380fadaa449470
Log: Fixed bug #65272: correctly set flock() out param in windows
 [2015-02-02 16:38 UTC] rdlowrey@php.net
This is a documentation bug, but it's also a problem in the actual implementation in windows environments. The error constant used to determine the value of $wouldblock is different in windows than other environments. This issue has been corrected in 5.5 and up.

The implementation should now be consistent across all platforms and the documentation should be updated appropriately.
 [2015-02-02 16:43 UTC] benjamin dot morel at gmail dot com
@rdlowrey Who should update the documentation?
 [2015-02-02 18:46 UTC] peehaa@php.net
The docs are updated to reflect the fact that LOCK_NB does indeed work on Windows systems.

I also have a docs patch ready when rdlowrey's changes are released.
 [2015-02-21 12:34 UTC] ab@php.net
-Assigned To: +Assigned To: ab
 [2015-02-21 12:34 UTC] ab@php.net
@rdlowrey, this fix seems to be not correct. ERROR_INVALID_BLOCK has nothing to do with locking.

Thanks.
 [2015-02-21 13:20 UTC] ab@php.net
@rdlowrey, pushed an improvement. Tests pass.

Thanks.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC