Skip to content

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

Closed

Description

VS Code version: 1.143.1 (Windows 11)

What happens. A WebviewViewProvider that sets webview.options and then webview.html in resolveWebviewView sometimes makes the webview host page throw an uncaught error:

Uncaught Error: Found unexpected null
    at assertIsDefined (index.html:94)
    at hookupOnLoadHandlers (index.html:1181)
    at index.html:1079

The view still renders correctly: the second inner frame loads. The error shows up in the console of the webview target, for example when it is captured over CDP. In our automated UI runs it happens about 2 times in 18.

Why it seems to happen (from reading the 1.143.1 build):

  • set contentOptions calls doUpdateContent whenever the options differ. That sends content with the current html, which is still empty at that point.
  • setHtml calls doUpdateContent again, so the host page receives two content messages and builds one inner frame per message.
  • If the first frame fires DOMContentLoaded just before the second message replaces it, the setTimeout(0) in onFrameLoaded runs hookupOnLoadHandlers(newFrame) on a frame that has already been removed. Its contentWindow is then null, and assertIsDefined throws.

Why the extension cannot avoid it:

  • The default options have scripts disabled, so a view that needs enableScripts: true always triggers an options update.
  • Setting html first and options afterwards also builds two frames.
  • registerWebviewViewProvider cannot take enableScripts up front.

Steps to reproduce:

  1. Register a webview view whose resolveWebviewView does webview.options = { enableScripts: true, localResourceRoots: [...] } and then webview.html = ....
  2. Attach to the view's webview target over CDP with Runtime.enable right after the view is revealed.
  3. Repeat. Occasionally Runtime.exceptionThrown reports the error above.

Expected. No uncaught error. For example, hookupOnLoadHandlers could skip a frame that is no longer attached, or the two updates could be coalesced.

Activity

  1. ayushkrsingh946 commented on Oct 28, 2026

    @ayushkrsingh946
    Contributor

    Thanks for the clear analysis and reproduction steps Sergio González Carbajal (@sergwei)!

    Cause

    When webview.options and webview.html are set back-to-back, two content update messages are dispatched to the webview host. The first message mounts an iframe (pending-frame) and navigates to ./fake.html. Once DOMContentLoaded fires, onFrameLoaded schedules document writing and handler setup via setTimeout(..., 0).

    When the second message arrives before or around the time that setTimeout fires, previousPendingFrame.remove() unmounts the first iframe to make way for the new pending frame. Once an iframe is disconnected from the DOM, its contentWindow becomes null. When the first frame's scheduled timeout executes, hookupOnLoadHandlers(newFrame) tries to access assertIsDefined(newFrame.contentWindow), throwing the uncaught Found unexpected null error.

    Proposed Fix

    We can guard onFrameLoaded's deferred callback and hookupOnLoadHandlers against unmounted frames (if (!newFrame.parentElement) { return; }), mirroring the existing check in the Safari polling branch (// If the frame is no longer mounted, loading has stopped). Similarly, the 200ms fallback timeout in hookupOnLoadHandlers should skip execution if the frame has been unmounted in the interim.

    I am preparing a PR with this fix.

  2. added
    stale
    on Nov 4, 2026
  3. removed
    stale
    on Nov 16, 2026
  4. added
    bug
    webview
    on Nov 17, 2026
  5. added
    unreleased
    on Nov 17, 2026
  6. added this to the 1.148.0 milestone on Nov 17, 2026
  7. added
    insiders-released
    and removed
    unreleased
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

buginsiders-releasedverifiedwebview

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions