Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deadlock with apc_exists & apc_add? (apc & PHP)

Tags:

php

apc

I wonder if anyone has found any weird behaviours with apc_exists(), behaviour that causes the entire WAMP server to hang when using it together with apc_add() or apc_store()? After a long session of "debugging" and minimizing the problem I ended up with the following code that causes my WAMP to crash.

As far as I can tell it requires 1 apc_exists() and 2 apc_add() accessing different keys. [thus it sounds like a deadlock-issue] I run this script in chrome and then smashes F5-key until I get the rand-thingy to happen twice. At that time or the first time it usually hangs.

<?php
$result = "asdfioasdjfoasdjf";
if(apc_exists("asdf")) {
    echo("#1<br/>");
    apc_add("launcher", $result, 1);
} else {
    echo("#2<br/>");
    $result = "asdfasdfasdf";
    apc_add("launcher", $result, 10);
}
if(rand(0,100) < 4) {
    echo("#stored data!<br/>");
    apc_add("asdf", "2130130", 1);
}
?>

My system/setup:
Windows 7 64bit
WAMP 2.2d 32bit
PHP Version 5.3.10
apc version 3.1.9 | $Revision: 325040 $

Am I doing something wrong in the code? Is this related to windows / wamp or does it exist in other environments and php/apc-versions? In the above case, if I replace apc_exists() with apc_fetch(), the system doesn't crash, does anyone know why?

like image 255
Daniel MesSer Avatar asked May 08 '12 07:05

Daniel MesSer


1 Answers

I believe I found the cause. It all comes down to this well put answer here on SO:

First, it's important to know that if one tries to store a key which already exists with a specific TTL, AND the ttl hasn't passed yet, a new entry will be created; it doesn't matter if the key is the same, internally there will be two entries of the same key.

Second, APC can fail (miss). Even when there's no apparent reason for it. Why? APC was apparently created favoring speed over consistency, meaning that when the APC driver is busy doing some cleanout, it will simply return NULL instead of waiting until it's done, even though the data exists. The long version of this is here: http://phpadvent.org/2010/share-and-enjoy-by-gopal-vijayaraghavan

So what's going on in the specific case mentioned in the question? The time between each request is shorter than 1 second, the specified TTL of the keys, so duplicates can happen if you attempt to store the key here. "But, it's using apc_add, shouldn't that guarantee the key is stored only if it doesn't already exist?" Apparently not :) this is what causes the deadlock to be random: sometimes apc_add would work as you'd expect, some others it "misses", that is, apc_add fails to understand there is another existing key even if it is there. This is probably not a problem if the TTL=0, because in such case the key is simply overwritten, but in the specific case of the question, it will store a duplicate as a result of erroneously failing to find the key AND the key having a TTL that hasn't passed yet.

Since now there are two entries with the same key internally, when apc_exists is used, it gets confused and hangs.

Takeaways: don't store flags on APC, and always have a fallback case prepared in case it "misses". APC seems to work best when used only to store copies of stuff that exists elsewhere (ie a file or a database entry)

like image 133
Mahn Avatar answered Sep 29 '22 13:09

Mahn