Skip to content

fix: close the failed tab and restore selection when new_page navigation fails - #3019

Closed
chiragjangiir wants to merge 5 commits into
ChromeDevTools:mainfrom
chiragjangiir:fix/new-page-cleanup-on-failed-navigation
Closed

chiragjangiir wants to merge 5 commits into
ChromeDevTools:mainfrom
chiragjangiir:fix/new-page-cleanup-on-failed-navigation

Conversation

@chiragjangiir

@chiragjangiir chiragjangiir commented Oct 7, 2026 •

Copy link
Copy Markdown

fix: close the failed tab and restore selection when new_page navigation fails

Fixes #2925

Problem

new_page creates its tab and selects it (in McpContext.newPage) before navigation starts. When the goto then fails — blocked by --blockedUrlPattern/--allowedUrlPattern, connection refused, or navigation timeout — the tool returns isError: true with only the navigation error, but:

  • the failed tab stays open, and
  • it silently remains the selected page.

Agents accumulate stray error tabs with a moved [selected] marker, and under --no-page-id-routing the next page-scoped call (e.g. evaluate_script) silently runs on the leftover tab instead of the page the agent was working on.

Fix

In the new_page handler (src/tools/pages.ts), when navigation fails after the tab was created:

  1. Re-select the page that was selected before the call, if it is still open.
  2. Close the tab this call created — by its own id, never by whatever happens to be selected.
  3. Rethrow the original navigation error, so isError: true and the original message are kept.

This implements the first outcome the issue accepts: no tab from the failed call remains open, and the previously selected page stays selected. The tab is identified by the id of the page context.newPage() returned, so a selection that moved for any other reason cannot make the cleanup close the wrong page — or skip itself.

background: true needs no separate handling: McpContext.newPage selects every page it creates and background only keeps it from being brought to the front, so a failed background load moves the selection exactly like a foreground one.

If the tab cannot be closed, the response says so instead of hiding it — it names the tab and the selection that was kept, and lists the pages, while the original navigation error is still what is thrown:

Note: the tab this call opened is still open as page 2. Page 1 is selected again.
## Pages
1: about:blank [selected]
2: about:blank

Dialog-blocked loads. A load whose page opens a dialog during parse can only fail (goto cannot settle while the renderer is paused), so this closes that tab like any other failure. Closing a page with a dialog open is safe here — measured at 8 ms with no error — and it keeps the guarantee uniform: after any failed new_page, no tab from that call is open and the selection is unchanged. #2928 takes the other split and keeps the tab for handle_dialog; if you prefer that, the change is small and I will make it, but as #2928 stands the kept tab is still not named in the error.

To get the created page's id in the handler, Context.newPage returns McpPage, which is what McpContext.newPage already returns.

Testing

tests/tools/pages.test.ts gains a test per failure class and per state that is not simply undone. Nine of the ten fail against main:

test on main
closes the tab when the navigation is blocked fails
closes the tab when the URL is not in the allowed list fails
closes the tab when the connection is refused fails
closes the tab when the navigation times out fails
closes the tab when it was opened in the background fails
closes the tab when a dialog blocks the load fails
keeps another page and its dialog when the load fails fails
reports a tab it could not close and keeps the navigation error fails
leaves page-scoped calls on the page selected before a failed new_page fails
leaves the pages and the selection alone when the URL is rejected before a tab exists passes (unchanged behaviour)

The connection-refused and timeout cases use a loopback port with no listener and a loopback server that never answers, so they need no network. The routing case drives evaluate_script with pageIdRouting: false and asserts the script ran on the page selected before the failure.

  • npm run test:no-build — 1342 tests, 1339 pass, 0 fail, 3 skipped (two Windows-only named-pipe tests, one fullscreen window-state test).
  • npm run typecheck, npm run check-format — pass.
  • npm run gen leaves no diff, so the generated docs are current.

Docs

new_page's description now states what a failure leaves behind, and docs/tool-reference.md and src/config/cli-options.ts are the npm run gen output for it. skills/chrome-devtools/SKILL.md no longer reads as if every new_page yields a page id to target.

Notes

  • new_page rejected by URL validation (e.g. chrome://settings) is unchanged: it throws before any tab is created, so nothing is closed and nothing is re-selected.
  • Successful new_page is unchanged, background: true included.
  • navigate_page failure semantics are untouched: it reports in-band on the selected page.

@google-cla

google-cla Bot commented Oct 7, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@chiragjangiir
chiragjangiir force-pushed the fix/new-page-cleanup-on-failed-navigation branch from f22d4b1 to ac06b08 Compare October 7, 2026 09:32
…ion fails

A failed new_page navigation left the newly created tab open and
selected, accumulating stray tabs and silently misrouting page-scoped
tools under --no-page-id-routing. Restore the pre-call state by
re-selecting the previously selected page and closing the failed tab,
then surface the original navigation error.

Fixes ChromeDevTools#2925

🤖 Generated with Codebuff
@chiragjangiir
chiragjangiir force-pushed the fix/new-page-cleanup-on-failed-navigation branch from ac06b08 to c0e30f6 Compare October 7, 2026 22:58
…s selection

`new_page` creates and selects a tab before navigating, so a failed navigation
left that tab open as the selected page while the error response listed no
pages: the caller could not tell that either had happened, and with pageId
routing off the next page-scoped call silently ran on the leftover tab. Close
the tab this call created — by its own id, never by whatever happens to be
selected now — and put the selection back where it was.

`background: true` is no exception: `McpContext.newPage` selects every page it
creates, so the same failure moved the selection there too.

A tab that could not be closed is reported in the response with its id and the
selection that was kept, so a failure never leaves state the caller cannot see.

🤖 Generated with Codebuff
One test for each way the tab can be lost — a blocklist refusal, an allowlist
refusal, a refused connection, a navigation timeout, `background: true`, and a
dialog that pauses the renderer — each asserting the pages and the selection
are the ones from before the call. Nine of the ten fail against `main`.

Plus the state that is not simply undone: a page that already held a dialog
keeps it and stays selected, a tab that could not be closed is named in the
response along with the selection that was restored, a URL rejected before a
tab exists closes nothing and re-selects nothing, and a page-scoped call with
pageId routing off still runs on the page selected before the failure.

🤖 Generated with Codebuff
The tool description and the skill now state that a failed load closes the tab
it opened, keeps the previously selected page selected, and reports the tab by
id instead when it could not be closed, so a caller knows the error left no new
page to target.

`src/config/cli-options.ts` and `docs/tool-reference.md` are generated from the
description; both are the output of `npm run gen`.

🤖 Generated with Codebuff
@chiragjangiir

Copy link
Copy Markdown
Author

I have reworked this so it covers every failure class in the issue's table, including two the first revision missed.

What the first revision got wrong. It found the tab to close by asking for the currently selected page, and closed it only if that happened to be the tab this call created. Had anything else moved the selection in between, the tab would have been left open and the fix would have skipped itself silently. It also swallowed a cleanup failure in an empty catch, so a tab that could not be closed was left unreported — the same defect the issue is about, one level down.

What changed.

  • The tab is closed by the id of the tab this call created, never by whatever is selected.
  • The selection is restored before the close, so a close that fails still leaves the selection right.
  • A close that fails is no longer silent: the response names the remaining tab and the selection that was kept and lists the pages (Note: the tab this call opened is still open as page 2. Page 1 is selected again.), while the original navigation error is still what is thrown and the result still isError: true.

Coverage. Ten tests, one per criterion in the issue. Nine fail against main and all ten pass here. background: true is covered explicitly: McpContext.newPage selects every page it creates, so a failed background load moves the selection just like a foreground one.

One judgement call worth your review. When a load is blocked by a dialog (alert() during parse), this closes the tab and drops the dialog, where #2928 keeps the tab so handle_dialog can answer it. I went with closing because it keeps the guarantee uniform — after any failed new_page, no tab from that call is open and the previous selection is unchanged — and I measured that closing a page with a pending dialog is safe (8 ms, no error). #2928's split is defensible too, but as it stands the kept tab is not named in the error, so that class is still unreported there. If you would rather have the dialog-blocked tab kept, say so and I will keep it and name its pageId in the error instead.

Overlap with #2928. Both PRs need the created page's identity in the handler and both widen Context.newPage to return McpPage (which is what McpContext.newPage already returns); we arrived at that independently. If you would rather land one PR than two, I am glad to fold in whatever is worth keeping from the other.

Docs. new_page's description now states the failure behaviour; docs/tool-reference.md and src/config/cli-options.ts are its npm run gen output, and skills/chrome-devtools/SKILL.md no longer reads as if a failed call yields a page id.

Verification. npm run test:no-build — 1342 tests, 1339 pass, 0 fail, 3 skipped (two Windows-only named-pipe tests, one fullscreen window-state test). npm run typecheck and npm run check-format pass, and npm run gen leaves no diff.

@yulunz

yulunz commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR and the clear write-up!

#2928 was opened earlier for #2925 and also preserves the new tab when goto times out due to an open JavaScript dialog (page.getDialog()), so we're going to move forward with #2928 (incorporating the best-effort try/catch and !previousPage.isClosed() guard from your cleanup logic). Closing this as a duplicate of #2928—thanks again!

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.

Failed new_page leaves an unreported tab open and selected; close it or report its pageId

2 participants