Dale Spiteri
Dale Spiteri

Reputation: 817

Node.JS + Puppeteer: browser.close freezes process

I have a really simple function I'm running.

module.exports.test = async () => {
    const browser = await puppeteer.launch(puppetOptions);
    try {
        const page = await browser.newPage();
        await page.goto('https://google.com');
        // helper function that pauses for five seconds before moving on
        await pause(5000);
        await browser.close();
        console.log('browser closed');
    } catch (err) {
        console.log(err);
        await browser.close();
    }
}

I run it from my index.js file:

const server = app.listen(process.env.PORT || 5000, () => {
    test();    
});

Now I run it in my terminal with node index.js. It opens a browser. It opens a new page and navigates to Google. It waits five seconds. It closes the browser. Everything appears great. I hit ctrl + c in my terminal to stop the process, but nothing happens. Typically this works. If I remove the browser.close function, ctrl + c goes back to working as expected, and ends the process. This function I'm running is the result of me breaking down a more complex function that appears to have a memory leak, so it really seems that browser.close is the culprit. For the life of me though, I can't figure out why it would be causing an issue when simplified this much. This is happening in both headless, and headfull modes. Here are the puppeteer launch options:

puppetOptions = {
    defaultViewport: null,
    args: [
        "--incognito",
        "--no-sandbox",
        "--single-process",
        "--no-zygote"
    ],
}

puppetOptionsHeadfull = {
    headless: false,
    executablePath: 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe',
}

EDIT: I tried this in my bash terminal as well and have the same issue. When I try to manually close the terminal to abort it, an error pops up.

Processes are running in session:
| WPID PID COMMAND
| 10900 1122 winpty node.exe index.js
Close anyway?

EDIT 2: Narrowed this down to it being an issue with puppeteer-extra most likely, however, it seems like a bug in the core package. Fairly recent open issue on their repo that reflects this bug found here: https://github.com/berstend/puppeteer-extra/issues/421

I'll leave this question open just in case anyone else stumbles on it with the same issue, they don't pull their hair out debugging it.

Upvotes: 5

Views: 2740

Answers (2)

Andrew
Andrew

Reputation: 41

It is depends on flag combination. If we need headless mode and --no-sandbox flag, then try this puppeteer launch code:

const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox','--single-process', '--no-zygote'], ignoreHTTPSErrors: true });
  const page = await browser.newPage();
...
await browser.close();

Upvotes: 2

Jake Young
Jake Young

Reputation: 11

I have been having this issue as well. It seems for me that the two arguments '--no-sandbox', and '--disable-setuid-sandbox' are culprits, although it's not intuitive why that would be the case. Try removing the no sandbox argument

Upvotes: 1

Related Questions