Compare commits

...

1 Commits

Author SHA1 Message Date
Jarred Sumner
87617d4a17 Fix slow memory leak in console.trace() 2024-08-15 23:16:00 -07:00
4 changed files with 31 additions and 2 deletions

View File

@@ -633,8 +633,8 @@ pub fn writeTrace(comptime Writer: type, writer: Writer, global: *JSGlobalObject
var source_code_slice: ?ZigString.Slice = null;
defer if (source_code_slice) |slice| slice.deinit();
var err = ZigString.init("trace output").toErrorInstance(global);
err.toZigException(global, exception);
const err = ZigString.init("trace output").toErrorInstance(global);
// remapZigException will call .toZigException() on the ErrorInstance.
vm.remapZigException(
exception,
err,

View File

@@ -383,6 +383,7 @@ pub const Msg = struct {
pub fn fromJS(allocator: std.mem.Allocator, globalObject: *bun.JSC.JSGlobalObject, file: string, err: bun.JSC.JSValue) !Msg {
var zig_exception_holder: bun.JSC.ZigException.Holder = bun.JSC.ZigException.Holder.init();
defer zig_exception_holder.deinit(globalObject.bunVM());
if (err.toError()) |value| {
value.toZigException(globalObject, zig_exception_holder.zigException());
} else {

View File

@@ -0,0 +1,19 @@
for (let i = 0; i < 1024; i++) {
eval(`console.trace();`);
}
Bun.gc(true);
const baseline = process.memoryUsage.rss();
for (let j = 0; j < 1024; j++) {
for (let i = 0; i < 1024; i++) {
eval(`console.trace();`);
}
}
Bun.gc(true);
const delta = ((process.memoryUsage.rss() - baseline) / 1024 / 1024) | 0;
console.error(delta, "MB", "delta");
// on macOS in a debug build, this was 124 MB after fixing the leak.
// on macOS in a leaking release build, this was 239 MB.
if (delta > 200) {
throw new Error("Memory leak detected");
}

View File

@@ -0,0 +1,9 @@
import { test, expect, describe, it } from "bun:test";
import "harness";
import path from "node:path";
describe("console.trace leak", () => {
test("should not leak", async () => {
expect(["--smol", path.join(import.meta.dir, "console-trace-leak-fixture.js")]).toRun();
// This is a lot slower in a debug build.
}, 60_000);
});