rebrowser / rebrowser-patches

Collection of patches for puppeteer and playwright to avoid automation detection and leaks. Helps to avoid Cloudflare and DataDome CAPTCHA pages. Easy to patch/unpatch, can be enabled/disabled on demand.
https://rebrowser.net
306 stars 27 forks source link

Error: [rebrowser-patches] acquireContextId failed #18

Closed younes-zeboudj closed 1 week ago

younes-zeboudj commented 1 month ago

Sometimes, page.evaluate(...) throws the following error:

Error: [rebrowser-patches] acquireContextId failed
    at ExecutionContext.acquireContextId (c:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\ExecutionContext.js:390:19)
    at async #evaluate (c:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\ExecutionContext.js:397:13)
    at async ExecutionContext.evaluate (c:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\ExecutionContext.js:280:16)
    at async IsolatedWorld.evaluate (c:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\IsolatedWorld.js:99:16)
    at async CdpFrame.evaluate (c:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\api\Frame.js:339:20)
    at async CdpPage.evaluate (c:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\api\Page.js:805:20)
    at async loadhandler (C:\Users\DELL\Desktop\newtls\job\dist\bundle.js:1037:7) {stack: 'Error: [rebrowser-patches] acquireContextId f…Desktop\\newtls\\job\\dist\\bundle.js:1037:7)', message: '[rebrowser-patches] acquireContextId failed'}

This happens quite often but I can't figure out what triggers it. This may be due to the fast loading of a page.

younes-zeboudj commented 1 month ago

workaround, didn't test it thoroughly yet

async function evaluateInPage(page, content) {
  while (true) {
    let retry;
    let r= await page
      .evaluate(content)
      .catch(
        (e) => (retry = e?.toString().includes("acquireContextId failed"))
      );

    if (retry) {
      await new Promise((R) => setTimeout(R, 300));
    } else {
      return r;
    }
  }
}
nwebson commented 1 month ago

Do you have any specific website where it happens the most? Could it be something because of many iframes on the page? Also, try to set process.env.REBROWSER_PATCHES_DEBUG=1, it will give you some debug messages right before this error happens.

nwebson commented 1 month ago

Btw, I just added another place for logging this error, please use the latest patch and process.env.REBROWSER_PATCHES_DEBUG=1.

m4Nu2oo7 commented 1 month ago

Thank you for providing the patches! However, I haven't been able to use them successfully so far. For instance, I encounter the error 'acquireContextId failed' already with this code snippet. This is just an example to show that fetch does not work inside evaluate.

const browser = await puppeteer.launch({
  headless: false,
  executablePath: config.googleChromePath,
});
const context = await browser.createBrowserContext();
const page = await context.newPage();
const url = "https://httpbin.org/ip";
await page.goto(url);
const ip = await page.evaluate(async (url) => {
  const response = await fetch(url);
  const data = await response.json();
  return data.origin;
}, url);
console.log("IP:", ip);
younes-zeboudj commented 4 weeks ago

Moreover, I get the following issue occasionally:

Exception has occurred: ProtocolError: Protocol error (Runtime.evaluate): Cannot find context with specified id
  at <instance_members_initializer> (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\common\CallbackRegistry.js:93:14)
    at new Callback (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\common\CallbackRegistry.js:97:16)
    at CallbackRegistry.create (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\common\CallbackRegistry.js:22:26)
    at Connection._rawSend (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\Connection.js:89:26)
    at CdpCDPSession.send (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\CDPSession.js:66:33)
    at #evaluate (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\ExecutionContext.js:410:18)
    at ExecutionContext.evaluate (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\ExecutionContext.js:280:36)
    at IsolatedWorld.evaluate (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\IsolatedWorld.js:99:30)
    at CdpFrame.evaluate (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\api\Frame.js:339:43)
    at CdpFrame.<anonymous> (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\util\decorators.js:98:27)

I couldn't find a solution yet, even retrying the evaluation many times. It seems like the library is causing puppeteer to lose access to contexts's ids in some situations.

nwebson commented 2 weeks ago

📣 I just released a new version of rebrowser-patches with a completely new fix for Runtime.Enable leak. This fix doesn't lose access to main context and also works in workers 🎉 It's a complete drop-in replacement, so you don't need to change any of your existing code. Please try your code with new version.