puppeteer: Error: Protocol error (Runtime.callFunctionOn): Target closed.

I’m running into this error after 22 intervals.

  • Node.js 8 + Puppeteer on Debian.
 try {
    await page.evaluate(async () => (
      new Promise((resolve, reject) => {
        try {
          const maxScroll = Number.MAX_SAFE_INTEGER;
          let lastScroll = 0;
          const interval = setInterval(() => {
            window.scrollBy(0, document.body.offsetHeight);
            const { scrollTop } = document.documentElement;
            if (scrollTop === maxScroll || scrollTop === lastScroll) {
              clearInterval(interval);
              resolve();
            } else {
              lastScroll = scrollTop;
            }
          }, 1000);
        } catch (error) {
          reject(error);
        }
      })
    ));
  } catch (error) {
    log('Error while scrolling:', error);
  }
  log('Scrolling finished');

To reproduce it, just open a SPA that has lazy loading when scrolled to the end of the page.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 12
  • Comments: 23 (9 by maintainers)

Most upvoted comments

Just to make @aslushnikov’s comment more explicit, with an example right here, I found that adding the --disable-dev-shm-usage launch option made a massive difference:

const browser = await puppeteer.launch({
  args: ['--disable-dev-shm-usage']
});

See also here.

In my case, it was because I carelessly put await browser.close(); above await page.close();

so --shm-size=1gb should solve your issue?

–shm-size=1gb

browser = await pptrLaunch({
      // devtools: true,
      args: [
        '--enable-features=NetworkService',
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-dev-shm-usage',
        '--disable-web-security',
        '--disable-features=IsolateOrigins,site-per-process',
        '--shm-size=3gb', // this solves the issue
      ],
      ignoreHTTPSErrors: true,
    });

In my case, I am opening same URL, in 12 tabs(browser.newPage() ), and in each tab, there is 1 api which gives huge JSON response. In this case, I am getting Error: Protocol error (Runtime.callFunctionOn): Target closed. To avoid this, I set --shm-size=3gb, which helps to increase pptr browser memory. Working fine now.

Turning OFF slowMo works for me.

@andy2046 I have the same error. I’m on windows 10, node version 8.9.3. Can you please help me with some hints regarding how to set the parameter --shm-size?

can you try to add catch for the Promise, as shown below, you also need to try catch and reject inside setInterval

await page.evaluate(async () => {
      new Promise((resolve, reject) => {
        try {
          const maxScroll = Number.MAX_SAFE_INTEGER;
          let lastScroll = 0;
          const interval = setInterval(() => {
            window.scrollBy(0, document.body.offsetHeight);
            const { scrollTop } = document.documentElement;
            if (scrollTop === maxScroll || scrollTop === lastScroll) {
              clearInterval(interval);
              resolve();
            } else {
              lastScroll = scrollTop;
            }
          }, 1000);
        } catch (error) {
          reject(error);
        }
      }).catch(error => {
        console.error(error) // add catch here
      })
    });

@andy2046 I’d be happy to look into this if you share full script with a URL.