Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

puppeteer script randomly hangs #7225

Closed
pcla56 opened this issue May 10, 2021 · 11 comments
Closed

puppeteer script randomly hangs #7225

pcla56 opened this issue May 10, 2021 · 11 comments
Assignees

Comments

@pcla56
Copy link

pcla56 commented May 10, 2021

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 9.1.1
  • Platform / OS version: ubuntu 20.04.2
  • URLs (if applicable):
  • Node.js version: 14.16.1

What steps will reproduce the problem?

When I run "node make_canvas.js arg1 arg2" (from /home/ikadmin) it sometimes works and sometimes hangs.

Please include code that reproduces the issue.

  1. make_canvas.js

// make_canvas.js $ref $canvas_path

var myArgs = process.argv.slice(2);

const minimal_args = [
'--autoplay-policy=user-gesture-required',
'--disable-background-networking',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-breakpad',
'--disable-client-side-phishing-detection',
'--disable-component-update',
'--disable-default-apps',
'--disable-dev-shm-usage',
'--disable-domain-reliability',
'--disable-features=AudioServiceOutOfProcess',
'--disable-hang-monitor',
'--disable-ipc-flooding-protection',
'--disable-notifications',
'--disable-offer-store-unmasked-wallet-cards',
'--disable-popup-blocking',
'--disable-print-preview',
'--disable-prompt-on-repost',
'--disable-renderer-backgrounding',
'--disable-setuid-sandbox',
'--disable-speech-api',
'--disable-sync',
'--hide-scrollbars',
'--ignore-gpu-blacklist',
'--metrics-recording-only',
'--mute-audio',
'--no-default-browser-check',
'--no-first-run',
'--no-pings',
'--no-sandbox',
'--no-zygote',
'--password-store=basic',
'--use-gl=swiftshader',
'--use-mock-keychain',
'--allow-insecure-localhost',
];

const puppeteer = require('puppeteer');
const VIEWPORT = {width: 1024, height: 1500};
const URL = 'http://localhost/make-canvas/'+myArgs[0];

(async () => {
const browser = await puppeteer.launch({args:minimal_args,headless:true,ignoreHTTPSErrors: true,userDataDir:'/home/ikadmin/puppeteer_cache'});
const page = await browser.newPage();
await page.setViewport(VIEWPORT);
//await page.setCookie({name:'GNB_cookies',value:'dismiss',domain:'goodnewsbig.com'});
await page.goto(URL, { waitUntil: 'networkidle0' });
const modal = await page.waitForSelector('#gobig');
await modal.screenshot({ path: myArgs[1], type: 'jpeg' });
await page.close();
await browser.close();
})()

  1. hung processes

ikadmin 3900 3893 0 15:18 ? 00:00:00 /home/ikadmin/node_modules/puppeteer/.local-chromium/linux-869685/chrome-linux/chrome --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=Translate --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --enable-blink-features=IdleDetection --user-data-dir=/home/ikadmin/puppeteer_cache --headless --hide-scrollbars --mute-audio --autoplay-policy=user-gesture-required --disable-background-networking --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-update --disable-default-apps --disable-dev-shm-usage --disable-domain-reliability --disable-features=AudioServiceOutOfProcess --disable-hang-monitor --disable-ipc-flooding-protection --disable-notifications --disable-offer-store-unmasked-wallet-cards --disable-popup-blocking --disable-print-preview --disable-prompt-on-repost --disable-renderer-backgrounding --disable-setuid-sandbox --disable-speech-api --disable-sync --hide-scrollbars --ignore-gpu-blacklist --metrics-recording-only --mute-audio --no-default-browser-check --no-first-run --no-pings --no-sandbox --no-zygote --password-store=basic --use-gl=swiftshader --use-mock-keychain --allow-insecure-localhost --remote-debugging-port=0 about:blank

ikadmin 3915 3900 0 15:18 ? 00:00:00 /home/ikadmin/node_modules/puppeteer/.local-chromium/linux-869685/chrome-linux/chrome --type=gpu-process --field-trial-handle=1442900448426305919,10311597978264912417,131072 --enable-features=NetworkService,NetworkServiceInProcess --disable-features=AudioServiceOutOfProcess,PaintHolding --no-sandbox --disable-dev-shm-usage --disable-breakpad --headless --ozone-platform=headless --headless --gpu-preferences=UAAAAAAAAAAgAAAQAAAAAAAAAAAAAAAAAABgAAAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAgAAAAAAAAACAAAAAAAAAA= --use-gl=swiftshader --override-use-software-gl-for-tests --shared-files

ikadmin 3919 3900 0 15:18 ? 00:00:00 /home/ikadmin/node_modules/puppeteer/.local-chromium/linux-869685/chrome-linux/chrome --type=renderer --no-sandbox --disable-dev-shm-usage --autoplay-policy=user-gesture-required --disable-background-timer-throttling --disable-breakpad --disable-notifications --disable-speech-api --enable-automation --force-color-profile=srgb --no-zygote --remote-debugging-port=0 --allow-pre-commit-input --ozone-platform=headless --field-trial-handle=1442900448426305919,10311597978264912417,131072 --enable-features=NetworkService,NetworkServiceInProcess --disable-features=AudioServiceOutOfProcess,PaintHolding --disable-gpu-compositing --enable-blink-features=IdleDetection --lang=en-GB --headless --lang=en-GB --num-raster-threads=2 --enable-main-frame-before-activation --renderer-client-id=3 --shared-files=v8_context_snapshot_data:100

ikadmin 3920 3900 0 15:18 ? 00:00:00 /home/ikadmin/node_modules/puppeteer/.local-chromium/linux-869685/chrome-linux/chrome --type=renderer --no-sandbox --disable-dev-shm-usage --autoplay-policy=user-gesture-required --disable-background-timer-throttling --disable-breakpad --disable-notifications --disable-speech-api --enable-automation --force-color-profile=srgb --no-zygote --remote-debugging-port=0 --allow-pre-commit-input --ozone-platform=headless --field-trial-handle=1442900448426305919,10311597978264912417,131072 --enable-features=NetworkService,NetworkServiceInProcess --disable-features=AudioServiceOutOfProcess,PaintHolding --disable-gpu-compositing --enable-blink-features=IdleDetection --lang=en-GB --headless --lang=en-GB --num-raster-threads=2 --enable-main-frame-before-activation --renderer-client-id=4 --shared-files=v8_context_snapshot_data:100

  1. All I want to do is display an html element (bootstrap modal) and take a screenshot of it to a file.
  2. I can see the requests in the apache2 logs and everything appears to be ok.

What is the expected result?
I get screenshot of the modal written to my file, and the process completes and tidies everything up.
I am using some recommendations I found (minimal_args, and userDataDir) to try and make the screenshot as efficient as possible. When the script runs ok I get my file fine.

What happens instead?
If it hangs, nothing. I have tried to read up how to trap errors /crashes, but the frustrating thing is when it works it completes in a few seconds. When I have got this step stable, my last addition is to try and exec the script from php. So far that has never worked and simply leaves the hanging processes.
I feel there is something fundamental left hanging around as far as the headless browser is concerned and would appreciate some guidance/help to resolve this issue please.

@regseb
Copy link

regseb commented Jun 4, 2021

I have a similar problem with Playwright (which is based on Puppeteer): microsoft/playwright#6796

Here is the equivalent of my code randomly hanging at browser.newPage():

const puppeteer = require('puppeteer');

(async () => {
    try {
        for (let i = 0; i < 100; ++i) {
            console.log(new Date(), i);
            const browser = await puppeteer.launch({
                executablePath: '/usr/bin/chromium',
            });
            const page = await browser.newPage();
            await page.close();
            await browser.close();
        }
    } catch (err) {
        console.log(new Date(), err);
    }
})();

@tiberiuzuld
Copy link

tiberiuzuld commented Jul 28, 2021

I have same issue, it just hangs at the newPage promise from what I gather from error when I manually kill chromium.

Error: Protocol error (Target.setAutoAttach): Target closed.
   at /usr/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:208:63
   at new Promise (<anonymous>)
   at CDPSession.send (/usr/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:207:16)
   at Page._initialize (/usr/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:177:26)
   at Function.create (/usr/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:169:20)
   at /usr/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Target.js:72:88
   at processTicksAndRejections (internal/process/task_queues.js:95:5)
   at async Browser._createPageInContext (/usr/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:253:22)
   at async RenderingEngine.render (/usr/app/bin/engine/renderingEngine.js:44:17) --> await browser.newPage();

I upgraded from puppeteer v6.0.0 where was working fine and this issue appears for me in both 9.1.1 and 10.1.0

@tiberiuzuld
Copy link

Tested this again with Node16, puppeteer 11 and Chromium 93 on Alpine
The issue still happens when opening multiple browsers in parallel.

@pcla56
Copy link
Author

pcla56 commented Nov 23, 2021

Friends, apologies for being so slow to update. I have managed to get this to be reliable by using the following code. I stumbled across it by accident, and have no idea why it works but for my purposes it seems to be reliable.

var myArgs = process.argv.slice(2);

const minimal_args = [
'--autoplay-policy=user-gesture-required',
'--disable-background-networking',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-breakpad',
'--disable-client-side-phishing-detection',
'--disable-component-update',
'--disable-default-apps',
'--disable-dev-shm-usage',
'--disable-domain-reliability',
'--disable-features=AudioServiceOutOfProcess',
'--disable-hang-monitor',
'--disable-ipc-flooding-protection',
'--disable-notifications',
'--disable-offer-store-unmasked-wallet-cards',
'--disable-popup-blocking',
'--disable-print-preview',
'--disable-prompt-on-repost',
'--disable-renderer-backgrounding',
'--disable-setuid-sandbox',
'--disable-speech-api',
'--disable-sync',
'--hide-scrollbars',
'--ignore-gpu-blacklist',
'--metrics-recording-only',
'--mute-audio',
'--no-default-browser-check',
'--no-first-run',
'--no-pings',
'--no-sandbox',
'--no-zygote',
'--password-store=basic',
'--use-gl=swiftshader',
'--use-mock-keychain',
'--allow-insecure-localhost',
];

const puppeteer = require('puppeteer');
const VIEWPORT = {width: 1024, height: 1500};
const URL = 'http://localhost/make-canvas/'+myArgs[0];

(async () => {
const browser = await puppeteer.launch({args:minimal_args,headless:true,ignoreHTTPSErrors: true,userDataDir:'/var/www/html/res/node/puppeteer_cache'});
const page = await browser.newPage();
await page.setViewport(VIEWPORT);
//await page.setCookie({name:'GNB_cookies',value:'dismiss',domain:'goodnewsbig.com'});
await page.goto(URL);
await page.goto(URL,{waitUntil: 'domcontentloaded'});
const modal = await page.waitForSelector('#gobig',{visible:true});
await modal.screenshot({ path: myArgs[1], type: 'jpeg' });
await page.close();
await browser.close();
})()

If this helps you move forward I'm pleased!

@benallfree
Copy link
Contributor

@pcla56 Can you please see if #7802 fixes this issue?

@claabs
Copy link

claabs commented Jan 26, 2022

I still regularly see this in Docker on multiple OS's (Alpine, Debian, Fedora), with multiple versions of Chrome/Chromium (93+).

My solution was to just add a retry function to .launch() and .newPage(). It usually works on the second attempt.

@rudolfbyker
Copy link

@tiberiuzuld
Copy link

tiberiuzuld commented Jun 21, 2022

Hello,
I upgraded now my instance to:
docker node:16-alpine3.16
puppeteer: 14.1.2
chromium: 102.0.5005.61-r0

 puppeteer.launch({
      headless: process.env.NODE_ENV === 'production',
      ignoreHTTPSErrors: true,
      args: ['--disable-infobars'],
      timeout: 30000 // 30s
    })

I was not able to reproduce the issue after 30 min of testing.
Planning to test some more this week and the next, will update if I am able to reproduce.

@stale
Copy link

stale bot commented Aug 30, 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.

@stale stale bot added the unconfirmed label Aug 30, 2022
@OrKoN OrKoN self-assigned this Aug 31, 2022
@matija2209
Copy link

matija2209 commented Sep 4, 2022

My puppeeter hangs when going throw pages. It doesn't throw any error whatsoever.

export const initializePuppeteerV2 = async () => {
    const browser = await puppeteer.launch({
      headless: true,
      args: [
        '--ignore-certificate-errors',
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-accelerated-2d-canvas',
        '--disable-gpu'
            ]
    });
    const context = await browser.createIncognitoBrowserContext();
    const page = await context.newPage(); // Create new instance of puppet
    page.on('error', err => {
        debugger
      logger.log('Puppeteer error.', err);
    });

    page.setDefaultNavigationTimeout(10000);

    if (process.env.NODE_ENV === 'production') {
      await page.setRequestInterception(true); // Optimize (no stylesheets, images)...
      page.on('request', request => {
        if (['image', 'stylesheet'].includes(request.resourceType())) {
          request.abort();
        } else {
          request.continue();
        }
      });
    }

    return {browser: context, page};
  }

@OrKoN
Copy link
Collaborator

OrKoN commented Sep 6, 2022

Closing in favour of #7475 as it appears none of the repros here are complete and commenters report different not really related problems in the thread. If you believe this issue still affects you, please open a new issue with complete information on how to reproduce it. Note that if you run many browser instances in parallel, you might run out of the system resources leading to timeouts.

@OrKoN OrKoN closed this as not planned Won't fix, can't repro, duplicate, stale Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants