DEV Community

Cover image for My Fix for One Chrome Bug Broke Every Export on AWS Lambda. My Laptop Said It Was Fine.
Femi Akinyemi
Femi Akinyemi

Posted on

My Fix for One Chrome Bug Broke Every Export on AWS Lambda. My Laptop Said It Was Fine.

I'm building Tilzix. You paste a website, and it makes carousels and short videos in that brand's style. Each slide is a small web page. To make a video, headless Chrome (Chrome with no screen) runs on AWS Lambda. It opens the slide, takes 30 screenshots a second, and sends them to ffmpeg. Three tabs work at the same time, so a deck of eight slides doesn't take long.

This is about a fix I shipped on a Monday. It quietly stopped every export from finishing until Wednesday. My laptop said everything was fine the whole time.

Bug one: the tab that got smaller

On 22 Nov a video came out wrong. The first frames of one slide showed only its top-left corner, on black.

All three tabs were in one browser window, so only one tab was in front. Chrome treats the other tabs as "in the background". By the time recording started, one of them had shrunk to Chrome's own window size, 756×469. The slide is much bigger than that. I take screenshots with captureBeyondViewport: false, so the screenshot showed only what fit: the corner.

I fixed it in three ways:

  1. Chrome flags so background tabs aren't slowed down or half drawn:
   "--disable-backgrounding-occluded-windows",
   "--disable-renderer-backgrounding",
   "--disable-background-timer-throttling",
Enter fullscreen mode Exit fullscreen mode
  1. A size check just before recording. If the tab is the wrong size, set it again:
   for (let tries = 0; tries < 4; tries++) {
     const size = await page.evaluate(() => `${innerWidth}x${innerHeight}`);
     if (size === `${w}x${h}`) break;
     await page.setViewport(view);
   }
Enter fullscreen mode Exit fullscreen mode
  1. Each tab in its own browser context (think of it as a separate incognito window), so no tab is ever in the background:
   for (let k = 0; k < LANES; k++) {
     const page = k === 0
       ? await browser.newPage()
       : await (await browser.createBrowserContext()).newPage();
     await page.setViewport(view);
     pages.push(page);
   }
Enter fullscreen mode Exit fullscreen mode

I tested it on my laptop. The videos were perfect. I shipped it on 23 Nov.

Bug two: nothing finished

Two days later I was testing something else: posting a video straight to TikTok. The app said: "The export stopped partway." I tried again. Same thing. And again.

The Lambda logs showed this:

ERROR [export] TargetCloseError: Protocol error (Target.createTarget): Target closed
Enter fullscreen mode Exit fullscreen mode

I went back through the logs. Not one export had finished since the 23 Nov deploy. Nothing warned me. The rest of the site looked fine. Even the other Lambda job that uses Chrome, reading a brand's website, kept working, because it only ever opens one tab.

Same error, different cause

This part could have cost me another day. I had seen "Target closed" earlier that same week, on my own Mac. The script I run before every deploy crashed with it.

That time it was memory. My Mac has 16 GB, and 12.5 GB of swap was in use. 4.2 GB of that was test Chromes left open by earlier crashed runs. Each crash left more Chrome running, so the next crash came sooner.

Same words, same week, two causes that have nothing to do with each other. "Target closed" only means Puppeteer lost its connection to Chrome. It doesn't tell you why Chrome went away. Now I read it as "Chrome died, go find out what killed it", not as the answer.

Why it only broke on Lambda

Chrome on Lambda comes from @sparticuz/chromium. It squeezes Chrome into Lambda's small, locked-down space. It starts Chrome with these flags, among others:

--single-process --no-zygote --in-process-gpu --no-sandbox
Enter fullscreen mode Exit fullscreen mode

--single-process is the one that matters. Chrome normally splits its work into many processes. Here, everything runs in one. On my laptop Chrome runs the normal way, so a second browser context is no problem. On Lambda, making one killed Chrome straight away. The first tab opened fine. The second createBrowserContext() took the whole browser down.

I didn't dig into Chrome's source code to find exactly why. What I can show is that it happens every time.

Proving it, not guessing

I didn't want to ship a second guess, so I ran the exact image Lambda runs, on my Mac:

docker run --rm --platform linux/arm64 \
  -e AWS_EXECUTION_ENV=AWS_Lambda_nodejs22.x \
  -e AWS_LAMBDA_FUNCTION_NAME=test \
  --entrypoint node \
  -v "$PWD/test-tabs.mjs":/var/task/test-tabs.mjs:ro \
  my-lambda-image:latest \
  /var/task/test-tabs.mjs contexts
Enter fullscreen mode Exit fullscreen mode

One trap on the way: without AWS_EXECUTION_ENV, the packaged Chrome can't find its libraries and fails with a different error. With it set, the container acts like Lambda.

The test starts Chrome the same way the export does and opens three tabs two ways: once with separate contexts, once with all three in the default one.

contexts  FAILED: Protocol error (Target.createTarget): Target closed
same      OK: 3 tabs open
Enter fullscreen mode Exit fullscreen mode

Same image, same flags, same error as production. That's when I trusted the fix.

The fix

const page = k === 0 || onLambda()
  ? await browser.newPage()
  : await (await browser.createBrowserContext()).newPage();
Enter fullscreen mode Exit fullscreen mode

onLambda() just checks for AWS_LAMBDA_FUNCTION_NAME. On Lambda, all tabs share one context. Chrome there has no windows, so tabs keep their own size anyway. The flags and the size check from the first fix stay on everywhere. If a tab ever does shrink, it gets resized before a single frame is taken.

Exports have finished since, and the videos come out right.

What I learned

My laptop isn't production, even with "the same" Chrome. Chrome on a Mac and Chrome on Lambda are different builds with different flags. If a fix changes how Chrome behaves, test it on the real image. Running it with docker run takes five minutes.

Check the flags your Chrome package starts with. Printing chromium.args is one line. Seeing --single-process would have warned me that opening another context was risky.

An error message tells you what happened, not why. "Target closed" meant memory on my Mac and a Chrome flag on Lambda, in the same week.

Silent failures cost the most. The bug lived for two days, and I only caught it while testing something else. An alert for failed exports is the next thing on my list, so the next one tells me first.

Last week I wrote about a Lambda that was never rebuilt. There, the code was fixed and production wasn't. This one is the opposite: production ran exactly the code I wrote, and the code was the problem. Both times, the answer was in what was really running, not in what I thought was running.

If you want to see what the exports make, Tilzix is free to try. Paste a website and get a carousel or video in about a minute.

What's a bug that only happened in production for you, while your laptop said everything was fine?

Top comments (0)