Skip to content

ai slop#28154

Closed
robobun wants to merge 2 commits into
mainfrom
farm/10f1404f/fix-mimalloc-arena-heap-tag
Closed

ai slop#28154
robobun wants to merge 2 commits into
mainfrom
farm/10f1404f/fix-mimalloc-arena-heap-tag

Conversation

@robobun

@robobun robobun commented Mar 16, 2026

Copy link
Copy Markdown
Collaborator

This PR has been marked as AI slop and the description has been updated to avoid confusion or misleading reviewers.

Many AI PRs are fine, but sometimes they submit a PR too early, fail to test if the problem is real, fail to reproduce the problem, or fail to test that the problem is fixed. If you think this PR is not AI slop, please leave a comment.

MimallocArena.init() used mi_heap_new() which creates a heap with tag 0
— the same tag as the backing heap. When worker threads exit, mimalloc
routes their abandoned pages to the first heap matching the tag via
_mi_heap_by_tag. With tag 0, those pages get routed to a MimallocArena
instead of the backing heap. When mi_heap_destroy is later called on
that arena, it frees the reclaimed pages and their live blocks,
corrupting the malloc free-list.

Use mi_heap_new_ex with a non-zero tag (111) so abandoned pages from
dead threads are always routed to the backing heap (tag 0) where they
belong.

Closes #28153
@robobun

robobun commented Mar 16, 2026

Copy link
Copy Markdown
Collaborator Author
Updated 7:13 AM PT - Mar 16th, 2026

@autofix-ci[bot], your commit e6c1b36 has 4 failures in Build #39736 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 28154

That installs a local version of the PR into your bun-28154 executable, so you can run:

bun-28154 --bun

@robobun

robobun commented Mar 16, 2026

Copy link
Copy Markdown
Collaborator Author

✅ All local gates pass on e6c1b36b. CI still running (61/65 checks pending, 0 failures so far). Review threads resolved (0 unresolved). Waiting for CI to finish before posting final approval.

Gate summary:

  • Gate 1 (CI): 2/65 complete, 0 failures — still in progress
  • Gate 2 (Classification): Bug fix
  • Gate 3 (Test proof): PASS — baked binary fails on new test, PR binary passes
  • Gate 4 (Diff): PASS — no TODOs/FIXMEs, fix matches root cause
  • Gate 5 (Bot convergence): PASS — all threads resolved
  • Gate 6 (Hygiene): PASS

@coderabbitai

coderabbitai Bot commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

Replaces plain heap creation with a tagged mimalloc heap (tag 111) in the allocator and adds a regression test that exercises heap behavior during worker teardown and concurrent transpilation.

Changes

Cohort / File(s) Summary
Heap Allocator
src/allocators/MimallocArena.zig
Replaced plain heap creation with a non-zero tagged heap (arena_heap_tag = 111) via mimalloc.mi_heap_new_ex, preserving safety-check behavior and ensuring reclaimed pages from dead threads route to the backing heap.
Regression Test
test/regression/issue/28153.test.ts
Added new test that spawns workers sequentially while running a Bun.Transpiler transform, then allocates and verifies large buffers after worker teardown to validate heap correctness.
🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary fix: preventing heap corruption caused by mimalloc arena page misrouting through the use of a non-zero heap tag.
Description check ✅ Passed The pull request description comprehensively covers the crash symptom, root cause analysis, the fix implementation, testing approach, and verification results against the template requirements.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

📝 Coding Plan
  • Generate coding plan for human review comments

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can generate a title for your PR based on the changes with custom instructions.

Set the reviews.auto_title_instructions setting to generate a title for your PR based on the changes in the PR with custom instructions.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@test/regression/issue/28153.test.ts`:
- Around line 58-65: The spawn call for proc via Bun.spawn does not pipe stdout,
so proc.stdout will be null when later awaiting proc.stdout.text(); update the
Bun.spawn options used where proc is created to include stdout: "pipe"
(alongside stderr: "pipe") so proc.stdout is available, ensuring the Promise.all
that awaits proc.stdout.text(), proc.stderr.text(), and proc.exited succeeds.
- Around line 67-68: Update the assertions to include stderr in failure
diagnostics: when asserting stdout.trim() === "PASS" and exitCode === 0
(references to stdout.trim() and exitCode), make the assertion messages include
stderr so failures print useful debugging output (or explicitly
log/console.error stderr before/when the expect for exitCode fails); ensure you
reference the existing stderr variable so any non-zero exitCode or unexpected
stdout shows the stderr contents in the test output.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: fec708c1-062d-4861-a7e2-851d47eabfa7

📥 Commits

Reviewing files that changed from the base of the PR and between d50ab98 and 1e650f4.

📒 Files selected for processing (2)
  • src/allocators/MimallocArena.zig
  • test/regression/issue/28153.test.ts

Comment thread test/regression/issue/28153.test.ts
Comment thread test/regression/issue/28153.test.ts
@github-actions

Copy link
Copy Markdown
Contributor

Found 12 issues this PR may fix:

  1. Heap corruption (malloc free list) during long-running server with sharp + mongodb #27929 - Heap corruption with malloc free list during long-running server processes
  2. Segmentation fault after long-running session (v1.3.9-canary.51) #26984 - Segmentation fault after long-running session
  3. Segmentation fault (SIGILL) in long-running parallel async processes with high-frequency database operations #22998 - Segmentation fault in long-running parallel async processes with worker threads
  4. Segfault and DataCloneError with worker threads in oxfmt on Bun 1.3.5 #25610 - Segfault and DataCloneError with worker threads causing memory corruption
  5. [Windows] N-API vtable corruption causes segfault, TUI escape, and orphaned processes in Bun v1.3.10 #27471 - N-API vtable corruption causing segfaults from heap corruption
  6. Segmentation fault (address 0x28) on macOS Silicon after ~3min runtime when running Claude Code #27790 - Segmentation fault in Claude Code after sustained runtime
  7. Bun crashes in Claude Code cause Windows BSOD (KERNEL_SECURITY_CHECK_FAILURE) — full system reboots on Windows 11 x64 #27692 - Bun crashes in Claude Code causing severe system-level memory corruption
  8. Segmentation fault at address 0x10 when running Claude Code standalone executable #26843 - Segmentation fault in Claude Code standalone executable
  9. MessageChannel between Workers causes crash/unexpected exit #25805 - MessageChannel between Workers causing crashes during cleanup
  10. Bun crashes when using snarkjs for ZK proof generation/verification in worker threads #22376 - Worker thread crashes during intensive ZK proof operations
  11. Segfault (null pointer at 0x0) in long-running standalone executable on Windows after sustained memory pressure #26862 - Segfault in long-running executable after sustained memory pressure
  12. Segmentation fault at address 0x8 (After ~2 hours) #21002 - Segmentation fault after ~2 hours of sustained use

If this is helpful, consider adding Fixes #<number> to the PR description to auto-close the issue on merge.

🤖 Generated with Claude Code

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one-line fix looks correct — using mi_heap_new_ex(111, true, null) to assign a unique heap tag prevents abandoned pages from dead worker threads being misrouted to MimallocArena heaps — but since this touches low-level allocator internals where a mistake means heap corruption, a human should verify the root cause analysis and that tag 111 has no conflicts.

Extended reasoning...

Overview

This PR changes a single function call in src/allocators/MimallocArena.zig: mi_heap_new() is replaced with mi_heap_new_ex(arena_heap_tag, true, null) where arena_heap_tag = 111. A regression test is added in test/regression/issue/28153.test.ts. The total logic change is one line.

Security risks

The change is in memory allocator code. If the fix is wrong (e.g., tag 111 conflicts with another heap tag, or allow_destroy has unexpected side effects with the new API), it could introduce new heap corruption rather than fixing it. The API signature matches the extern declaration in mimalloc.zig:226 and the parameters look correct, but the stakes are high.

Level of scrutiny

This deserves careful human review despite its small size. Allocator internals are among the most safety-critical code in a runtime — a single wrong parameter could cause use-after-free, double-free, or heap corruption that only manifests under specific timing conditions. The PR description provides a detailed and plausible root cause analysis, but it should be verified by someone familiar with mimalloc's heap tagging and page reclamation internals.

Other factors

  • The PR has the claude label, indicating it was likely auto-generated, which increases the importance of human verification.
  • The regression test is acknowledged as best-effort (the real bug requires ~10 hours of sustained load to trigger), so the test alone doesn't provide strong confidence.
  • No other callers of mi_heap_new() exist in the codebase outside the mimalloc wrapper itself, so this is the only site that needed the fix.
  • The magic number 111 for the tag is arbitrary but should be confirmed not to collide with any other heap tags used in the codebase or by mimalloc internally.

@robobun robobun requested a review from dylan-conway March 16, 2026 12:50

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — single-line fix with correct mi_heap_new_ex signature, unique tag prevents abandoned page misrouting.

Extended reasoning...

Overview

This PR changes one line in src/allocators/MimallocArena.zig: replacing mi_heap_new() (which creates heaps with tag 0) with mi_heap_new_ex(111, true, null) (non-zero tag). A regression test is added at test/regression/issue/28153.test.ts.

Security risks

None. The change improves memory safety by preventing heap corruption from mimalloc page misrouting. The fix is purely defensive — it prevents abandoned pages from dead worker threads (tag 0) from being incorrectly reclaimed by MimallocArena heaps.

Level of scrutiny

This is a small, targeted allocator fix. The mi_heap_new_ex extern signature (heap_tag: c_int, allow_destroy: bool, arena_id: mi_arena_id_t) was verified against the Zig bindings. Tag 111 is unique — no other code in the repository uses mi_heap_new_ex. The allow_destroy=true preserves the existing mi_heap_destroy behavior in deinit(). CI passes across all platforms.

Other factors

All CodeRabbit review comments were false positives (Bun.spawn defaults stdout to pipe, not inherit) and were resolved. The regression test follows established patterns (tempDir, bunExe/bunEnv, await using, checking stdout before exit code). The root cause analysis in the PR description is thorough and matches the fix.

@github-actions

Copy link
Copy Markdown
Contributor

This PR has been closed because it was flagged as AI slop.

Many AI-generated PRs are fine, but this one was identified as having one or more of the following issues:

  • Fails to verify the problem actually exists
  • Fails to test that the fix works
  • Makes incorrect assumptions about the codebase
  • Submits changes that are incomplete or misleading

If you believe this was done in error, please leave a comment explaining why.

@github-actions github-actions Bot changed the title Fix heap corruption from mimalloc arena page misrouting ai slop Mar 16, 2026
@github-actions github-actions Bot closed this Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants