Professional Web Applications Themes

#35326 [Opn]: Concurrency issue with recursive mkdir - PHP Bugs

ID: 35326 User updated by: bugs dot php dot net at chsc dot dk Reported By: bugs dot php dot net at chsc dot dk Status: Open Bug Type: Feature/Change Request Operating System: Linux -PHP Version: 5.0.5 +PHP Version: 5.1.6 New Comment: Problem still exists in PHP 5.1.6. Previous Comments: ------------------------------------------------------------------------ [2005-11-22 13:52:24] bugs dot php dot net at chsc dot dk Yes, it also occurs with the CVS snapshot (this time I tried with the CGI version). AFAICS the problem is in php_plain_files_mkdir in main/streams/plain_wrapper.c. If php_mkdir() or VCWD_MKDIR() returns false, the method gives up instead of checking whether ...

  1. #1

    Default #35326 [Opn]: Concurrency issue with recursive mkdir

    ID: 35326
    User updated by: bugs dot php dot net at chsc dot dk
    Reported By: bugs dot php dot net at chsc dot dk
    Status: Open
    Bug Type: Feature/Change Request
    Operating System: Linux
    -PHP Version: 5.0.5
    +PHP Version: 5.1.6
    New Comment:

    Problem still exists in PHP 5.1.6.


    Previous Comments:
    ------------------------------------------------------------------------

    [2005-11-22 13:52:24] bugs dot php dot net at chsc dot dk

    Yes, it also occurs with the CVS snapshot (this time I tried with the
    CGI version).

    AFAICS the problem is in php_plain_files_mkdir in
    main/streams/plain_wrapper.c. If php_mkdir() or VCWD_MKDIR() returns
    false, the method gives up instead of checking whether directory exists
    anyway, i.e. if it was created by another thread.

    ------------------------------------------------------------------------

    [2005-11-22 12:13:10] [email]tony2001php.net[/email]

    Please try using this CVS snapshot:

    [url]http://snaps.php.net/php5-latest.tar.gz[/url]

    For Windows:

    [url]http://snaps.php.net/win32/php5-win32-latest.zip[/url]



    ------------------------------------------------------------------------

    [2005-11-22 11:32:49] bugs dot php dot net at chsc dot dk

    Description:
    ------------
    There appears to be a concurrency issue when mkdir is called on two
    separate threads with the following parameters:

    Thread 1:
    mkdir('/tmp/foo/bar/1', 0777, true);

    Thread 2:
    mkdir('/tmp/foo/bar/2', 0777, true);

    Sometimes one of the threads (e.g. thread 2) two fails with "Warning:
    mkdir(): File exists" and the directory /tmp/foo/bar/2 does not exist
    afterwards.

    What I think is happening in thread 2 is is this:
    PHP looks for /tmp/foo/bar/2 and sees that it does not exist. Neither
    does /tmp/foo/bar and /tmp/foo. So it tries to create /tmp/foo, but
    that directory was just created a millisecond second ago by another
    thread, so mkdir gives instead of just moving on to create /tmp/foo/bar
    and /tmp/foo/bar/2.

    Reproduce code:
    ---------------
    This code reproduces the problem in about every second attempt. It
    consists of two files, index.php and iframe.php.


    index.php:

    <?php
    $fire = time() + 2;
    ?>
    <h1>Nr. 1</h1>
    <iframe src="iframe.php?fire=<?= $fire ?>&amp;no=1"></iframe>
    <h1>Nr. 2</h1>
    <iframe src="iframe.php?fire=<?= $fire ?>&amp;no=2"></iframe>


    iframe.php:

    <?php
    $fire = $_GET['fire'];
    $no = $_GET['no'];
    // synchronize the two threads
    while ($fire > time());
    $dir = '/tmp/' . $fire . '/foo/bar/foo/bar/' . $no;
    //sleep($no);
    var_dump(is_dir($dir));
    echo "Making $dir ";
    mkdir($dir, 0777, true);
    var_dump(is_dir($dir));
    ?>


    Expected result:
    ----------------
    Thread 2 should output this:
    bool(false) Making /tmp/1132653651/foo/bar/foo/bar/2 bool(true)


    Actual result:
    --------------
    Thread 2 sometimes outputs this:
    bool(false) Making /tmp/1132655181/foo/bar/foo/bar/2 Warning: mkdir():
    File exists in /home/chsc/public_html/mkdir/iframe.php on line 10
    bool(false)

    Notice that the directory does not exist afterwards.

    If I uncomment the sleep() call above, there are no problems.


    A workaround:
    $ok = mkdir($dir, 0777, true);
    if (!$ok && !is_dir($dir)) {
    sleep(1);
    mkdir($dir, 0777, true);
    }


    In other words, it works if I try again. I hope that mkdir() can be
    changed so that it only returns false if the directory cannot be
    created at all (e.g. because of lack of permissions or because it
    already exists).



    ------------------------------------------------------------------------


    --
    Edit this bug report at [url]http://bugs.php.net/?id=35326&edit=1[/url]
    bugs dot php dot net at chsc dot dk Guest

  2. #2

    Default Re: #35326 [Opn]: Concurrency issue with recursive mkdir

    thanks dude! this code really helps me to figure out the problem.
    zwacks Guest

Similar Threads

  1. Replies: 0
    Last Post: November 15th, 12:22 AM
  2. Replies: 0
    Last Post: November 11th, 10:58 PM
  3. CONCURRENCY
    By sacto_dummy in forum Coldfusion - Getting Started
    Replies: 6
    Last Post: September 25th, 09:48 PM
  4. Web Service Concurrency
    By Chris Amaru in forum ASP.NET Web Services
    Replies: 3
    Last Post: January 28th, 10:13 PM
  5. #24811 [NEW]: recursive data structures issue
    By xris at siliconmechanics dot com in forum PHP Development
    Replies: 0
    Last Post: July 25th, 09:43 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139