Pagedjs cli memory leak
Hello, after some extensive testing I finally came across to the origin of our deployment issues (Editoria's stack) where pagedjs service https://gitlab.coko.foundation/cokoapps/pagedjs as well as other services deployed in the same server, after some time become unavailable and crash.
The aforementioned service is just a REST wrapper around pagedjs-cli (v.0.1.6) and you can find more info in its Dockerfile.
So the problem that I am facing is that for unknown reason, with fixed css and fixed content sometimes I am getting the following timeout error when calling pagedjs-cli e.g. node /home/node/pagedjs/node_modules/.bin/pagedjs-cli -i temp/b87d01c8f883638c2d4da38f69b078fd/index.html -o temp/b87d01c8f883638c2d4da38f69b078fd/output.pdf
2022-06-28 13:20:18
(node:1) UnhandledPromiseRejectionWarning: Error: Error: Command failed: /home/node/pagedjs/node_modules/.bin/pagedjs-cli -i temp/1cf84a9b7dcc79f879a06bb12941b8ce/index.html -o temp/1cf84a9b7dcc79f879a06bb12941b8ce/output.pdf
2022-06-28 13:20:18
- Loading: temp/1cf84a9b7dcc79f879a06bb12941b8ce/index.html
2022-06-28 13:20:18
✔ Loaded
2022-06-28 13:20:18
- Rendering: Page 1
2022-06-28 13:20:18
✔ Rendering 51 pages took 1257.800000011921 milliseconds.
2022-06-28 13:20:18
- Generating
2022-06-28 13:20:18
TimeoutError: waiting for Page.printToPDF failed: timeout 30000ms exceeded
2022-06-28 13:20:18
at Object.waitWithTimeout (/home/node/pagedjs/node_modules/puppeteer/lib/cjs/puppeteer/common/helper.js:224:26)
2022-06-28 13:20:18
at Page.createPDFStream (/home/node/pagedjs/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:2045:49)
2022-06-28 13:20:18
at Page.pdf (/home/node/pagedjs/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:2057:37)
2022-06-28 13:20:18
at Printer.pdf (/home/node/pagedjs/node_modules/pagedjs-cli/src/printer.js:335:26)
2022-06-28 13:20:18
at processTicksAndRejections (internal/process/task_queues.js:97:5)
2022-06-28 13:20:18
at async /home/node/pagedjs/node_modules/pagedjs-cli/bin/paged:168:14
2022-06-28 13:20:18
at conversionHandler (/home/node/pagedjs/server/api/pagedjs.controller.js:131:11)
2022-06-28 13:20:18
at runMicrotasks (<anonymous>)
2022-06-28 13:20:18
at processTicksAndRejections (internal/process/task_queues.js:97:5)
2022-06-28 13:20:18
(node:1) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
2022-06-28 13:20:18
(node:1) [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.
could these https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagepdfoptions, https://stackoverflow.com/questions/69436420/how-to-increase-timeout-for-pupetteers-page-pdf-when-getting-timeouterror-wai be useful?
Also, another error that I am getting sometimes (again the situation is the same as above, fixed content and css)
2022-06-30 18:24:41
(node:1) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 4)
2022-06-30 18:24:41
at processTicksAndRejections (internal/process/task_queues.js:97:5)
2022-06-30 18:24:41
at runMicrotasks (<anonymous>)
2022-06-30 18:24:41
at conversionHandler (/home/node/pagedjs/server/api/pagedjs.controller.js:131:11)
2022-06-30 18:24:41
- Loading: temp/9bcce8363bb1b0184d1b7fd2bbfb5412/index.html
2022-06-30 18:24:41
(node:1) UnhandledPromiseRejectionWarning: Error: Error: Command failed: /home/node/pagedjs/node_modules/.bin/pagedjs-cli -i temp/9bcce8363bb1b0184d1b7fd2bbfb5412/index.html -o temp/9bcce8363bb1b0184d1b7fd2bbfb5412/output.pdf
2022-06-30 18:24:04
(node:1) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
2022-06-30 18:24:04
at processTicksAndRejections (internal/process/task_queues.js:97:5)
2022-06-30 18:24:04
at runMicrotasks (<anonymous>)
2022-06-30 18:24:04
at conversionHandler (/home/node/pagedjs/server/api/pagedjs.controller.js:131:11)
2022-06-30 18:24:04
- Loading: temp/95bf0847b91ff37f0074d3220f84fa93/index.html
2022-06-30 18:24:04
(node:1) UnhandledPromiseRejectionWarning: Error: Error: Command failed: /home/node/pagedjs/node_modules/.bin/pagedjs-cli -i temp/95bf0847b91ff37f0074d3220f84fa93/index.html -o temp/95bf0847b91ff37f0074d3220f84fa93/output.pdf
The most important is that when any of the above happens nothing is cleaned from memory as presented below.
This leads to memory leak as these processes add up every time this error occur (which happens a lot), resulting to resource starvation on the server.
Is any browser.close()
missing inside a catch clause or am I far off?