php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65333 apc_store() fails when memory usage is high
Submitted: 2013-07-25 10:45 UTC Modified: 2013-08-05 19:52 UTC
From: iwanski dot marcin at gmail dot com Assigned: krakjoe (profile)
Status: Closed Package: APC (PECL)
PHP Version: 5.4.17 OS: Linux
Private report: No CVE-ID: None
 [2013-07-25 10:45 UTC] iwanski dot marcin at gmail dot com
Description:
------------
When two or more concurrent scripts try to save data to the APC user cache, and 
APC memory usage is high (near limit set by apc.shm_size option), function 
apc_store() randomly fails to save data and returns false. 

I`ve updated APC to latest available version from trunk (3.1.15-dev).

I provide simple test script. To reproduce the bug, you should:
1) Set apc.shm_size to relative small value (eg. 32M)
2) Leave the rest of options at default values
3  Run test script simultaneously in two different browsers. This is very 
important to have at least two instances executing at a time, because one single 
script will always work correctly. If the problem doesn`t occur, try to repeat 
this several times and make sure you have reached full memory usage allowed by 
limit (one execution of a script takes approximately 12MB of RAM).


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

for ($i = 0; $i < 20000; $i++) {
    $unique = uniqid();
    if (false === apc_store($unique, $unique, 3600)) {
        throw new Exception('Failed');
    }
    usleep(1000);
}
echo 'OK';


Expected result:
----------------
Script run in each browser should write values to the cache and display OK.

Actual result:
--------------
Result is correct only when APC has a lot of free memory available. 
When memory usage is high (near limit set by apc.shm_size) one of the scripts 
very often throws an exception, because apc_store() is unable to write to the 
cache. 

Test case works well with relatively small value of apc.shm_size, but the problem 
remains also on production servers, with 512MB or more shm_size.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-25 18:22 UTC] gopalv@php.net
This is not a bug - this is exactly how APC works.

The memory cleanups are not performed within a request, because it can take upto 15 seconds to cleanup a 2G cache. That is only done at the end of a request (RSHUTDOWN).

So apc_store() in a loop will end up with all entries stuck in the garbage collection list and the memory will fill up.

Once memory fills up, it will return false until at least one GC loop cleans up enough memory.

After a few requests, the cache would be cleaned up properly and apc_store will again start working.
 [2013-08-05 19:52 UTC] krakjoe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: krakjoe
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 09:01:29 2024 UTC