$ cat post/debugging-the-"weird-error"-that-wouldn't-go-away.md

Debugging the "Weird Error" That Wouldn't Go Away


May 2, 2005. I remember it like yesterday—or maybe it was just last week. I was working late at my startup, trying to debug a pesky issue that had been bugging me for days.

The Setup

At the time, we were running a small web application using a LAMP stack (Linux, Apache, MySQL, and PHP). It was nothing fancy, but it needed to handle spikes in traffic during our launch. We were also using Xen for some of our server virtualization needs, which felt like a step up from just plain old Linux.

The Issue

One day, I noticed something weird: every so often, users would report that the site would hang or become unresponsive. It was intermittent and hard to reproduce, making it even more frustrating. I checked logs, poked around in Apache’s error files, and even dug through MySQL’s slow query log, but nothing seemed out of place.

Then one evening, as I was sipping on a cup of Earl Grey tea (a rare treat), I stumbled across an obscure entry in the PHP error log that read:

PHP Notice: Undefined variable: someVariable

It was so mundane; it didn’t even look like an issue. But something about it felt… off. I decided to investigate further.

The Digression

I started by looking at our codebase, which had grown a bit messy over time with contributions from many developers who were no longer on the team. It wasn’t hard to find where someVariable was supposed to be set. But what caught my eye was this snippet:

if ($user->isPremium()) {
    $someVariable = 'Hello, user';
} else {
    // This block was empty.
}

It dawned on me that the problem could be in how we handled else blocks. If a function like isPremium() ever returned false for all users at once (which it never did), $someVariable would remain undefined. And here’s where I made a rookie mistake: I had assumed that any variable declared inside an if block wouldn’t affect the global scope if there was no corresponding else block.

The Fix

To fix this, I simply added a default value to the else statement:

if ($user->isPremium()) {
    $someVariable = 'Hello, user';
} else {
    $someVariable = '';
}

This ensured that $someVariable was always defined. With the code change, I re-deployed our app and monitored it for a few days. The issue didn’t reappear.

Reflection

This experience taught me two valuable lessons:

  1. Trust But Verify: Even when something seems straightforward or insignificant, always double-check your assumptions.
  2. Document Everything: In large codebases with multiple contributors, documenting edge cases can save you and others countless hours of debugging.

I also realized that as we grew, the role of a sysadmin was shifting towards more scripting and automation—something I found myself getting more involved in. But for now, it was back to the trenches, fixing bugs and making sure our little web app ran smoothly until the next big problem showed up.


That’s my story from May 2005. Hope it gives you a glimpse into what it was like to debug in those days!