7

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?

akira
  • 6,050
  • 29
  • 37
Daniel MesSer
  • 1,191
  • 1
  • 7
  • 13
  • 1
    Same here. It seems that apc_exists + 1 second timeout keys = deadlock in some random cases. I guess it's a bug? – Mahn Jun 01 '12 at 21:26
  • Well, i actually had a 300s key, so it's not related to the time – Daniel MesSer Jun 05 '12 at 13:31
  • Yeah, that was an early assumption, but I think the answer below still applies; as long as the key has an expiration date and it has not expired yet, internal duplicates can happen – Mahn Jun 05 '12 at 13:50

2 Answers2

6

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)

Community
  • 1
  • 1
Mahn
  • 16,261
  • 16
  • 62
  • 78
  • 1
    That's good advice and all, but I'm not sure it answers the question at hand -- the sample code above is all straight-line; there's nothing in there that should cause a hang. –  Jun 03 '12 at 17:38
  • 1
    Correct, the point is, there is nothing wrong with OP's code, what he is experiencing is just how APC works. – Mahn Jun 03 '12 at 19:17
  • 2
    Crashing the interpreter is "just how APC works"? I think not. I'm pretty sure this is a bug in APC, and should be reported accordingly. –  Jun 03 '12 at 21:57
  • Judging by the article linked in my answer above, it looks like the author of the extension acknowledges these flaws and doesn't plan to fix them, so, well, if you can persuade him to fix this behaviour go ahead, but I wouldn't count on that happening. – Mahn Jun 03 '12 at 23:31
  • 1
    Thanks for the clarification and feedback, it seems like this is a general issue and not related to wamp. I will thusly report this as a bug to apc and see what happens. – Daniel MesSer Jun 04 '12 at 05:08
1

I believe it's not mentioned explicitly but the deadlocks occur only with the apc_exists function. apc_fetch doesn't seem to suffer any deadlocks. I've found that changing apc_store for apc_add has no effect on deadlocks, they occur for both functions.

An exists check using apc_fetch could look like:

public function has($key) {
    apc_fetch($key, $exists);
    return $exists;
}
Halcyon
  • 57,230
  • 10
  • 89
  • 128