Skip to content

webview: skip load handlers when pending frame is unmounted (#344744) - #344773

Merged
Dmitriy Vasyura (dmitrivMS) merged 5 commits into
microsoft:mainfrom
ayushkrsingh946:fix/webview-unmounted-frame-error
Nov 17, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 5 commits into
microsoft:mainfrom
ayushkrsingh946:fix/webview-unmounted-frame-error

Conversation

@ayushkrsingh946

Copy link
Copy Markdown
Contributor

Fixes #344744

Problem

When a webview view sets webview.options and then webview.html in quick succession (e.g. inside resolveWebviewView), two content update messages are dispatched to the webview host.

  1. The first content message creates a pending iframe (newFrame), attaches it to document.body, and begins loading ./fake.html.
  2. Upon DOMContentLoaded, onFrameLoaded schedules a setTimeout(..., 0) to write content and call hookupOnLoadHandlers(newFrame).
  3. Meanwhile, the second content message arrives and removes the previous pending frame from the DOM (previousPendingFrame.remove()) to replace it with a new one.
  4. When the deferred setTimeout from the first frame runs, newFrame is already detached from the DOM. An unmounted iframe element has contentWindow === null, causing assertIsDefined(newFrame.contentWindow) in hookupOnLoadHandlers to throw:
    Uncaught Error: Found unexpected null at assertIsDefined (index.html:94) at hookupOnLoadHandlers (index.html:1181)
    

Fix

  • Guard onFrameLoaded's deferred setTimeout callback and DOMContentLoaded listener so that if !newFrame.parentElement (frame is no longer mounted), we stop early without writing content or hooking up handlers. This mirrors the existing unmounted check in the Safari polling branch (// If the frame is no longer mounted, loading has stopped).
  • Guard hookupOnLoadHandlers and its 200ms fallback timeout if !newFrame.parentElement, preventing attempts to access newFrame.contentWindow or clobber timers for newer pending frames.

Copilot AI balanced review requested due to automatic review settings October 28, 2026 15:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The inline script changes leave the CSP hash stale, causing browsers to block the bootstrap script and preventing webviews from initializing.

1 open issue
What changed in this PR

This PR addresses #344744 by skipping webview load work for iframes removed during rapid content updates, preventing access to a detached frame’s null contentWindow.

Changes:

  • Add mounted-frame checks before deferred content writes and DOMContentLoaded handling.
  • Guard load-handler setup, load events, and the fallback timeout against detached frames.
File Description
src/​vs/​workbench/​contrib/​webview/​browser/​pre/​index.html Adds detached-frame guards throughout the webview loading lifecycle.

Review effort: Balanced

@ayushkrsingh946

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Existing preload tests would pass without the guards, so deterministic coverage is needed for iframe replacement before the deferred callback and removal before the 200 ms fallback.

1 open issue
1 resolved since last review

Review effort: Max

Resolve the preload CSP conflict by hashing the combined inline script.

Co-authored-by: Copilot <[email protected]>
@dmitrivMS

Copy link
Copy Markdown
Collaborator

Sorry about that, fixing...

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) added this pull request to the merge queue Nov 17, 2026
Merged via the queue into microsoft:main with commit ef9c58c Nov 17, 2026
37 of 38 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.148.0 milestone Nov 17, 2026
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.

Webview view: "Found unexpected null" in hookupOnLoadHandlers when options and html are set back to back

5 participants