Skip to content

Return a deep cloned VSBuffer when it might be transferred and then accessed again. - #320190

Closed
er91 (er91) wants to merge 2 commits into
microsoft:mainfrom
er91:patch-2
Closed

er91 (er91) wants to merge 2 commits into
microsoft:mainfrom
er91:patch-2

Conversation

@er91

@er91 er91 (er91) commented Jun 5, 2026 •

Copy link
Copy Markdown

Context

Fixes #316841

Summary

As described in #316841, if a VSBuffer object is transferred to an iframe or a web worker with window.postMessage() or webWorker.postMessage(), the backing store of its underlying Uint8Array will be detached. So the next time we call any instance method on the underlying Uint8Array (e.g. subarray()), an error will be thrown. Safari throws an error immediately in this case, while Chrome often does not throw because it detaches the Uint8Array asynchronously.

To fix this, we need to return a deep cloned VSBuffer if the return value might be transferred and accessed again in current context. This PR fixes 2 such cases, both in IPC related code (and observed in Safari).

This PR tries to minimize performance penalty by:

  1. Do not perform deep clone if the VSBuffer.buffer is a native nodejs Buffer object, because native nodejs Buffer object cannot (and should not) be transferred.
  2. Only perform deep clone when the VSBuffer might be transferred, and then accessed again in current context. If returned VSBuffer will not be accessed in any other way in current context, we do not perform the deep clone.

Testing

Tested with locally built VSCode server on Linux ARM64, and Lean 4 extension.

Without this PR, the Lean 4 extension's InfoView (which is a webview) can only be loaded properly in Chrome; Safari shows an empty webview, with 2 distinct errors in Javascript console:

Underlying ArrayBuffer has been detached from the view or out-of-bounds: subarray@[native code]
slice@http://localhost:8000/oss-dev/static/out/vs/base/common/buffer.js:103:45
_read@http://localhost:8000/oss-dev/static/out/vs/base/parts/ipc/common/ipc.net.js:133:44
acceptChunk@http://localhost:8000/oss-dev/static/out/vs/base/parts/ipc/common/ipc.net.js:249:43
_deliver@http://localhost:8000/oss-dev/static/out/vs/base/common/event.js:792:21
fire@http://localhost:8000/oss-dev/static/out/vs/base/common/event.js:817:20
@http://localhost:8000/oss-dev/static/out/vs/platform/remote/browser/browserSocketFactory.js:31:24
Underlying ArrayBuffer has been detached from the view or out-of-bounds: subarray@[native code]
slice@http://localhost:8000/oss-dev/static/out/vs/base/common/buffer.js:103:45
read@http://localhost:8000/oss-dev/static/out/vs/base/parts/ipc/common/ipc.js:104:37
deserialize@http://localhost:8000/oss-dev/static/out/vs/base/parts/ipc/common/ipc.js:178:27
onRawMessage@http://localhost:8000/oss-dev/static/out/vs/base/parts/ipc/common/ipc.js:253:31
_deliver@http://localhost:8000/oss-dev/static/out/vs/base/common/event.js:792:21
_deliverQueue@http://localhost:8000/oss-dev/static/out/vs/base/common/event.js:801:20
fire@http://localhost:8000/oss-dev/static/out/vs/base/common/event.js:821:25
fire@http://localhost:8000/oss-dev/static/out/vs/base/parts/ipc/common/ipc.net.js:445:27
_receiveMessage@http://localhost:8000/oss-dev/static/out/vs/base/parts/ipc/common/ipc.net.js:704:33
_deliver@http://localhost:8000/oss-dev/static/out/vs/base/common/event.js:792:21
fire@http://localhost:8000/oss-dev/static/out/vs/base/common/event.js:817:20
acceptChunk@http://localhost:8000/oss-dev/static/out/vs/base/parts/ipc/common/ipc.net.js:267:29
_deliver@http://localhost:8000/oss-dev/static/out/vs/base/common/event.js:792:21
fire@http://localhost:8000/oss-dev/static/out/vs/base/common/event.js:817:20
@http://localhost:8000/oss-dev/static/out/vs/platform/remote/browser/browserSocketFactory.js:31:24

With this PR, the webview can be loaded properly in Safari as well, and the 2 errors disappear.

Copilot AI review requested due to automatic review settings June 5, 2026 22:26

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

Note

Copilot was unable to run its full agentic suite in this review.

This PR hardens ChunkStream._read() against a scenario where callers transfer returned VSBuffers (e.g., via window.postMessage()), which can detach underlying buffers and make the stream’s retained chunks inaccessible.

Changes:

  • Documented why _read() sometimes needs to return deep clones instead of references/views.
  • Updated _read() fast paths to return clones when advance === false and returning data that aliases an internal chunk.
  • Added cloning in additional branches to avoid returning buffers that could be transferred while still needed internally.
Comments suppressed due to low confidence (1)

src/vs/base/parts/ipc/common/ipc.net.ts:207

  • The new comment explains callers may transfer the returned VSBuffer. Returning a shared singleton from getEmptyBuffer() means a transfer could detach that shared underlying buffer and affect subsequent callers. Consider returning a fresh empty buffer (or a clone) here to avoid shared-state detachment.
		if (byteCount === 0) {
			return getEmptyBuffer();
		}

Comment thread src/vs/base/parts/ipc/common/ipc.net.ts Resolved
Comment thread src/vs/base/parts/ipc/common/ipc.net.ts Resolved
Comment thread src/vs/base/parts/ipc/common/ipc.net.ts Resolved
@er91

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@er91 er91 (er91) changed the title Return a deep cloned VSBuffer when it might be accessed again. Return a deep cloned VSBuffer when it might be detached and then accessed again. Jun 8, 2026
@er91 er91 (er91) changed the title Return a deep cloned VSBuffer when it might be detached and then accessed again. Return a deep cloned VSBuffer when it might be transferred and then accessed again. Jun 11, 2026
@alexdima

Copy link
Copy Markdown
Member

Matt Bierner (@mjbvz) I have a potential smaller change in #321291

@er91

Copy link
Copy Markdown
Author

Closing this in favor of #321291

@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Jul 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

4 participants