question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AssertionError when intercepting requests on same url

See original GitHub issue

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 1.4.0
  • Platform / OS version: MacOS High Sierra
  • URLs (if applicable): any
  • Node.js version: 8.9.4

What steps will reproduce the problem? I have a pool of puppeteer browsers running (a minimum of 2). I recently added a request interceptor to filter out ad urls. When performing multiple requests to the same url, usually happens on the 3rd time, an AssertionError is generated for all filtered requests: AssertionError [ERR_ASSERTION]: Request is already handled!. The strack trace points to the interceptedRequest.continue() line of my page.on('request', (interceptedRequest) => {}) function in the api route below. Further on in the strack trace points to node_modules/puppeteer/lib/NetworkManager.js:220:10 and node_modules/puppeteer/lib/NetworkManager.js:169:12 while also showing an unhandled promise rejection error

Please include code that reproduces the issue.

// api route using the pool

const browserPagePool = require("../utils/browserPagePool");

router.get("/api/test", async (req, res, next) => {
  try {

    const page = await browserPagePool.acquire();

    await page.setRequestInterception(true);
    page.on('request', (interceptedRequest) => {
      if ( isAdUrl(interceptedRequest.url()) ) {
        interceptedRequest.abort();
      } else {
        interceptedRequest.continue();
      }
    });

    await page.goto(url, { waitUntil: "networkidle0", timeout: 10000 });
    const content = await page.content()
    await browserPagePool.release(page); 

    return res.status(200).json(content);

  } catch (err) {
    console.error(err);
  }
});

// browserPagePool.js

const genericPool = require("generic-pool");
const puppeteer = require("puppeteer");
const browserPromise = async () => await puppeteer.launch({ headless: true });

const factory = {
  create: async function() {
    const browser = await browserPromise();
    return browser.newPage();
  },
  destroy: function(puppeteer) {
    return puppeteer.close();
  }
};

const browserPagePool = genericPool.createPool(factory, {
  max: 10,
  min: 2,
  maxWaitingClients: 50
});

module.exports = browserPagePool;

Should I somehow be clearing out previous requests from an already used puppeteer pool, or checking if a request is already completed in the interceptedRequest function, or something else? I’m not sure how to gracefully handle this.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
aslushnikovcommented, Jun 29, 2018

@lsbyerley if you’re pooling browser pages, you should un-subscribe your current event listeners from the page object before releasing the page back to the pool; otherwise, you re-subscribe multiple times to the event.

A quick and a little dirty fix would be to add removeAllListeners('request') call:

  page.removeAllListeners('request'); // remove listeners we were using for this task.
  await browserPagePool.release(page); 

Hope this helps.

7reactions
amitksingh1490commented, Feb 15, 2019

you can check interceptedRequest._interceptionHandled to be false in order to avoid this error

Read more comments on GitHub >

github_iconTop Results From Across the Web

Browser-Mob Intercepting multiple requests with same url but ...
I want to intercept requests in my application and one of the scenarios I have come across is that - due to nature...
Read more >
nock - npm
Nock works by overriding Node's http.request function. ... This means that you can intercept 2 or more calls to the same URL and...
Read more >
nock - Devpost
Nock can be used to test modules that perform HTTP requests in isolation. ... This means that you can intercept 2 or more...
Read more >
5. Creating a Web service with Spring-WS
xml of your web application. Requests that you want the MessageDispatcherServlet to handle will have to be mapped using a URL mapping in...
Read more >
Cypress and Flaky Tests: How to Handle Timeout Errors
The term 'flaky test' is a general one that can apply to any test ... The example above displays an intercepted network request...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found