puppeteer: [Bug]: Browser.close does not shutdown the process immediately
Error string
MaxListenersExceededWarning: Possible EventEmitter memory leak detected.
Bug behavior
- Flaky
Background
The code example above is a simplified version of our Lambda function. Basically. we don’t do anything tricky, only load the page, giving it reasonable time to load while monitoring the ready state of the page and its source code, and get the data from it.
The crawler works great except sometimes the browser freezes. In most cases, we catch this when closing the browser. We scrape hundreds of thousands of pages and weren’t able to make it work reliably reusing the same browser instance, even with periodical restarts. Therefore, we close and open the browser on each crawl.
At some point, it freezes on the browser.close() so we had to implement a timeout function and try to kill the process, which doesn’t work well. Even after sending the SIGKILL signal, the process stays. Probably in a zombie state or just stucks somewhere, I don’t know and we have no instruments to investigate. Must be an upstream bug in the chrome itself.
Expectation
There should be a way to get rid of a faulty process. Disregarding the nature of the failure, we should be able to kill it and start a new one.
Reality
The fact we’re sending SIGKILL and it brings us nowhere tells me the child process is spawned incorrectly and wasn’t properly detached from its parent. When something goes wrong, such processes stay as long as their parent, which can take forever as we have no control over Lambda’s lifecycle (almost). The only solution I can think of is to call exit in the main function, which will forcefully restart the Lambda. Then we handle a faulty response and restart the job.
We started seeing this problem after upgrading to 112+. We are on 122 now but the picture is the same.
Puppeteer configuration file (if used)
No response
Puppeteer version
22.4.1
Node version
20.11.1
Package manager
npm
Package manager version
10.2.4
Operating system
Linux
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 19
So I tried
dumpio: truebut did not find anything suspicious there. Some GPU-related complaints when thegraphicsModeis set tofalse. When set totrue, those complaints are gone but that doesn’t change the main picture.A typical log would look like this:
When calling
browser.close()this linepuppeteer:browsers:launcher Browser process 176 onExitnever appears.In my opinion, the problem is that the page keeps loading. So I set the timeout of 25 seconds which aborts the promise but the page keeps loading, then I close it and something doesn’t go according to the plan.
So instead of closing the page, I did this:
And that worked!
A reproducible script would probably be to load a heavy page that takes too long, preferably in a limited environment with a slow vCPU, set a navigation timeout, and try closing the page and the browser.
Thanks, I see that Browser.close sends a success response but actually does not close the browser.