Repository navigation
Make VSBuffer#slice return an owned buffer (fixes #316841) - #316842
Amine (dop-amine) wants to merge 2 commits into
Conversation
Switch VSBuffer#slice from subarray (view) to slice (copy) so the returned buffer survives transfer-list detachment of the source's ArrayBuffer. VSBuffer#slice was changed to subarray in microsoft#76076 for performance; the resulting view shares its underlying ArrayBuffer with the source. When the source is later transferred to another context (e.g. the extension-host iframe via postMessage with a transfer list, as the browser-mode IPC layer does), WebKit/JavaScriptCore throws synchronously on any subsequent view access per ECMA-262 §25.1.2.1. V8 is permissive, masking the bug on Chromium/Electron and exposing it on Safari and iOS — producing permanently blank webview panels for every webview-heavy extension on every Safari/iOS client of browser-mode VS Code (vscode.dev, code-server, code serve-web, Codespaces). Adds a regression test that detaches the source ArrayBuffer after slicing (via structuredClone with a transfer list) and asserts the slice remains readable. Full diagnosis, candidate fixes ranked by blast radius, and production validation across three independent code-server deployments in microsoft#316841. Co-authored-by: Cursor <[email protected]>
There was a problem hiding this comment.
Pull request overview
Fixes a Safari/iOS regression where webview-heavy extensions render blank in browser-mode VS Code. The IPC layer (ChunkStream._read) hands sliced VSBuffer chunks to the extension host via postMessage with a transfer list, which detaches the underlying ArrayBuffer; because VSBuffer#slice returned a view sharing that buffer, subsequent reads in the queued residual chunk threw on WebKit (V8 is permissive, masking the issue elsewhere). The fix switches slice to return a copy and adds a regression test that detaches the source via structuredClone and verifies the slice remains readable.
Changes:
- Replace
this.buffer.subarray(...)withthis.buffer.slice(...)inVSBuffer#sliceso the returned buffer owns its storage. - Rewrite the accompanying comment to document the WebKit detachment failure mode and reference #316841.
- Add a unit test that detaches the source
ArrayBufferand asserts the slice survives.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/vs/base/common/buffer.ts | Make VSBuffer#slice return a copy and document the rationale. |
| src/vs/base/test/common/buffer.test.ts | Regression test verifying a slice survives detachment of the source's ArrayBuffer. |
| // This causes webview-heavy extensions to render blank on every | ||
| // Safari / iOS client of browser-mode VS Code (vscode.dev, code-server, | ||
| // code serve-web, Codespaces). See #316841 for the full diagnosis. | ||
| return new VSBuffer(this.buffer.slice(start, end)); |
There was a problem hiding this comment.
VSBuffer.alloc / .wrap / .fromString wrap a Node Buffer whenever hasBuffer is true (lines 30–67 of this file), and Buffer.prototype.slice is a deprecated alias for subarray that returns a view, not a copy. The fix would have worked on the browser-mode deployment target where the original bug surfaces (no Buffer in the browser, so this.buffer is always a plain Uint8Array and Uint8Array.prototype.slice is spec-defined to copy) — but the regression test I just added would fail under the Node unit-test runner, where VSBuffer.alloc(1024) produces a Buffer and source.buffer.slice(0, 512) aliases the same ArrayBuffer the test detaches via structuredClone.
Fixed in 10b54f1. return new VSBuffer(new Uint8Array(this.buffer.subarray(start, end)));. new Uint8Array(typedArray) is spec'd to copy into fresh ArrayBuffer storage per ECMA-262 §23.2.5.1 step 12 regardless of whether the source is a Buffer or a plain Uint8Array, so the returned buffer always owns its storage. Comment expanded to credit the original 2019 caveat and explain why the naive slice swap is unsafe.
Switch the slice implementation from `this.buffer.slice(start, end)` to `new Uint8Array(this.buffer.subarray(start, end))`. In Node/Electron, VSBuffer.alloc / .wrap / .fromString may wrap a Node Buffer (when `hasBuffer` is true). `Buffer.prototype.slice` is a deprecated alias for `Buffer.prototype.subarray` and returns a view, not a copy — exactly the caveat the 2019 perf comment was guarding against. That would have left the contract violation in place in Node code paths and very likely caused the new regression test to fail under the Node unit-test runner (where `VSBuffer.alloc(1024)` produces a Buffer, and `source.buffer.slice(0, 512)` aliases the same ArrayBuffer the test detaches via structuredClone). `new Uint8Array(typedArray)` is spec'd to copy into a fresh ArrayBuffer per ECMA-262 §23.2.5.1 step 12, regardless of whether the source is a Buffer or a plain Uint8Array, so the returned VSBuffer always owns its storage. Comment expanded to credit the original 2019 caveat and document why the naive `slice` swap is unsafe. Thanks to copilot-pull-request-reviewer for the catch. Co-authored-by: Cursor <[email protected]>
|
@microsoft-github-policy-service agree |
|
This is what slice is meant to do. If there's an issue related to transfers, it should be fixed where the transfer happens |
|
Matt Bierner (@mjbvz) Do you mind taking a look at #320190, which fixes where the transfer happens? Thanks! |
Summary
Make
VSBuffer#slicereturn an owned buffer instead of a view, fixing webview-heavy extensions rendering blank on Safari / iOS clients of browser-mode VS Code.Fixes #316841.
Change
src/vs/base/common/buffer.ts— switchVSBuffer#slicefromthis.buffer.subarray(start, end)tothis.buffer.slice(start, end). The returnedVSBuffernow owns its underlyingArrayBufferinstead of sharing the source's, so transfer-list detachment of the source no longer invalidates it. Comment rewritten to document why.src/vs/base/test/common/buffer.test.ts— regression test that detaches the sourceArrayBufferafter slicing (viastructuredClone(_, { transfer: [_] })) and asserts the slice remains readable. Fails on the currentmain, passes after this change.Approach selection
This is candidate fix (A) from the ranked options in #316841 — the simplest and broadest. (B) keeps the view-returning optimization on desktop Electron by splitting browser / node implementations; (C) copies at the IPC reader (
ChunkStream._read) instead of inVSBuffer#slice, leaving the contract violation lurking elsewhere.I went with (A) because:
memcpyper slice call; sub-kilobyte typical IPC message sizes; not measurable above noise in a sustained extension-host workload (Claude Code session + multiple notebook renderers running concurrently).slicereturns an independent buffer, which is what most callers actually expect.Happy to rewrite as (B) or (C) if maintainers prefer; the trade-offs are spelled out in the issue. Opening as Draft RFC to invite that conversation early rather than committing to defend (A).
Validation
issue #316841 - VSBuffer#slice returns an independent buffer that survives detachment of the source) added, fails onmain, passes after change.workbench.jsin production has been running across three independent code-server deployments for ~24h with no observable side effects or perf regressions on V8 clients. On WebKit clients (Safari macOS / iPadOS, iOS browsers) it converts a 100%-blank-panel failure mode into a fully-working one.npm install+npm run watch+ the buffer test suite — pending; disk is tight on the machine I drafted on. CI on this PR will run the unit tests and catch anything I missed; I'll un-draft once it's green (or refactor in response to maintainer feedback, whichever comes first).Cross-references
Made with Cursor