跪拜 Guibai
← Back to the summary

The Loneliest Bug Fix: When No One Knows It Was Ever Broken

Some bugs, when fixed too cleanly, leave no trace they ever existed. This is perhaps the loneliest romance of a programmer.

1. The Origin: A Module That "Looked Fine"

The company had a core order module whose code history dated back to 2018. Over five years, eight different teams had cycled through the frontend and backend, and the comments still contained the original architect's QQ number.

This module had one defining characteristic: it looked perfectly normal.

But experienced programmers know that "looking normal" is often the most dangerous signal.

When I took over, the code was filled with this kind of "artistry":

// Don't delete this, it will crash, reason unknown
setTimeout(() => {
  if (window.__magic_flag) {
    // 2021.3.15 Temporary fix, to be verified
    fixSomething();
  }
}, 1000);

What was __magic_flag? Unknown. Who added it? Git blame pointed to a deactivated account. What did fixSomething do? The comment said "to be verified"—it had been waiting for three years.

2. I Decided to "Excavate"

Over a weekend, I spent two days using Chrome DevTools' Performance panel and manual breakpoints to finally reconstruct this module's "hidden history":

Layer 1: Timer Hell

That setTimeout was meant to solve a race condition. In a 2019 version, a backend API response slowed down, so the frontend added a 1-second delay as a "safety net." Later, the API was fixed, but the timer was never removed; instead, subsequent code grew dependent on it.

Layer 2: Global Variable Plague

__magic_flag was added by another colleague to mark "whether it had already been fixed." The problem: if a user switched pages quickly, the flag hadn't reset yet, and the fix logic was skipped.

Layer 3: A Silent Disaster

The most terrifying part was that this bug almost never triggered. It only appeared under a superposition of network jitter, extremely fast user actions, and a specific browser version. Over five years, it probably occurred about 12 times online, each time dismissed by customer service with "just refresh the page."

Nobody reported a bug because refreshing actually fixed it for the user. Testing never caught it because automated tests don't "switch pages rapidly."

This module was like a landmine buried for five years, with a carpet laid over it.

3. The Fix: Not a Patch, but Bomb Disposal

I didn't choose the ancestral craft of "adding one more if-statement." Instead, I did three things:

1. Replaced the Timer with a Promise Chain

// Before
setTimeout(() => {
  if (window.__magic_flag) fixSomething();
}, 1000);

// After
await ensureOrderDataReady();
// An explicit data-ready signal, no more magic numbers

2. Introduced a State Machine to Manage the Page Lifecycle

type OrderPageState = 'idle' | 'loading' | 'ready' | 'error';

const [pageState, setPageState] = useState<OrderPageState>('idle');

The global variable __magic_flag was completely removed. When the page switches, the state resets automatically; no "dirty state" residue remains.

3. Added 47 Unit Tests

Covering all edge conditions: network timeouts, rapid switching, abnormal API response structures, user disconnection and reconnection...

Using @testing-library/react, I simulated a user "frantically clicking" to ensure the state machine wouldn't crash.

4. The Most Absurd Outcome

On Monday, I submitted the PR, and the code review went smoothly. A senior dev glanced at the diff and said, "Just a refactor? No new features?"

I said, "Fixed a few hidden bugs."

He asked, "Is there a ticket?"

I said, "No, I just looked into it over the weekend."

He nodded and merged it.

Two weeks later, something even more surreal happened:

A young tester from the QA team came over and asked me, "Hey, could you share the test cases for that order module? Our group is compiling best practices recently and found your module to be exceptionally stable—no online issues in three years. We want to learn from it."

I paused, then said, "Oh... that one, yeah, it's pretty stable."

She nodded earnestly, "Yeah, our lead said this kind of 'innately stable' module is the most valuable reference, unlike some modules that get bugs fixed every day."

I opened my mouth but ultimately didn't tell her the truth.

5. The Programmer's "Bug-Fixing Paradox"

This incident made me realize something:

In software engineering, a bug's visibility is often inversely proportional to its danger.

And when you truly fix the latter, no one will thank you—because they never knew there was a problem in the first place.

The QA team thought this module "was never broken." The boss thought I just "optimized the code structure a bit." Even I myself, three months later, if I didn't check the Git history, would assume this module was born this clean.

6. The Only Evidence

The only thing that knows this secret is that commit message in Git:

refactor(order): remove magic timer and global state flag

- Replace setTimeout polling with Promise
- Introduce OrderPageState state machine
- Add 47 boundary condition tests
- Remove __magic_flag (legacy from 2019)

BREAKING CHANGE: None, behavior remains consistent

And the 3,000-word "excavation notes" on my local machine, chronicling the past and present of __magic_flag.

7. Final Thoughts

Some say a programmer's best work is "code nobody notices"—it runs stably, throws no errors, lags never, and needs no explanation.

But I'd say, a programmer's loneliest work is a fix that "nobody knows was ever broken."

You spent two weekends dismantling a five-year landmine and laying a truly solid foundation. Then you watch the QA team document it as a "natively excellent case study" in their best practices.

Does this count as success?

I guess so. It's just a little lonely.


If you've ever dismantled a landmine like this, feel free to leave your story in the comments. At least here, someone understands.

👉Telegram self-service, adding 86 is also fine👈

Comments

Top 2 from juejin.cn, machine-translated. The original thread is authoritative.

Fantastic_23

If nothing goes wrong, there's no credit; if something goes wrong, you're the first one held responsible.

9527打工仔

If this piece of business code has no new requirements, it's best not to touch it.