Bun Crash: Reproducing StructuredClone Issue
Introduction
In the realm of JavaScript development, the structuredClone function is a powerful tool for creating deep copies of objects. However, as with any complex feature, it's not immune to occasional hiccups. This article delves into a specific crash encountered in Bun, a fast JavaScript runtime, when using structuredClone. We'll dissect the issue, explore how to reproduce it, and discuss the underlying causes and potential solutions. Understanding these intricacies not only aids in resolving the immediate problem but also enhances our comprehension of JavaScript runtime behavior and object cloning mechanisms.
Understanding structuredClone
The structuredClone method creates deep copies of JavaScript values. Unlike shallow copies, which only duplicate the top-level structure, deep copies recursively clone all nested objects and arrays. This is particularly useful when you need to ensure that modifications to the copy do not affect the original value. However, structuredClone has limitations; it cannot clone functions, DOM nodes, or certain other types of objects. When it encounters such an object, it throws a DataCloneError. In this context, we will analyze a specific scenario where structuredClone leads to a crash in the Bun runtime, shedding light on the error's nature and potential resolutions.
Reproducing the Crash in Bun
The crash occurs in Bun version 1.2.22 (Windows x64) when attempting to clone a mocked function using structuredClone. Here’s the code snippet that triggers the issue:
import { mock } from 'bun:test';
const fn = mock(() => { });
const deep = structuredClone(fn);
This code imports the mock function from the bun:test module, creates a mocked function fn, and then attempts to create a deep copy of it using structuredClone. Executing this code in Bun v1.2.22 results in a DataCloneError and a subsequent crash.
The error message indicates that the object cannot be cloned, which is consistent with the limitations of structuredClone. However, the crash suggests a deeper issue within Bun's implementation of structuredClone or its interaction with mocked functions. To fully grasp the impact, we must scrutinize the error logs and stack traces, which furnish vital cues for pinpointing the source of the crash.
Analyzing the Error Logs and Stack Trace
When the code is executed, the following error message is displayed:
DataCloneError: The object can not be cloned.
DOMException {
line: 3,
column: 29,
sourceURL: "s:\\eram\\mcp-agg\\script\\Untitled.ts",
stack: "structuredClone@[native code]\nmodule code@s:\\eram\\mcp-agg\\script\\Untitled.ts:3:29\nmoduleEvaluation@\nmoduleEvaluation@\n@",
code: 25,
name: "DataCloneError",
message: "The object can not be cloned.",
INDEX_SIZE_ERR: 1,
DOMSTRING_SIZE_ERR: 2,
HIERARCHY_REQUEST_ERR: 3,
WRONG_DOCUMENT_ERR: 4,
INVALID_CHARACTER_ERR: 5,
NO_DATA_ALLOWED_ERR: 6,
NO_MODIFICATION_ALLOWED_ERR: 7,
NOT_FOUND_ERR: 8,
NOT_SUPPORTED_ERR: 9,
INUSE_ATTRIBUTE_ERR: 10,
INVALID_STATE_ERR: 11,
SYNTAX_ERR: 12,
INVALID_MODIFICATION_ERR: 13,
NAMESPACE_ERR: 14,
INVALID_ACCESS_ERR: 15,
VALIDATION_ERR: 16,
TYPE_MISMATCH_ERR: 17,
SECURITY_ERR: 18,
NETWORK_ERR: 19,
ABORT_ERR: 20,
URL_MISMATCH_ERR: 21,
QUOTA_EXCEEDED_ERR: 22,
TIMEOUT_ERR: 23,
INVALID_NODE_TYPE_ERR: 24,
DATA_CLONE_ERR: 25,
toString: [Function: toString],
}
at s:\eram\mcp-agg\script\Untitled.ts:3
Bun v1.2.22 (Windows x64)
This error confirms that structuredClone cannot clone the mocked function, resulting in a DataCloneError. However, the stack trace provides additional context. It shows that the error originates from the structuredClone function called in the script. This is an anticipated outcome, aligning with the acknowledged constraints of structuredClone concerning functions.
Further down, the stack trace reveals a more severe issue:
Bun v1.2.22 ([`6bafe26`](<https://github.com/oven-sh/bun/tree/6bafe2602e9a24ad9072664115bc7fd195cf3211>)) on windows x86_64 [TestCommand]
**panic**: index out of bounds: index 1, len 0
- [`CallFrame.zig:25`](<https://github.com/oven-sh/bun/blob/6bafe2602e9a24ad9072664115bc7fd195cf3211/src/bun.js/bindings/CallFrame.zig#L25>): `argumentsCount`
- *2 unknown/js code*
- `llint_entry`
This output indicates a panic within Bun's internals, specifically an index out of bounds error. This error occurs in the CallFrame.zig file at line 25, which is part of Bun's JavaScript bindings. The error message