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.

When would an error "Cannot find context with specified id undefined" happen?

See original GitHub issue

I am getting the following error:

Error: Protocol error (Runtime.callFunctionOn): Cannot find context with specified id undefined
    at Session._onMessage (/srv/node_modules/puppeteer-edge/lib/Connection.js:205:25)
    at Connection._onMessage (/srv/node_modules/puppeteer-edge/lib/Connection.js:105:19)
    at emitOne (events.js:115:13)
    at WebSocket.emit (events.js:210:7)
    at Receiver._receiver.onmessage (/srv/node_modules/ws/lib/WebSocket.js:143:47)
    at Receiver.dataMessage (/srv/node_modules/ws/lib/Receiver.js:389:14)
    at Receiver.getData (/srv/node_modules/ws/lib/Receiver.js:330:12)
    at Receiver.startLoop (/srv/node_modules/ws/lib/Receiver.js:165:16)
    at Receiver.add (/srv/node_modules/ws/lib/Receiver.js:139:10)
    at Socket._ultron.on (/srv/node_modules/ws/lib/WebSocket.js:139:22)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:266:12)
    at readableAddChunk (_stream_readable.js:253:11)
    at Socket.Readable.push (_stream_readable.js:211:10)
    at TCP.onread (net.js:585:20)

It is a large codebase and it is unclear whats triggering this error.

Any guides?

On that note, there needs to be a better way to throw error. Without knowing the origin of the error in the code it is impossible to trace down these exceptions.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:132
  • Comments:103 (29 by maintainers)

github_iconTop GitHub Comments

29reactions
jakub300commented, Jun 7, 2018

Hi, encountered this problem when updating code that was using puppeteer < 1.0 to newest version. I was also able to find source of the problem.

First, my reproduction script

const puppeteer = require('puppeteer');
let page;

const sleep = (time) => new Promise(resolve => setTimeout(resolve, time));

function wait() {
  page
    .waitForSelector('body')
    .then(() => {
      wait();
    })
    .catch(e => {});
}

(async () => {
  const browser = await puppeteer.launch();
  page = await browser.newPage();
  wait();
  for(let i = 0; i < 10; i += 1) {
    await sleep(1000);
    await page.goto('http://example.com/');
  }
  await browser.close();
})().catch(e => {});

with error:

(node:47660) UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Cannot find context with specified id undefined
    at Promise (/Users/jakubbogucki/Projects/GeneratorChisel/puppeteer-test/node_modules/puppeteer/lib/Connection.js:200:56)
    at new Promise (<anonymous>)
    at CDPSession.send (/Users/jakubbogucki/Projects/GeneratorChisel/puppeteer-test/node_modules/puppeteer/lib/Connection.js:199:12)
    at ExecutionContext.evaluateHandle (/Users/jakubbogucki/Projects/GeneratorChisel/puppeteer-test/node_modules/puppeteer/lib/ExecutionContext.js:79:75)
    at ExecutionContext.evaluate (/Users/jakubbogucki/Projects/GeneratorChisel/puppeteer-test/node_modules/puppeteer/lib/ExecutionContext.js:46:31)
    at Frame.evaluate (/Users/jakubbogucki/Projects/GeneratorChisel/puppeteer-test/node_modules/puppeteer/lib/FrameManager.js:326:20)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

Please note that it doesn’t happen every time so I added the loop, but possibly 10 runs may be not enough.

End the source of the problem is that line: https://github.com/GoogleChrome/puppeteer/blob/808bf8e5582482a1d849ff22a51e52024810905c/lib/FrameManager.js#L875 You can replace evaluation with

this._frame.evaluate(s => !s, success).catch(e => {})

If you look at rerun function as a whole you will see that earlier it is wrapping evaluation in try/catch and handles errors but that evaluation is not protected in any way. Ironically later there is check for Cannot find context with specified id but it is never reached if second evaluation fails.

/cc @JoelEinbinder @aslushnikov author and reviewer of #1746 that introduced the problematic line

21reactions
gajuscommented, Nov 23, 2017

Then why not release 0.14.0? Like, now.

There is literally no reason whatsoever not to automate the releases. There is no reason whatsoever not to release a new version every time a PR has been merged into the master. If you merge something into the master, then it is ready for the release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

cannot find context with specified id undefined - Stack Overflow
My test was completing but I was getting this error when attempting to await browser.close(); Adding the wait after searching for my final ......
Read more >
puppeteer error "Cannot find context with specified id undefined"
What are you trying to achieve? open a page; click a link by click; see text from new page. What do you get...
Read more >
Testbot failures "Cannot find context with specified id" - Drupal
It seems some of the random failures happen when the chromedriver can't find a stale context. Therefore, I'm removing this: - $session =...
Read more >
undefined - JavaScript - MDN Web Docs - Mozilla
The global undefined property represents the primitive value undefined . It is one of JavaScript's primitive types.
Read more >
Cannot read properties of undefined (reading 'length') - TrackJS
This is a blocking error, and script execution will stop when this error occurs. Understanding the Root Cause. This error can be thrown...
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