After much digging around I haven't been able to identify the issue behind a race condition I'm finding on a little PHP pseudo-cron.
The code looks like this:
fh = fopen(ROOT . '.cron.lock', 'w+');
if (flock($fh, LOCK_EX|LOCK_NB)) {
//Cron logic goes here
flock($fh, LOCK_UN);
}
It should be pretty straightforward, and usually does work. The point is that every so often, this little cron executes twice (sending a duplicate email to a user), which is rather annoying.
Initially I thought I'd have to use the third $ewouldblock
parameter. But this has turned out not to work, just causing the cron to execute always without regard for any other process.
Whenever I test this code in a CLI environment, it works perfectly fine. But it won't as soon as I move over to using the script inside a HTTPD (Apache) request.
http://php.net/manual/en/function.flock.php
If anyone can help, or maybe give me a pointer what the whole $ewouldblock
parameter is about (since the documentation is not very straightforward about it) I would appreciate a lot.