Skip to content

fix(runtime)!: evaluate in one call, raise dedicated errors for a navigation and a foreign object - #755

Merged
route merged 1 commit into
mainfrom
fix/await-context-destroyed-error
Nov 30, 2026
Merged

route merged 1 commit into
mainfrom
fix/await-context-destroyed-error

Conversation

@route

@route route commented Nov 30, 2026

Copy link
Copy Markdown
Member

An object result of evaluate without a subtype (a plain object, a class instance, a built-in like Math) took two Runtime.callFunctionOn calls, both with deep serialization, so its contents crossed the socket twice. The second call awaited a possible thenable, since Chrome marks only native promises. Every await took two calls as well.

Now the first call sends awaitPromise: true, which awaits native promises and any thenable in the same call, as Puppeteer and Playwright do:

page.evaluate("({s: 'x'.repeat(1024 * 1024)})")   # 1 MiB received instead of 2
page.evaluate("await fetch(url).then(r => r.status)", url: "/")   # one call instead of two
page.evaluate("location.href = '/next'; ({a: 1})")   # {"a" => 1}, raised NoExecutionContextError before

The second call used to lose the race with a navigation the script started, that's the last line.

The await was a separate call so that it stayed out of the retries: an await cut off by a navigation raised NodeNotFoundError, which is retried, and would run the script again. So errors are now mapped by what they mean:

  • "Inspected target navigated or closed" and "Execution context was destroyed." raise ExecutionContextDestroyedError. No node is involved, and a command cut off this way may have already run, so it isn't retried.
  • "Argument should belong to the same JavaScript world as target object" raises ForeignObjectError: a Node or RemoteObject argument from a previous page or another isolated world. A retry can't help it.

The errors evaluate still retries, NodeNotFoundError and NoExecutionContextError, are now raised only before the script runs: a stale context id or a Node argument that can't be resolved yet.

Breaking:

  • A command cut off by a navigation raises ExecutionContextDestroyedError instead of NodeNotFoundError, or a plain BrowserError in an iframe.
  • An argument from another page or world raises ForeignObjectError instead of NodeNotFoundError.

…igation and a foreign object

An object result of evaluate took a second Runtime.callFunctionOn to await a possible thenable, so its contents crossed
the socket twice, and every awaited script took two calls. The first call now sends awaitPromise: true, which awaits
native promises and thenables alike. A script that starts a navigation and returns an object no longer raises
NoExecutionContextError, the second call used to lose that race.

A single call needs the retry to be safe, so errors are mapped by what they mean:
- "Inspected target navigated or closed" and "Execution context was destroyed." raise ExecutionContextDestroyedError
  instead of NodeNotFoundError or a plain BrowserError. No node is involved, and a command cut off this way may have
  already run, so it isn't retried. Node#exists? still returns false for it.
- "Argument should belong to the same JavaScript world as target object" raises ForeignObjectError instead of
  NodeNotFoundError: a Node or RemoteObject argument from a previous page or another isolated world, which can't
  succeed on a retry.

NodeNotFoundError and NoExecutionContextError, the errors evaluate still retries, are now raised only before the script
runs.
@route
route merged commit 30406fe into main Nov 30, 2026
6 of 7 checks passed
@route
route deleted the fix/await-context-destroyed-error branch November 30, 2026 19:20
route added a commit to rubycdp/cuprite that referenced this pull request Nov 30, 2026
…bjectError as a stale node (#403)

Ferrum raises ExecutionContextDestroyedError for a command cut off by a navigation and ForeignObjectError for a node
argument from another page or world, both were NodeNotFoundError before (rubycdp/ferrum#755). Node commands raise
ObsoleteNode for them, Capybara finds the element again after either, and set doesn't fail when a navigation cuts
off its refocus.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant