Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LOCK_NB Ignored

Tags:

php

flock

running this code twice :

$fp = @fopen('test.test', "wb");

    if (flock($fp, LOCK_NB | LOCK_EX)){
                @fwrite($fp, $data);
                echo 'written';
                sleep(5);
    }else{
        echo 'skipped , ok';
    }

    @flock($fp, LOCK_UN);
    @fclose($fp);

always gives me the output of "written"

Means the LOCK_NB is skipped , any clues (on both winbdows and unix)

EDIT (2012-03-29 still not fixed): https://bugs.php.net/bug.php?id=54453&edit=3 PHP Bug #54453

like image 432
Rami Dabain Avatar asked Apr 02 '11 15:04

Rami Dabain


1 Answers

When using Apache+PHP I was tricked into believing LOCK_NB was ignored (it wasn't, it was the browser waiting for the first request to finish).

Because I was making 2 requests with the same browser, the browser was waiting for the first call to finish before making the next one (even ignoring a "Connection: close" header).

Using 2 separate browsers (in my case Chrome + Firefox, or Chrome + wget on the server) I concluded LOCK_NB worked just fine.

If a file in w+ mode was locked with LOCK_EX | LOCK_NB, attempting another LOCK_EX | LOCK_NB on the same file returned false (the intended behaviour).

like image 118
Tiberiu-Ionuț Stan Avatar answered Oct 05 '22 05:10

Tiberiu-Ionuț Stan