Beware the favicon feedback loop

The other day I noticed something strange happening on one of my servers. I have munin set up to monitor memory, cpu and disk space usage etc. and I noticed that for some reason memory consumption was going through the roof.

After checking the logs I noticed the following error:

PHP Fatal error: Allowed memory size of 10485760 bytes exhausted
 (tried to allocate 523800 bytes)

Hmm. My server has plenty of memory and PHP’s memory usage is set to 128Mb which should be more than enough to serve a WordPress website.

So I started to debug the website on my localhost. On my dev machine I have XDebug installed (if you develop for PHP you should too!).

When I did some digging and testing I noticed that I was able to reproduce the problem when I removed an image from my uploads directory that was supposed to be loaded on the home page. With XDebug disabled I was able to see in the logs that my PHP script was going into a recursive loop. This recursive loop ended up allocating all the available memory in small chunks until memory was completely exhausted, hence the error above.

After another hour of tracing and poking around I check finally checked my 404 template. In it I was doing something like this:

<?php get_header() ?>
<?php get_footer() ?>

After triggering a manual 404 page by browsing to a non-existing page on my site I did a massive face palm as I suddenly realised my mistake.

On my home page the favicon image could not be loaded which was triggering a 404 on that image being loaded. The 404 was handled by WordPress to display the 404 page, however, in the template I output the website header which has the favicon in it. This in turn would trigger a 404, and so… That’s your recursive loop right there.

After fixing the favicon the website’s behavior has again returned to normal.

This entry was posted in Programming and tagged . Bookmark the permalink. Both comments and trackbacks are currently closed.