Back to Projects

The Tab I Forgot About Was Still Working

Alex Wilson••6 min read•Builder Journal
Nearly closed laptop leaking screen light beside a blinking router at night

Last Saturday morning my egress watchdog sent an email. It's a small job I built in August that snapshots query traffic on my Supabase project every day and stays quiet unless something regresses. It almost never fires. This time it did: roughly 15 requests a second hitting the auth checks on Claudia, my personal assistant app, which runs on a Mac Mini in my office.

Nobody was using Claudia at 15 requests a second. I'm the only user. I was asleep for part of that window.

The culprit was a browser tab.

The second time this year

The annoying part is that I'd seen this shape before. In early September the same watchdog flagged a jump in auth traffic, around 216 MB a day of session reads that had no business existing. That one took a while to find. It turned out to be one stale tab on a Mac at home, left open across a deploy that switched the app from a dev build to a production build. The old page's client code no longer matched the server, and it got stuck validating its session over and over, a dozen times a second, but only while that machine was awake. Closing the tab fixed it. The note I left myself said "hardening candidates: poller backoff."

I didn't do the hardening. Six weeks later, a different tab found a different loop.

What the code was doing

Claudia's chat page listens for new messages over a realtime channel. When a background job finishes, its result shows up in the conversation without a reload. Realtime channels drop, though. Laptops sleep, Wi-Fi changes, the tunnel between my house and the internet hiccups. So the chat feed had a fallback, written months ago:

// When a channel drops, fall back to polling every 10s
pollRef.current = setInterval(() => {
  refetchActiveJobs();
  router.refresh();
}, 10_000);

Read that as the browser would. The channel drops. Every 10 seconds, forever, refetch the running jobs and call router.refresh(). In Next.js that means re-running the whole server render for the page: the 50-row conversation list in the sidebar, the message thread, and the auth check in middleware that every request pays for.

There was no check for whether the tab was visible. There was no backoff. There was no ceiling. The only exit was the channel reconnecting, and a tab that has been asleep in the background for three days doesn't always reconnect cleanly. So a tab I opened once and forgot about kept re-rendering the entire app six times a minute, for as long as the computer stayed on.

When I wrote that fallback it looked responsible. Realtime might fail, so poll. What I hadn't pictured was the tab nobody looks at.

The fix was mostly deletion

The part that made me laugh: the fallback was already redundant. A few weeks earlier I'd added what the code calls safety net 3, for a different bug where a long-open tab's channel would stall while still claiming to be connected. Safety net 3 does the right version of the same job:

  • It only runs while the tab is visible. Hidden tabs don't poll at all.
  • Every 20 seconds it asks one tiny question: what's the id of the newest message in this conversation?
  • It calls router.refresh() only if that id changed.
  • When I come back to the tab, it checks immediately.

That's one indexed query returning one column of one row, instead of a full page render. And when nothing has changed, which is almost every tick, nothing re-renders.

So the fix for the 10-second loop was to delete it. When the channel drops now, the page shows a small "reconnecting" banner and lets safety net 3 handle catching up. The whole fallback collapsed to two lines:

const startPolling = (): void => setRealtimeDown(true);
const stopPolling = (): void => setRealtimeDown(false);

I kept the function names, which are now lies. That's on my list.

Naming the next one

The honest part of this story is that the 10-second loop doesn't fully explain 15 requests a second. One tab refreshing every 10 seconds is a fraction of that, even counting the extra requests a refresh fans out into. The alert also said most of the traffic wasn't page loads. Maybe there were several tabs on several devices. Maybe something else was also looping. I couldn't tell, and that was the real problem.

Both incidents had the same blind spot. The watchdog could tell me something was hammering auth. It couldn't tell me which page, which kind of request, or which device. My tunnel doesn't log paths. Both times I found the answer by guessing and closing tabs.

So the second half of the change is a small request tally in the app's middleware. Every request gets counted under a short key: method, path, what kind of Next.js request it is (a full document, an RSC fetch, a prefetch, a server action), and a rough device label like "Mac/Safari" or "Android/Chrome." At the end of each minute, if the total went over 300 (5 a second, where my normal use is well under 1), it writes the top eight keys to the web log. Otherwise it says nothing and throws the counts away.

It's about 50 lines, in memory, no database, no dashboard. A restart resets it. That's fine. Its whole job is to make the next runaway tab introduce itself by name instead of making me play detective.

It has been running for five days. The log has zero lines from it. I'm treating that as the good outcome, not proof it doesn't work.

What I'm taking from it

The bug wasn't the polling. Polling is fine. The bug was a loop with no conditions on it: not visible, not changed, not bounded. Every one of those was cheap to check and the code checked none of them. The version that already existed in the same file checked all three.

The other lesson is about what to monitor. An alert that says "something is wrong" got me halfway twice. An alert that says "GET /app rsc Mac/Safari, 840 times this minute" would have gotten me there in one look. Cheap attribution, logged only when it matters, beats a better threshold.

And when I write myself a note that says "hardening candidates," I'm going to treat it as a task instead of a souvenir.

Share: