Skip to content

Fix webview resource loading detach in Safari/WebKit - #321291

Merged
Alexandru Dima (alexdima) merged 1 commit into
mainfrom
alexdima/webview-resource-chunk-detach
Jun 15, 2026
Merged

Alexandru Dima (alexdima) merged 1 commit into
mainfrom
alexdima/webview-resource-chunk-detach

Conversation

@alexdima

@alexdima Alexandru Dima (alexdima) commented Jun 13, 2026 •

Copy link
Copy Markdown
Member

fyi Matt Bierner (@mjbvz) this is for #316841 -- looks like if this code got ahold of a slice of a larger buffer, it would send off the entire buffer... this would cause other code to throw when it tried to use other pieces of the larger buffer.


🤖

Context

Fixes the Underlying ArrayBuffer has been detached from the view or out-of-bounds: subarray error seen in web/remote mode on Safari/WebKit.

Related: #316841, #314126

Problem

When transferable streams are unsupported (Safari/WebKit), webview resource chunks are sent via postMessage with a transfer list. The chunk was wrapped as a Uint8Array view over its entire underlying ArrayBuffer, and that whole ArrayBuffer was transferred:

const data = new Uint8Array(chunk.buffer.buffer, chunk.buffer.byteOffset, chunk.buffer.byteLength);
this._send('did-load-resource-chunk', { id, data }, [data.buffer]);

In web/remote mode the chunk is frequently a view aliasing a larger, shared ArrayBuffer (e.g. a message buffer sliced into many views by the IPC deserialize pipeline). Transferring that shared buffer detaches every sibling view still in use. WebKit detaches synchronously, so the next IPC read throws Underlying ArrayBuffer has been detached from the view and webview resource loading breaks. V8 (Chrome/Edge/desktop) detaches lazily, which is why this reproduces on Safari/iOS only.

Fix

Copy the chunk into a freshly-owned ArrayBuffer (sized to exactly the chunk window) before transferring it, so the transfer can only ever detach the copy:

const data = chunk.buffer.slice();
this._send('did-load-resource-chunk', { id, data }, [data.buffer]);

Uint8Array.prototype.slice returns an exact-window copy backed by its own ArrayBuffer, leaving the shared source intact.

When transferable streams are unsupported (Safari/WebKit), webview
resource chunks are sent via postMessage with a transfer list. The
chunk was wrapped as a view over its underlying ArrayBuffer and that
whole ArrayBuffer was transferred. In web/remote mode the chunk can be
a view aliasing a larger, shared buffer (e.g. from the IPC deserialize
pipeline), so transferring it detached every sibling view. WebKit
detaches synchronously, throwing "Underlying ArrayBuffer has been
detached from the view" and breaking webview resource loading.

Copy the chunk into a freshly-owned ArrayBuffer (sized to exactly the
chunk window) before transferring it.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings June 13, 2026 23:35

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.

Pull request overview

Fixes a Safari/WebKit-specific failure in webview resource loading where transferring a shared underlying ArrayBuffer via postMessage can synchronously detach sibling views, triggering Underlying ArrayBuffer has been detached from the view or out-of-bounds: subarray in web/remote mode.

Changes:

  • In the Safari fallback path (when transferable streams are unsupported), copy each resource chunk into a newly-owned buffer via Uint8Array#slice() before transferring it.
  • Add detailed inline commentary explaining why the copy is required on WebKit (synchronous detachment behavior).
Show a summary per file
File Description
src/vs/workbench/contrib/webview/browser/webviewElement.ts Copies chunk data before transferring in Safari fallback to avoid detaching shared IPC buffers and breaking resource loading.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

@er91

er91 (er91) commented Jun 14, 2026 •

Copy link
Copy Markdown

Verified that this PR fixes the 2 distinct error paths in Safari, and VSCode extension webview works properly with it.

This PR seems more surgical and fixes both error paths with one change :-)

Closing #320190 in favor of this.

@alexdima

Copy link
Copy Markdown
Member Author

Given the feedback from er91 (@er91) let's get this in for testing.

@alexdima
Alexandru Dima (alexdima) enabled auto-merge (squash) June 14, 2026 21:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants