puppeteer-cluster icon indicating copy to clipboard operation
puppeteer-cluster copied to clipboard

Bug: No way to handle `UnhandledPromiseRejectionWarning` inside `cluster.work`

Open mhassan1 opened this issue 2 years ago • 5 comments

When the Promise returned by doWork here is rejected, we end up with an UnhandledPromiseRejectionWarning, which we can only deal with using a global process.on('unhandledRejection', ...) listener. There should be a better way for consumers to handle this error; maybe cluster should emit a clustererror event (or an error event, since it may be fatal), in this case.

I would be happy to contribute a fix, once we have an approach.

To reproduce:

const { Cluster } = require('puppeteer-cluster')

Cluster.launch({
  puppeteerOptions: {
    args: [
      '--single-process'
    ]
  },
}).then(cluster => {
  cluster.queue(async () => {})
})

NOTE: This reproduces on my Mac because the --single-process flag prevents Chromium from starting up. YMMV.

Actual output: An UnhandledPromiseRejectionWarning is emitted by the process, and the process exits:

Error: Unable to get browser page
    at Worker.<anonymous> (/puppeteer-cluster/node_modules/puppeteer-cluster/dist/Worker.js:41:31)
    at Generator.next (<anonymous>)
    at fulfilled (/puppeteer-cluster/node_modules/puppeteer-cluster/dist/Worker.js:5:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.12.1

Process finished with exit code 1

Expected output: An event is emitted by cluster, and no UnhandledPromiseRejectionWarning is emitted by the process

Related: https://github.com/thomasdondorf/puppeteer-cluster/issues/323

mhassan1 avatar Dec 09 '22 21:12 mhassan1

im also facing the same issue.

drizzef avatar Feb 06 '23 16:02 drizzef

I can confirm the symptom. The roor cause seems like at https://github.com/thomasdondorf/puppeteer-cluster/blob/b5b098aed84b8d2c170b3f9d0ac050f53582df45/src/Cluster.ts#L238, the SetTimeout made Cluster.doWork run in the event loop scheduled as macro tasks. Therefore, any error rejects Cluster.doWork does not populate to Cluster.work and onward, which leads to the unhandeld rejection warning.

zhaow-de avatar Apr 21 '23 10:04 zhaow-de

me too

yangguangwuwu avatar Oct 30 '23 00:10 yangguangwuwu

throw new Error('Unable to get browser page'); ^

Error: Unable to get browser page at Worker. (/data/node_modules/puppeteer-cluster/dist/Worker.js:41:31) at Generator.next () at fulfilled (/data/node_modules/puppeteer-cluster/dist/Worker.js:5:58) at processTicksAndRejections (node:internal/process/task_queues:96:5)

yangguangwuwu avatar Oct 30 '23 00:10 yangguangwuwu

delete --single-process
run ok It is possible that puppeteer-cluster relies on multiple processes of browser instances.

yangguangwuwu avatar Oct 30 '23 00:10 yangguangwuwu