Skip to content

Fix inactive document unregistration's notification algorithm - #345

Merged
domfarolino merged 2 commits into
mainfrom
fix-inactive-unregistration
Oct 14, 2026
Merged

domfarolino merged 2 commits into
mainfrom
fix-inactive-unregistration

Conversation

@domfarolino

@domfarolino domfarolino commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

See https://crrev.com/c/8578418 and https://crrev.com/c/8582224 for tests. After this CL, unregistration of an individual tool in an inactive document no longer attempts to notify all other documents in the frame that the tool has been unregistered.

Note that we may want to consider notifying all other documents that the set of tools have changed, when a tool-owning document gets removed and becomes inactive. That's besides this CL, and is being explored in https://crbug.com/508285989, but I'll hold off on filing a spec issue for that specifically, since the story/need for that may change given the changes that we'll make to satisfy #236.


Preview | Diff

Comment thread index.bs
beckysiegel pushed a commit to chromium/chromium that referenced this pull request Oct 14, 2026
`ModelContext::UnregisterTool()` always sends `UnregisterScriptTool()`
to the browser. But once a document is inactive, e.g., after its iframe
is removed, its `model_context_host_remote_` (a `HeapMojoRemote`) has
been reset as part of its execution context being destroyed, and calling
a method on an unbound remote crashes the renderer.

This is reachable from script, by aborting an imperative tool's
`AbortSignal` after the registering document has become inactive, e.g.,
after its iframe is removed:

const controller = new AbortController();
  await iframe.contentDocument.modelContext.registerTool(
      {...}, {signal: controller.signal});
  iframe.remove();
  controller.abort();  // Crash.

This CL still removes the tool from `ModelContext`'s own state, but only
notifies the browser when the document is active. An inactive document
has no browser host to notify: frameless documents never bind one, and
the document of a removed frame becomes inactive before its execution
context is destroyed. This matches the `IsActive()` checks that
`registerTool()` and `executeTool()` already perform.

See webmachinelearning/webmcp#345.

R=beaufort.francois@gmail.com

Bug: 573454028
Change-Id: If36db9f87aaa750561c1368b9b20e0a5a974b423
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8578418
Reviewed-by: Khushal Sagar <khushalsagar@chromium.org>
Commit-Queue: Dominic Farolino <dom@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1718966}
@domfarolino

Copy link
Copy Markdown
Collaborator Author

Welcome back @khushalsagar!

@domfarolino
domfarolino merged commit 1636b33 into main Oct 14, 2026
2 checks passed
@domfarolino
domfarolino deleted the fix-inactive-unregistration branch October 14, 2026 22:00
github-actions Bot added a commit that referenced this pull request Oct 14, 2026
SHA: 1636b33
Reason: push, by domfarolino

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Oct 14, 2026
`ModelContext::UnregisterTool()` always sends `UnregisterScriptTool()`
to the browser. But once a document is inactive, e.g., after its iframe
is removed, its `model_context_host_remote_` (a `HeapMojoRemote`) has
been reset as part of its execution context being destroyed, and calling
a method on an unbound remote crashes the renderer.

This is reachable from script, by aborting an imperative tool's
`AbortSignal` after the registering document has become inactive, e.g.,
after its iframe is removed:

const controller = new AbortController();
  await iframe.contentDocument.modelContext.registerTool(
      {...}, {signal: controller.signal});
  iframe.remove();
  controller.abort();  // Crash.

This CL still removes the tool from `ModelContext`'s own state, but only
notifies the browser when the document is active. An inactive document
has no browser host to notify: frameless documents never bind one, and
the document of a removed frame becomes inactive before its execution
context is destroyed. This matches the `IsActive()` checks that
`registerTool()` and `executeTool()` already perform.

See webmachinelearning/webmcp#345.

R=beaufort.francois@gmail.com

Bug: 573454028
Change-Id: If36db9f87aaa750561c1368b9b20e0a5a974b423
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8578418
Reviewed-by: Khushal Sagar <khushalsagar@chromium.org>
Commit-Queue: Dominic Farolino <dom@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1718966}
beckysiegel pushed a commit to chromium/chromium that referenced this pull request Oct 15, 2026
`ModelContext::registerTool()` checks that its document is active up
front, but then serializes `inputSchema`, which can run script via a
`toJSON()` method. If that script makes the document inactive, e.g., by
removing its iframe, `registerTool()` carries on and dereferences the
document's now-null frame (and would then call into its reset
`ModelContextHost` remote), crashing the renderer:

  iframe.contentDocument.modelContext.registerTool({
    name: 'tool',
    description: 'desc',
    inputSchema: {toJSON() { iframe.remove(); return {}; }},
    execute: () => {},
  });  // Crash.

This CL checks again after serialization, and rejects with the same
`InvalidStateError` as the up-front check. This matches the re-check
added to the spec in
webmachinelearning/webmcp#345.

R=khushalsagar

Bug: 573671121
Change-Id: I9afb20dfa0d23d6aa54d34b568e17461b88d39db
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8582224
Reviewed-by: Khushal Sagar <khushalsagar@chromium.org>
Commit-Queue: Dominic Farolino <dom@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1719223}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Oct 15, 2026
`ModelContext::registerTool()` checks that its document is active up
front, but then serializes `inputSchema`, which can run script via a
`toJSON()` method. If that script makes the document inactive, e.g., by
removing its iframe, `registerTool()` carries on and dereferences the
document's now-null frame (and would then call into its reset
`ModelContextHost` remote), crashing the renderer:

  iframe.contentDocument.modelContext.registerTool({
    name: 'tool',
    description: 'desc',
    inputSchema: {toJSON() { iframe.remove(); return {}; }},
    execute: () => {},
  });  // Crash.

This CL checks again after serialization, and rejects with the same
`InvalidStateError` as the up-front check. This matches the re-check
added to the spec in
webmachinelearning/webmcp#345.

R=khushalsagar

Bug: 573671121
Change-Id: I9afb20dfa0d23d6aa54d34b568e17461b88d39db
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8582224
Reviewed-by: Khushal Sagar <khushalsagar@chromium.org>
Commit-Queue: Dominic Farolino <dom@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1719223}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Oct 15, 2026
`ModelContext::registerTool()` checks that its document is active up
front, but then serializes `inputSchema`, which can run script via a
`toJSON()` method. If that script makes the document inactive, e.g., by
removing its iframe, `registerTool()` carries on and dereferences the
document's now-null frame (and would then call into its reset
`ModelContextHost` remote), crashing the renderer:

  iframe.contentDocument.modelContext.registerTool({
    name: 'tool',
    description: 'desc',
    inputSchema: {toJSON() { iframe.remove(); return {}; }},
    execute: () => {},
  });  // Crash.

This CL checks again after serialization, and rejects with the same
`InvalidStateError` as the up-front check. This matches the re-check
added to the spec in
webmachinelearning/webmcp#345.

R=khushalsagar

Bug: 573671121
Change-Id: I9afb20dfa0d23d6aa54d34b568e17461b88d39db
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8582224
Reviewed-by: Khushal Sagar <khushalsagar@chromium.org>
Commit-Queue: Dominic Farolino <dom@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1719223}
Comment thread index.bs
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.

3 participants