Skip to content
Closed
11 changes: 7 additions & 4 deletions src/jsc/ConsoleObject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5484,10 +5484,13 @@ pub mod formatter {
self.reset_line();
}

let mut display_name = value.get_name(self.global_this)?;
if display_name.is_empty() {
display_name = BunString::static_("Object");
}
// `get_name` hands back a +1 WTFStringImpl ref.
let name = bun_core::OwnedString::new(value.get_name(self.global_this)?);
let display_name = if name.is_empty() {
BunString::static_("Object")
} else {
name.get()
};
let _ = write!(
writer_,
"{}[{} ...]{}",
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/test_runner/ScopeFunctions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,13 @@ fn get_description(
return Ok(description_class_name.to_owned_slice());
}

let description_name = description.get_name(global)?;
// `description_name.deref()` handled by Drop on bun_core::String
// `get_name` hands back a +1 WTFStringImpl ref.
let description_name = bun_core::OwnedString::new(description.get_name(global)?);
return Ok(description_name.to_owned_slice());
}

if description.is_function() {
let func_name = description.get_name(global)?;
let func_name = bun_core::OwnedString::new(description.get_name(global)?);
if func_name.length() > 0 {
return Ok(func_name.to_owned_slice());
}
Expand Down
12 changes: 7 additions & 5 deletions src/runtime/test_runner/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1607,7 +1607,8 @@ impl Expect {
// so now execute the symmetric matching

// retrieve the matcher name
let matcher_name = matcher_fn.get_name(global_this)?;
// `get_name` hands back a +1 WTFStringImpl ref.
let matcher_name = bun_core::OwnedString::new(matcher_fn.get_name(global_this)?);
Comment thread
robobun marked this conversation as resolved.
Resolved

let matcher_params = CustomMatcherParamsFormatter {
colors: Output::enable_ansi_colors_stderr(),
Expand All @@ -1625,7 +1626,7 @@ impl Expect {
expect.flags.get(),
global_this,
value,
matcher_name,
&matcher_name,
&matcher_params,
false,
)?;
Expand All @@ -1643,7 +1644,7 @@ impl Expect {
matcher_args.push(*arg);
}

let _ = Self::execute_custom_matcher(global_this, matcher_name, matcher_fn, &matcher_args, expect.flags.get(), false)?;
let _ = Self::execute_custom_matcher(global_this, matcher_name.get(), matcher_fn, &matcher_args, expect.flags.get(), false)?;

Ok(this_value)
}
Expand Down Expand Up @@ -2619,7 +2620,8 @@ impl ExpectCustomAsymmetricMatcher {
}

// retrieve the matcher name
let matcher_name = matcher_fn.get_name(global_this)?;
// `get_name` hands back a +1 WTFStringImpl ref.
let matcher_name = bun_core::OwnedString::new(matcher_fn.get_name(global_this)?);

// retrieve the asymmetric matcher args
// if null, it means the function has not yet been called to capture the args, which is a misuse of the matcher
Expand All @@ -2639,7 +2641,7 @@ impl ExpectCustomAsymmetricMatcher {
matcher_args.push(captured_args.get_index(global_this, i as u32)?);
}

Expect::execute_custom_matcher(global_this, matcher_name, matcher_fn, &matcher_args, this.flags, true)
Expect::execute_custom_matcher(global_this, matcher_name.get(), matcher_fn, &matcher_args, this.flags, true)
}

/// Function called by c++ function "matchAsymmetricMatcher" to execute the custom matcher against the provided leftValue
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/test_runner/jest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,8 +795,8 @@ pub(crate) fn format_label(
)?;
}
b'j' | b'o' => {
let mut str = bun_core::String::empty();
// `str` released by Drop.
// `json_stringify_fast` hands back a +1 WTFStringImpl ref.
let mut str = bun_core::OwnedString::default();
// Use jsonStringifyFast for SIMD-optimized serialization
current_arg.json_stringify_fast(global_this, &mut str)?;
let owned_slice = str.to_owned_slice();
Expand Down
7 changes: 5 additions & 2 deletions src/runtime/test_runner/pretty_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1793,7 +1793,8 @@ impl<'a> Formatter<'a> {
writer.write_all(b"\n");
}
Tag::JSON => {
let mut str = bun_core::String::empty();
// `json_stringify` hands back a +1 WTFStringImpl ref.
let mut str = bun_core::OwnedString::default();

value.json_stringify(self.global_this, self.indent, &mut str)?;
self.add_for_new_line(str.length());
Expand Down Expand Up @@ -2901,7 +2902,9 @@ impl JestPrettyFormat {
else {
return Ok(true);
};
let matcher_name = matcher_fn.get_name(this.amf_global_this())?;
// `get_name` hands back a +1 WTFStringImpl ref.
let matcher_name =
bun_core::OwnedString::new(matcher_fn.get_name(this.amf_global_this())?);
Comment thread
robobun marked this conversation as resolved.
Resolved

Self::print_asymmetric_matcher_promise_prefix(flags, this, writer);
if flags.not() {
Expand Down
16 changes: 16 additions & 0 deletions test/js/bun/test/jest-each-json-title-leak-fixture.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions test/js/bun/test/jest-each.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it } from "bun:test";
import { bunEnv, bunExe } from "harness";

const NUMBERS = [
[1, 1, 2],
Expand Down Expand Up @@ -57,3 +58,47 @@ describe.each(["some", "cool", "strings"])("works with describe: %s", s => {
describe("does not return zero", () => {
expect(it.each([1, 2])("wat", () => {})).toBeUndefined();
});

describe("%j title memory", () => {
// The %j/%o title formatter used to leak one WTFStringImpl copy of the
// stringified JSON per registered test (jest.rs format_label).
it("does not retain a second copy of each stringified title", async () => {
const fixture = import.meta.dir + "/jest-each-json-title-leak-fixture.ts";

async function rssWithRows(rows: number): Promise<number> {
await using proc = Bun.spawn({
cmd: [bunExe(), "test", fixture],
env: {
...bunEnv,
LEAK_FIXTURE_ROWS: String(rows),
// ASAN's quarantine keeps freed allocations in RSS; without this
// the fixed build measures the same as the leaky one. The override
// comes last because ASAN's option parsing is last-wins.
ASAN_OPTIONS: [bunEnv.ASAN_OPTIONS, "quarantine_size_mb=0", "thread_local_quarantine_size_kb=0"]
.filter(Boolean)
.join(":"),
},
stdout: "pipe",
stderr: "pipe",
});
const [out, err, code] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
const match = (out + err).match(/RSS_BYTES:(\d+)/);
if (!match) {
// Bounded tails: err is ~70MB of 512KB titles on a healthy run.
throw new Error(
`Expected RSS_BYTES marker. exit code: ${code}\nstdout tail:\n${out.slice(-2000)}\nstderr tail:\n${err.slice(-2000)}`,
);
}
Comment thread
robobun marked this conversation as resolved.
Resolved
expect(code).toBe(0);
return Number(match![1]);
Comment thread
robobun marked this conversation as resolved.
Resolved
}

// 128 extra rows x ~512KB stringified title: the titles themselves
// retain ~64MB in every build. The unfixed build leaked a second copy
// per title (~132MB delta measured on both release and debug-asan);
// the fixed build stays at ~66MB.
const small = await rssWithRows(8);
const large = await rssWithRows(136);
expect(large - small).toBeLessThan(100 * 1024 * 1024);
}, 120_000);
});
Loading