question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NodeJs 12: UnhandledPromiseRejectionWarning.

See original GitHub issue

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 2.0.0
  • Platform / OS version: macOs 10.15.2
  • URLs (if applicable):
  • Node.js version: 12.4

What steps will reproduce the problem?

Code:

const puppeteer = require('puppeteer');

const main = async () => {
    const functionWithPagesInvocation = async (browser) => {
        const pages = await browser.pages();
        if (pages.length > 1) return pages[0].bringToFront();
    };
    const pool = [];
    for (let i = 0; i <= 5; i++) {
        const browser = await puppeteer.launch({ headless: true });
        pool.push(browser);

        await browser.newPage();
        functionWithPagesInvocation(browser).catch(e => console.error('Should catch error', e));
    }

    await Promise.all(pool.map(browser => browser.close()));
};

main().catch(e => console.error('Should atleast catch here', e));

Logs:

Should catch error Error: WebSocket is not open: readyState 2 (CLOSING)
    at WebSocket.send (/Users/petr/apify/puppeteer-testing/node_modules/ws/lib/websocket.js:329:19)
    at WebSocketTransport.send (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/WebSocketTransport.js:60:14)
    at Connection._rawSend (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Connection.js:86:21)
    at CDPSession.send (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Connection.js:181:33)
    at Page._initialize (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Page.js:137:20)
    at Function.create (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Page.js:48:16)
    at /Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Target.js:74:32
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Promise.all (index 0)
    at async BrowserContext.pages (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Browser.js:311:19)
  -- ASYNC --
    at Target.<anonymous> (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/helper.js:111:15)
    at /Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Browser.js:314:35
    at Array.map (<anonymous>)
    at BrowserContext.pages (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Browser.js:314:14)
    at BrowserContext.<anonymous> (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/helper.js:112:23)
    at /Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Browser.js:233:90
    at Array.map (<anonymous>)
    at Browser.pages (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Browser.js:233:67)
    at Browser.<anonymous> (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/helper.js:112:23)
    at functionWithPagesInvocation (/Users/petr/apify/puppeteer-testing/main.js:5:37)
  -- ASYNC --
    at BrowserContext.<anonymous> (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/helper.js:111:15)
    at /Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Browser.js:233:90
    at Array.map (<anonymous>)
    at Browser.pages (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Browser.js:233:67)
    at Browser.<anonymous> (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/helper.js:112:23)
    at functionWithPagesInvocation (/Users/petr/apify/puppeteer-testing/main.js:5:37)
    at main (/Users/petr/apify/puppeteer-testing/main.js:14:9)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:29769) UnhandledPromiseRejectionWarning: Error: Protocol error (Page.enable): Target closed.
    at /Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Connection.js:183:56
    at new Promise (<anonymous>)
    at CDPSession.send (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Connection.js:182:12)
    at FrameManager.initialize (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/FrameManager.js:60:20)
    at Page._initialize (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Page.js:136:26)
    at Function.create (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Page.js:48:16)
    at /Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Target.js:74:32
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Promise.all (index 0)
    at async BrowserContext.pages (/Users/petr/apify/puppeteer-testing/node_modules/puppeteer/lib/Browser.js:311:19)
(node:29769) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:29769) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

What is the expected result? I would expect the exception to be handled.

What happens instead? I guess that due to the promise handling changes in node v11+ some promise inside puppeteer is rejected earlier than the error handler is attached, so the UnhandledPromiseRejectionWarning appears.

NOTE: It does not happen in NodeJs 10

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
stale[bot]commented, Jun 27, 2022

We’re marking this issue as unconfirmed because it has not had recent activity and we weren’t able to confirm it yet. It will be closed if no further activity occurs within the next 30 days.

0reactions
stale[bot]commented, Jul 27, 2022

We are closing this issue. If the issue still persists in the latest version of Puppeteer, please reopen the issue and update the description. We will try our best to accomodate it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting a UnhandledPromiseRejectionWarning when ...
On the console, I am getting an 'UnhandledPromiseRejectionWarning' despite using the try-catch block. (node:8248) ...
Read more >
Unhandled Promise Rejections in Node.js
Node.js 6.6.0 added a sporadically useful bug/feature: logging unhandled promise rejections to the console by default.
Read more >
Node.js 15 release: Updated handling of rejections, npm 7, ...
To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.
Read more >
Node.js 15 Is Out! What Does It Mean for You? - Maxim Orlov
(node:1309) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a ...
Read more >
Tips for Upgrading Node 12 LTS Quickly and Painlessly
Node 12 LTS Reaching End of Life - How to Upgrade Painlessly ... If you see UnhandledPromiseRejectionWarning messages in your logs, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found