Files
bun.sh/test/regression/issue/26088.test.ts
robobun 6104705f5f fix(yaml): fix memory leak in YAML parser (#26090)
## Summary
- Fix memory leak in YAML parser that caused segfaults after high-volume
parsing
- Added `defer parser.deinit()` to free internal data structures
(context, block_indents, anchors, tag_handles, whitespace_buf)
- Fixes #26088

## Test plan
- [x] Added regression test at `test/regression/issue/26088.test.ts`
- [x] Verified YAML parsing still works correctly with debug build
- [x] Ran subset of YAML tests to confirm no regressions


🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-01-14 17:13:30 -08:00

28 lines
1.0 KiB
TypeScript

import { YAML } from "bun";
import { expect, test } from "bun:test";
// https://github.com/oven-sh/bun/issues/26088
// YAML parser was leaking memory on each parse call because AST nodes were
// not being freed. This caused segfaults after high-volume YAML parsing.
// Fix: Use ASTMemoryAllocator to ensure AST nodes are freed at end of scope.
test("YAML.parse shouldn't leak memory", () => {
// Create YAML with 10000 single-char strings - creates many AST E.String nodes
const items = Array.from({ length: 10000 }, () => " - x").join("\n");
const yaml = `list:\n${items}`;
Bun.gc(true);
const initialMemory = process.memoryUsage.rss();
// Parse 100 times - each creates 10000 AST string nodes
for (let i = 0; i < 100; i++) {
YAML.parse(yaml);
}
Bun.gc(true);
const finalMemory = process.memoryUsage.rss();
// Memory increase should be less than 50MB if AST nodes are freed properly
const memoryIncreaseMB = (finalMemory - initialMemory) / 1024 / 1024;
expect(memoryIncreaseMB).toBeLessThan(50);
});