diff --git a/src/shell/shell.zig b/src/shell/shell.zig index 39c9de5236..7f773b806f 100644 --- a/src/shell/shell.zig +++ b/src/shell/shell.zig @@ -72,7 +72,17 @@ pub const ShellErr = union(enum) { } pub fn throwJS(this: *const @This(), globalThis: *JSC.JSGlobalObject) bun.JSError { - defer this.deinit(bun.default_allocator); + defer { + // basically `transferToJS`. don't want to double deref the sys error + switch (this.*) { + .sys => { + // sys.toErrorInstance handles decrementing the ref count + }, + .custom, .invalid_arguments, .todo => { + this.deinit(bun.default_allocator); + }, + } + } switch (this.*) { .sys => { const err = this.sys.toErrorInstance(globalThis);