FAQ
Edit report at http://pear.php.net/bugs/bug.php?id=18192&edit=1

ID: 18192
Updated by: tacker@php.net
Reported By: roborg at hotmail dot com
Summary: Improper file opening in _write method
-Status: Verified
+Status: Feedback
Type: Bug
Package: Cache_Lite
Operating System: All
Package Version: 1.7.8
PHP Version: Irrelevant
Assigned To: tacker
Roadmap Versions:
New Comment:

-Status: Verified
+Status: Feedback
Gregory,

I cannot reproduce the bug via the console.

I've added this script:
http://svn.php.net/viewvc/pear/packages/Cache_Lite/trunk/tests/pearbug18192.php?revision=308092&view=markup

Executing it via:
php pearbug18192-sub.php & php pearbug18192-sub.php & php
pearbug18192-sub.php & php pearbug18192-sub.php & php
pearbug18192-sub.php & php pearbug18192-sub.php & php
pearbug18192-sub.php & php pearbug18192-sub.php & php
pearbug18192-sub.php & php pearbug18192-sub.php & php
pearbug18192-sub.php & php pearbug18192-sub.php & php
pearbug18192-sub.php & php pearbug18192-sub.php
does not yield a single error.


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

[2011-01-14 11:50:54] tacker

-Status: Open
+Status: Verified
-PHP Version: 5.2.8
+PHP Version: Irrelevant
-Assigned To:
+Assigned To: tacker


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

[2011-01-13 12:25:41] roborg

Added #patch bug:18192;patch:patch.txt;revision:1294921541;.

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

[2011-01-13 12:16:21] roborg

Description:
------------
The cache file to write to is opened with mode "wb" before it is
locked, so a read can occur between the file being opened
(and truncated) and locked. Running two copies of the test
script simultaneously will show this behaviour.

Suggested fix is to change line 766 onwards from:

$fp = @fopen($this->_file, "wb");
if ($fp) {
if ($this->_fileLocking) @flock($fp, LOCK_EX);

to

$fp = @fopen($this->_file, "cb");
if ($fp) {
if ($this->_fileLocking) @flock($fp, LOCK_EX);
ftruncate($fp, 0);

Test script:
---------------
<?php

require_once 'Cache/Lite.php';

$c = new Cache_Lite(array('cacheDir' => '.'));
$str = 'test';

for ($i = 0; $i < 10000; $i++)
{
if (!$c->save('test', $str))
die('Error saving');

if ($c->get('test') != $str)
die('Wrong data');
}


Expected result:
----------------
No output

Actual result:
--------------
die() is called

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

Search Discussions

  • Roborg at Feb 24, 2011 at 9:04 am
    Edit report at http://pear.php.net/bugs/bug.php?id=18192&edit=1

    ID: 18192
    Comment by: roborg@hotmail.com
    Reported By: roborg at hotmail dot com
    Summary: Improper file opening in _write method
    Status: Feedback
    Type: Bug
    Package: Cache_Lite
    Operating System: All
    Package Version: 1.7.8
    PHP Version: Irrelevant
    Assigned To: tacker
    Roadmap Versions:
    New Comment:

    That's odd - I just ran your script on my server and was flooded with
    "Error saving" and "Wrong data".

    I ran your script & command line on Red Hat Enterprise Linux Server
    release 5.5 (Tikanga) with PHP 5.3.3 but I verified it using Apache and
    PHP 5.3.5 on Windows XP as well. Both machines are multi-processor.


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

    [2011-02-07 11:52:33] tacker

    -Status: Verified
    +Status: Feedback
    Gregory,

    I cannot reproduce the bug via the console.

    I've added this script:
    http://svn.php.net/viewvc/pear/packages/Cache_Lite/trunk/tests/pearbug18192.php?revision=308092&view=markup

    Executing it via:
    php pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php
    does not yield a single error.

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

    [2011-01-14 11:50:54] tacker

    -Status: Open
    +Status: Verified
    -PHP Version: 5.2.8
    +PHP Version: Irrelevant
    -Assigned To:
    +Assigned To: tacker


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

    [2011-01-13 12:25:41] roborg

    Added #patch bug:18192;patch:patch.txt;revision:1294921541;.

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

    [2011-01-13 12:16:21] roborg

    Description:
    ------------
    The cache file to write to is opened with mode "wb" before it is
    locked, so a read can occur between the file being opened
    (and truncated) and locked. Running two copies of the test
    script simultaneously will show this behaviour.

    Suggested fix is to change line 766 onwards from:

    $fp = @fopen($this->_file, "wb");
    if ($fp) {
    if ($this->_fileLocking) @flock($fp, LOCK_EX);

    to

    $fp = @fopen($this->_file, "cb");
    if ($fp) {
    if ($this->_fileLocking) @flock($fp, LOCK_EX);
    ftruncate($fp, 0);

    Test script:
    ---------------
    <?php

    require_once 'Cache/Lite.php';

    $c = new Cache_Lite(array('cacheDir' => '.'));
    $str = 'test';

    for ($i = 0; $i < 10000; $i++)
    {
    if (!$c->save('test', $str))
    die('Error saving');

    if ($c->get('test') != $str)
    die('Wrong data');
    }


    Expected result:
    ----------------
    No output

    Actual result:
    --------------
    die() is called

    ------------------------------------------------------------------------
  • Tacker at Feb 26, 2011 at 1:20 pm
    Edit report at http://pear.php.net/bugs/bug.php?id=18192&edit=1

    ID: 18192
    Updated by: tacker@php.net
    Reported By: roborg at hotmail dot com
    Summary: Improper file opening in _write method
    -Status: Feedback
    +Status: Verified
    Type: Bug
    Package: Cache_Lite
    Operating System: All
    Package Version: 1.7.8
    PHP Version: Irrelevant
    Assigned To: tacker
    Roadmap Versions:
    New Comment:

    -Status: Feedback
    +Status: Verified
    Ok, I can vereify this also on an archlinux machine with PHP 5.3.5 and
    suhosin


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

    [2011-02-24 10:07:12] roborg

    That's odd - I just ran your script on my server and was flooded with
    "Error saving" and "Wrong data".

    I ran your script & command line on Red Hat Enterprise Linux Server
    release 5.5 (Tikanga) with PHP 5.3.3 but I verified it using Apache and
    PHP 5.3.5 on Windows XP as well. Both machines are multi-processor.

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

    [2011-02-07 11:52:33] tacker

    -Status: Verified
    +Status: Feedback
    Gregory,

    I cannot reproduce the bug via the console.

    I've added this script:
    http://svn.php.net/viewvc/pear/packages/Cache_Lite/trunk/tests/pearbug18192.php?revision=308092&view=markup

    Executing it via:
    php pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php
    does not yield a single error.

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

    [2011-01-14 11:50:54] tacker

    -Status: Open
    +Status: Verified
    -PHP Version: 5.2.8
    +PHP Version: Irrelevant
    -Assigned To:
    +Assigned To: tacker


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

    [2011-01-13 12:25:41] roborg

    Added #patch bug:18192;patch:patch.txt;revision:1294921541;.

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

    [2011-01-13 12:16:21] roborg

    Description:
    ------------
    The cache file to write to is opened with mode "wb" before it is
    locked, so a read can occur between the file being opened
    (and truncated) and locked. Running two copies of the test
    script simultaneously will show this behaviour.

    Suggested fix is to change line 766 onwards from:

    $fp = @fopen($this->_file, "wb");
    if ($fp) {
    if ($this->_fileLocking) @flock($fp, LOCK_EX);

    to

    $fp = @fopen($this->_file, "cb");
    if ($fp) {
    if ($this->_fileLocking) @flock($fp, LOCK_EX);
    ftruncate($fp, 0);

    Test script:
    ---------------
    <?php

    require_once 'Cache/Lite.php';

    $c = new Cache_Lite(array('cacheDir' => '.'));
    $str = 'test';

    for ($i = 0; $i < 10000; $i++)
    {
    if (!$c->save('test', $str))
    die('Error saving');

    if ($c->get('test') != $str)
    die('Wrong data');
    }


    Expected result:
    ----------------
    No output

    Actual result:
    --------------
    die() is called

    ------------------------------------------------------------------------
  • Tacker at Feb 26, 2011 at 1:56 pm
    Edit report at http://pear.php.net/bugs/bug.php?id=18192&edit=1

    ID: 18192
    Updated by: tacker@php.net
    Reported By: roborg at hotmail dot com
    Summary: Improper file opening in _write method
    -Status: Verified
    +Status: Suspended
    Type: Bug
    Package: Cache_Lite
    Operating System: All
    Package Version: 1.7.8
    PHP Version: Irrelevant
    Assigned To: tacker
    Roadmap Versions:
    New Comment:

    -Status: Verified
    +Status: Suspended
    Your changes will not fix the "problem".

    The internals of Cache_Lite do not have a global cache locking. Instead
    during a write, the cache file is locked, data is written, file is
    unlocked and then the written data is verified with a second lock. In
    between cache writes can happen from other processes.

    See _writeAndControl() for references.

    So what you are really seeing are results of locking not beeing
    thoroughly executed.

    A real fix would a far more complete rewrite of the locking mechanism.


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

    [2011-02-26 14:23:20] tacker

    -Status: Feedback
    +Status: Verified
    Ok, I can vereify this also on an archlinux machine with PHP 5.3.5 and
    suhosin

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

    [2011-02-24 10:07:12] roborg

    That's odd - I just ran your script on my server and was flooded with
    "Error saving" and "Wrong data".

    I ran your script & command line on Red Hat Enterprise Linux Server
    release 5.5 (Tikanga) with PHP 5.3.3 but I verified it using Apache and
    PHP 5.3.5 on Windows XP as well. Both machines are multi-processor.

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

    [2011-02-07 11:52:33] tacker

    -Status: Verified
    +Status: Feedback
    Gregory,

    I cannot reproduce the bug via the console.

    I've added this script:
    http://svn.php.net/viewvc/pear/packages/Cache_Lite/trunk/tests/pearbug18192.php?revision=308092&view=markup

    Executing it via:
    php pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php & php
    pearbug18192-sub.php & php pearbug18192-sub.php
    does not yield a single error.

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

    [2011-01-14 11:50:54] tacker

    -Status: Open
    +Status: Verified
    -PHP Version: 5.2.8
    +PHP Version: Irrelevant
    -Assigned To:
    +Assigned To: tacker


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

    [2011-01-13 12:25:41] roborg

    Added #patch bug:18192;patch:patch.txt;revision:1294921541;.

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

    The remainder of the comments for this report are too long. To view
    the rest of the comments, please view the bug report online at
    http://pear.php.net/bugs/bug.php?id=18192

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppear-bugs @
categoriesphp
postedFeb 7, '11 at 10:50a
activeFeb 26, '11 at 1:56p
posts4
users2
websitepear.php.net

2 users in discussion

Tacker: 3 posts Roborg: 1 post

People

Translate

site design / logo © 2022 Grokbase