Skip to content

[JSC] A module error does not keep the module record of its Evaluate() alive - #840

Open
robobun wants to merge 1 commit into
mainfrom
robobun/f48be7fb/module-error-does-not-keep-record
Open

robobun wants to merge 1 commit into
mainfrom
robobun/f48be7fb/module-error-does-not-keep-record

Conversation

@robobun

@robobun robobun commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

Problem

  • An error that a module throws keeps alive the module record of the Evaluate() that failed, and with it every module of that graph. In Bun, an Error that a program keeps from a failed node:vm graph of 23 modules holds 1,120 KB of heap. Without the record it holds 18 KB. An error of the host also keeps the whole vm context.
  • JSModuleLoader::attachErrorInfo (JSModuleLoader.cpp:217) stores the record on the error as a private property. getErrorInfo() reads it back into ModuleFailure::m_source, which nothing uses.

Fix

  • attachErrorInfo does not store the record. The key, the type and the kind stay.
  • Correct because ModuleFailure::isEvaluationError(), the only consumer, reads the key, the type and the kind.
  • Verified with a Bun debug build linked with this object: the numbers above, and Bun's vm-module-graph-evaluation*.test.ts, dynamic-import-evaluation-error-gc.test.ts and dynamic-import-tla-cycle.test.ts pass. No JSTests file: the registry of the jsc shell keeps every record alive.

Background

  • Evaluate() of a cyclic module record stamps an error that its walk throws (CyclicModuleRecord.cpp:619). The loader reads the stamp later to tell an evaluation error from a fetch error or a link error.
  • The module records of Bun's node:vm are in no registry. When the program drops its modules, nothing else keeps the records alive.

Downsides

  • None found. Checked the two callers of getErrorInfo() (JSMicrotask.cpp:1248 and :1439) and ModuleFailure::operator bool(), which stays true through the key and the kind.
Notes
  • The probe: 100 graphs, each a root that imports 10 modules with 100 KB of state, a module that throws, 10 modules with 100 KB of source that never run, and a vm.SyntheticModule whose callback never runs. The program keeps the 100 errors. With the record on the error: 109 MB of heap, and 100 of the 100 callbacks stay alive. Without it: 2 MB, and 0 of 100.
  • 20 vm contexts, each with a graph whose evaluateCallback throws an Error of the main realm, and the program keeps the errors: 20 of 20 contexts stay alive with the record, 0 of 20 without it.
  • Bun gets a node:vm test for this when it takes this change.

…) alive

JSModuleLoader::attachErrorInfo() stored the module record of the failing
Evaluate() or Link() on the error, as a private property. getErrorInfo()
read it back into ModuleFailure::m_source, which nothing uses:
ModuleFailure::isEvaluationError() looks at the key, the type and the kind.

An error that a program keeps therefore kept the record alive, and with it
every module of the graph that the record reaches, evaluated or not. For
the records of Bun's node:vm, which are in no registry, that was the only
thing that kept them alive.

The record is no longer stored. The key, the type and the kind are.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I reviewed this PR and didn't find any bugs; because the fix relies on nothing outside this tree (Bun's embedder code) reading the record private name back, a human look would still be worthwhile.

What was reviewed:

  • Traced every in-tree reader of the record: getErrorInfo() at JSModuleLoader.cpp:203 is the only one, and both of its callers (JSMicrotask.cpp:1248, :1439) only call isEvaluationError(), which uses key/type/kind.
  • Checked ModuleFailure::operator bool() (line 196): no in-tree caller uses it, and every attachErrorInfo call site that passed a record also passes a non-null key, so it would stay true anyway.
  • duplicateError/duplicateTypeError still copy the record name (lines 101, 145) — a harmless no-op on Bun builds since it is never written.
  • The #else branch preserves upstream behavior, matching the USE(BUN_JSC_ADDITIONS) convention.
Extended reasoning...

The diff is a nine-line change to one function, JSModuleLoader::attachErrorInfo in Source/JavaScriptCore/runtime/JSModuleLoader.cpp, that under USE(BUN_JSC_ADDITIONS) stops storing the AbstractModuleRecord on the error object as a private property, with the upstream putDirect kept in the #else branch. It touches no security-sensitive surface; it is a memory-retention fix for module evaluation errors. Within this tree the change is correct: the only reader of the private name feeds ModuleFailure, and the sole consumers use isEvaluationError(), which never looks at m_source. I did not approve because the correctness also depends on Bun's out-of-tree embedder not reading that private name (the PR author reports Bun's tests pass but that cannot be checked here), no JSTests regression test accompanies the change, and .github/CODEOWNERS assigns Source/JavaScriptCore to jsc-reviewers.

@github-actions

Copy link
Copy Markdown

Preview build of 7964380: autobuild-preview-pr-840-79643803

@coderabbitai

coderabbitai Bot commented Oct 18, 2026

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: cceb66de-5346-4caf-b6be-20a9a98f32b0
📥 Commits

Reviewing files that changed from the base of the PR and between 87ee10d and 7964380.

📒 Files selected for processing: 1
  • Source/JavaScriptCore/runtime/JSModuleLoader.cpp

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Walkthrough

attachErrorInfo now skips storing the source module record in Bun-additions builds. Other builds retain the existing behavior. Key, type, and failure-kind metadata handling is unchanged.

Changes

Module loader error metadata

Layer / File(s) Summary
Conditional source record storage
Source/JavaScriptCore/runtime/JSModuleLoader.cpp
Bun-additions builds mark source unused and do not store its module record. Other builds continue to store a non-null source record.

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 79643

This change stops Bun-additions builds from retaining the source module record on errors, intended to reduce retention for failed module graphs. No actionable merge-blocking issue is established by the available evidence.

Pre-merge checks: Passed 3 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Description check Warning The description gives a detailed problem statement, fix rationale, validation results, and changed behavior. However, it omits required template information, including a Bugzilla URL or bug number, a … Add the associated Bugzilla URL or bug number, include the required "Reviewed by NOBODY (OOPS!)." line or actual review information, and add the changed file and relevant function entries in the template format.
Passed Passed checks: 3 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: preventing a module error from keeping its evaluated module record alive.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description gives a detailed problem statement, fix rationale, validation results, and changed behavior. However, it omits required template information, including a Bugzilla URL or bug number, a review status line, and an explicit changed-file/function list.

Resolution

Add the associated Bugzilla URL or bug number, include the required "Reviewed by NOBODY (OOPS!)." line or actual review information, and add the changed file and relevant function entries in the template format.

  • Fix all pre-merge checks with AI
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use path_filters to narrow the review scope.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant