Repository navigation
structured clone - #3637
Merged
Merged
structured clone#3637
Conversation
Contributor
|
✅ |
Jarred-Sumner
approved these changes
Jul 15, 2023
Contributor
|
❌ @dylan-conway 7 files with test failures on bun-darwin-aarch64:
|
Contributor
|
❌ @dylan-conway 4 files with test failures on linux-x64-baseline:
|
Contributor
|
❌ @dylan-conway 4 files with test failures on linux-x64:
|
Contributor
|
❌ @dylan-conway 7 files with test failures on bun-darwin-x64-baseline:
|
Jarred-Sumner
pushed a commit
that referenced
this pull request
Jun 27, 2026
) ## Summary Serializing a graph that contains a detached `ArrayBuffer` threw a plain `TypeError` instead of a `"DataCloneError"` `DOMException`. The [HTML structured serialize algorithm](https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeinternal) requires `DataCloneError` here, and Node, Chrome, Firefox, and Safari all throw it. Code that branches on `DataCloneError` (the standard "unclonable value" signal, e.g. retry-without-transfer fallbacks) takes the wrong branch on Bun. ## Reproduction ```js const ab = new ArrayBuffer(8); structuredClone(ab, { transfer: [ab] }); // detach it try { structuredClone({ ab }); } catch (e) { console.log(e.name); } // Bun: "TypeError" Node / browsers: "DataCloneError" ``` Before the fix: ``` TypeError: Unable to deserialize data. ``` ## Cause The detached check in `CloneSerializer::dumpIfTerminal` set `SerializationReturnCode::ValidationError`, which `maybeThrowExceptionIfSerializationFailed` maps to a `TypeError`: ```cpp if (auto* arrayBuffer = toPossiblySharedArrayBuffer(vm, obj)) { if (arrayBuffer->isDetached()) { code = SerializationReturnCode::ValidationError; // -> TypeError ``` Upstream WebKit uses `DataCloneError` at this exact site. Bun's port of `SerializedScriptValue.cpp` (#3637, 2023) predates that upstream change and kept the old code. ## Fix Set `SerializationReturnCode::DataCloneError` instead. One line; no wire format change. This covers every entry point that shares the serializer: `structuredClone`, `Worker`/`MessagePort` `postMessage`, `bun:jsc` `serialize`, and `node:v8` `serialize`. Detached ArrayBuffers reached through a typed array or `DataView` already threw `DataCloneError` (they hit the `isOutOfBounds` check first), as did a detached buffer passed in the transfer list; only a direct reference to a detached `ArrayBuffer` in the serialized graph was wrong. Not changed: a `MessagePort` that appears in the graph but not in the transfer list still throws `TypeError`. That path also uses `ValidationError`, but it matches current upstream WebKit, so it is out of scope here. ## Verification The new test in `test/js/web/workers/structured-clone.test.ts` serializes a detached `ArrayBuffer` bare, inside an object, inside an array, and inside a `Map`, and asserts the thrown value is a `DOMException` named `DataCloneError` with `code === DOMException.DATA_CLONE_ERR`. <details> <summary>Before (unfixed) and after</summary> ``` # USE_SYSTEM_BUN=1 bun test structured-clone.test.ts -t "Serializing a detached ArrayBuffer" error: expect(received).toBeInstanceOf(expected) Expected constructor: [class DOMException] TypeError: Unable to deserialize data. (fail) structuredClone > transferables > Serializing a detached ArrayBuffer throws DataCloneError # bun bd test structured-clone.test.ts 162 pass, 0 fail ``` </details>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.