Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Puppeteer leaves Chrome running after your Express route finishes

ErrorNode process does not exit after the Puppeteer script ends

Short answer

Chrome stays alive when browser.close() is never reached. An error or early return skips it, or you called disconnect(), which leaves the process running. In Docker, orphaned Chrome processes can also linger without an init process. Close the browser in a finally block and run the container with --init.

  • Applies to Puppeteer 24, Node.js 22, Express 5, Docker 27
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Count Chrome processes before and after a request. If the number goes up and does not come back down, the browser is not being closed.

    ps aux | grep -c '[c]hrome'
  2. 2Match what you see, then jump to its fix

    1. 1Processes grow only on failed requests, such as timeouts or navigation errorsclose() is skipped because an error or early return happens firstFix
    2. 2The script ends cleanly but the Chrome process is still listeddisconnect() was used where close() was meantFix
    3. 3Many zombie chrome processes inside the container, as shown by psIn Docker, nothing reaps the leftover Chrome processesFix

What does the error look like?

  • Node process does not exit after the Puppeteer script ends
  • Chrome processes still listed by ps after the request has finished

Cause 1 of 3

close() is skipped because an error or early return happens first

1Check

Look for browser.close() placed at the end of the try block, or after a return. The call is asynchronous (it returns a Promise), and any throw before it means it never runs.

grep -n -B3 -A3 "browser.close" src/

2Fix

Create the browser first, then put close() in a finally block, which runs after a return or a throw.

app.get('/pdf', async (req, res, next) => {
  const browser = await puppeteer.launch();
  try {
    const page = await browser.newPage();
    await page.goto(req.query.url);
    res.send(await page.pdf());
  } catch (err) {
    next(err);
  } finally {
    await browser.close();
  }
});

3Verify

Send one failing request (a bad URL) and one good one, then count the processes again.

ps aux | grep -c '[c]hrome'

Confirmed by pptr.dev

Did this fix it?

Cause 2 of 3

disconnect() was used where close() was meant

1Check

disconnect() only cuts Puppeteer's connection and leaves the browser process running. close() shuts the browser and all its pages.

grep -n "disconnect" src/

2Fix

Use close() to end a browser you launched. Keep disconnect() for a browser you connected to and want to leave running.

// launched here, so close it here
await browser.close();

3Verify

Run the script once and confirm no Chrome process is left.

pgrep -fl chrome || echo none

Confirmed by pptr.dev

Did this fix it?

Cause 3 of 3

In Docker, nothing reaps the leftover Chrome processes

1Check

Puppeteer's troubleshooting guide says that when a container runs your app as PID 1, Chrome can be hard to terminate properly and zombies pile up.

docker exec my-container ps aux | grep -c defunct

2Fix

Start the container with an init process, either docker run --init or dumb-init in the image.

docker run --init --rm my-image

3Verify

Load-test the endpoint and count defunct processes again.

docker exec my-container ps aux | grep -c defunct

Confirmed by pptr.dev

Did this fix it?

How do I stop it coming back?

  • Wrap every launch in try/finally, so the close call cannot be skipped.
  • Reuse one browser for many requests and open and close pages, if launch time matters.
  • Watch process count or memory in your monitoring, and alert when it keeps rising.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.