News Archive
PhpRiot Newsletter
Your Email Address:

More information

My Infinite Loop

Note: This article was originally published at Planet PHP on 18 September 2012.
Planet PHP

I had a small spot of panic earlier this week when a customer reported that their account page on WonderProxy was timing out. The panic intensified when I was readily able to replicate the issue: the script was timing out, and completely failing to display any account information.

I've worked on many poorly performing pages in the past, often to great success: that's not the source of the panic. My real concern was that there was no reason for the page to take that long to generate. We switched over to generating the account information automatically every hour (after our billing updates) to ensure that those pages would build fast. That was unfortunately a large chunk of code I didn't want to try to debug quickly.

Fortunately the problem was much more benign: a small infinite loop!
We've got a small block of code that determines when a user's billing period starts and ends. It accepts a start date (that could be some number of months back), calculates an end date, and keeps adding a month until *now* occurs between the start and end dates.

$start = strtotime($startDate); $end = strtotime("+1 month", $start); $now = time(); while (!($start $now)) { $start = strtotime("+1 month", $start); $end = strtotime("+1 month", $start); }

When the account in question was configured (tragically, by me) the start date was set in the future. So my loop obligingly kept looking forever.

A quick database tweak later and the user's account was fixed, and I had a few sanity checks to add.