You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Webview view: "Found unexpected null" in hookupOnLoadHandlers when options and html are set back to back #344744
bugIssue identified by VS Code Team member as probable buginsiders-releasedPatch has been released in VS Code InsidersverifiedVerification succeededwebviewWebview issues
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:
Register a webview view whose resolveWebviewView does webview.options = { enableScripts: true, localResourceRoots: [...] } and then webview.html = ....
Attach to the view's webview target over CDP with Runtime.enable right after the view is revealed.
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.
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.
bugIssue identified by VS Code Team member as probable buginsiders-releasedPatch has been released in VS Code InsidersverifiedVerification succeededwebviewWebview issues
Activity
ayushkrsingh946 commentedon Oct 28, 2026
Thanks for the clear analysis and reproduction steps Sergio González Carbajal (@sergwei)!
Cause
When
webview.optionsandwebview.htmlare set back-to-back, twocontentupdate messages are dispatched to the webview host. The first message mounts an iframe (pending-frame) and navigates to./fake.html. OnceDOMContentLoadedfires,onFrameLoadedschedules document writing and handler setup viasetTimeout(..., 0).When the second message arrives before or around the time that
setTimeoutfires,previousPendingFrame.remove()unmounts the first iframe to make way for the new pending frame. Once an iframe is disconnected from the DOM, itscontentWindowbecomesnull. When the first frame's scheduled timeout executes,hookupOnLoadHandlers(newFrame)tries to accessassertIsDefined(newFrame.contentWindow), throwing the uncaughtFound unexpected nullerror.Proposed Fix
We can guard
onFrameLoaded's deferred callback andhookupOnLoadHandlersagainst 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 inhookupOnLoadHandlersshould skip execution if the frame has been unmounted in the interim.I am preparing a PR with this fix.
webview: skip load handlers when pending frame is unmounted (microsof…