Your phone buzzes with the "server alert" ringtone. Simultaneously, your manager taps you on the shoulder: "Um, it looks like we're down." Not a single screen in your web app loads. You attempt to SSH into your server but connecting takes ... for ... ever ... Finally you get on the box and see the CPU is pegged at a 100%. You look at the access logs, but nothing looks fishy. Load is normal. Frantic and grasping for straws, you reboot the server. The server comes back up ... but immediately the CPU goes to 100% again.
An hour later you find the problem. That hour seemed like an eternity as you tore out your hair, fended off account managers, grepped every line of source you could find, and wondered what it feels like to get fired. And what was the error? It was a while loop that processed some unexpected input and spun forever.
Infinite loop bugs are among the most insdious and (outside of wiping out customer data) most destructive bugs that can plauge engineers. In web application development, most bugs only affect the particular code path where the buggy code resides. Most of the time you know what paths are most critical and can pay them the most attention. But a while loop bug in some unimportant, admin-only screen can disable the entire application for everyone. When the problem strikes, it is very difficult to figure out what exactly is causing the app to freak out and where the buggy code lives.
Fortunately, there is a solution to avoid this class of bug completely: never write a while loop.
Anywhere in your code where the need arises for a while loop, instead write a "for-loop" with a sensible maximum.
For example, let us say that I am doing a standard chunked-file read in python. In my younger days I would write:
while True:
chunk = f.read(4000)
if not chunk:
break
output.write(chunk)
A better pattern is:
# needs to support.
EMERGENCY_BRAKE = 10000000
for x in xrange(0, EMERGENCY_BRAKE):
chunk = f.read(4000)
if not chunk:
break
output.write(chunk)
if (x+1) >= EMERGENCY_BRAKE:
raise Exception("File reading loop hit the emergency brake")
In the trivial case of reading a file, the lack of an emergency brake is unlikely to cause a problem. But when you start creating more complicated while loops, such as for tree traversal, streaming parsers, etc., while loops become very dangerous. I therefore highly recommend eliminating them altogether.
(Note - this blog post targets web application programmers. There are of course cases in software development where while loops are still fine. If you are writing the main event loop for a sensor on the next Pioneer space probe, your while loop should keep happily plugging away until the end of the universe).















About me

