Repository navigation
[JSC] Tolerate cross-DFS Evaluating module records on require(esm) re-entry - #204
Conversation
…re-enters Bun's require(esm) is synchronous and can re-enter the loader from inside a module that is currently mid-evaluation. The strict spec state machine assumes a single DFS for both Link and Evaluate, so a nested DFS that visits a module already Evaluating in an outer DFS triggers debug assertions and, in release, downgrades the outer module's status to Linked, wiping its in-flight bindings. Three small, USE(BUN_JSC_ADDITIONS)-guarded relaxations on AbstractModuleRecord: - innerModuleLinking step 2: also early-return when the dep is Evaluating. The module is already linked, so re-link must be a no-op; otherwise the linearization loop downgrades it to Linked. - innerModuleLinking step 9.c.i: extend the assert symmetrically. - innerModuleEvaluation step 11.c: when a dep is Evaluating but not on the local stack, it belongs to an outer DFS's SCC. Skip 11.c.i/ii/iii/iv/v entirely so we don't crash on the unset cycleRoot and don't merge the outer module's DFSAncestorIndex into the inner SCC. The outer DFS owns its lifecycle; the inner pass treats it as a satisfied dependency. No spec invariant change for purely-ESM workloads (the new state is unreachable without re-entry from synchronous require).
WalkthroughUpdated AbstractModuleRecord.cpp with Bun-specific handling for nested Changes
🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Review rate limit: 4/5 reviews remaining, refill in 12 minutes. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp`:
- Around line 1256-1262: The module status check in AbstractModuleRecord.cpp
currently treats Status::Evaluating (and the related relaxations) as allowed for
all builds; restrict these Bun-only relaxations by guarding them with the build
flag USE(BUN_JSC_ADDITIONS). Update the if-condition around module->status()
(the occurrence that checks Status::Linking, Status::Linked, Status::Evaluating,
Status::EvaluatingAsync, Status::Evaluated) to only include Status::Evaluating
when compiled with USE(BUN_JSC_ADDITIONS), and apply the same conditional guard
to the second similar block later (the one around the
cyclic->status(Status::Linked) linearization). Ensure you use the preprocessor
guard pattern so non-Bun builds keep the original strict set (without
Status::Evaluating).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: b6dc4545-7c87-42b3-b975-1d9e6fd1fb24
📒 Files selected for processing (1)
Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp
| // 2. If module.[[Status]] is one of LINKING, LINKED, EVALUATING, EVALUATING-ASYNC, or EVALUATED, then | ||
| // Bun extension: EVALUATING is reachable when require(esm) re-enters an outer module | ||
| // that is currently mid-evaluation. Such a module is already linked, so re-link is a | ||
| // no-op; we MUST early-return here, otherwise the linearization loop below would call | ||
| // cyclic->status(Status::Linked) and downgrade the evaluating module, wiping its | ||
| // in-flight bindings. | ||
| if (auto status = module->status(); status == Status::Linking || status == Status::Linked || status == Status::Evaluating || status == Status::EvaluatingAsync || status == Status::Evaluated) { |
There was a problem hiding this comment.
Guard Bun-only Status::Evaluating relaxations with USE(BUN_JSC_ADDITIONS).
These two relaxations are documented as Bun re-entry behavior, but they currently apply to all builds. That broadens non-Bun behavior and reduces invariant/debug-signal strictness in generic JSC linking paths.
Suggested patch
- if (auto status = module->status(); status == Status::Linking || status == Status::Linked || status == Status::Evaluating || status == Status::EvaluatingAsync || status == Status::Evaluated) {
+ if (auto status = module->status(); status == Status::Linking || status == Status::Linked
+#if USE(BUN_JSC_ADDITIONS)
+ || status == Status::Evaluating
+#endif
+ || status == Status::EvaluatingAsync || status == Status::Evaluated) {
// 2.a. Return index.
return index;
}
@@
- ASSERT_UNUSED(status, status == Status::Linking || status == Status::Linked || status == Status::Evaluating || status == Status::EvaluatingAsync || status == Status::Evaluated);
+ ASSERT_UNUSED(status, status == Status::Linking || status == Status::Linked
+#if USE(BUN_JSC_ADDITIONS)
+ || status == Status::Evaluating
+#endif
+ || status == Status::EvaluatingAsync || status == Status::Evaluated);As per coding guidelines: "Source/JavaScriptCore/**/*.{cpp,h}: Guard Bun-specific features with USE(BUN_JSC_ADDITIONS) and event-loop integration with USE(BUN_EVENT_LOOP)".
Also applies to: 1293-1296
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp` around lines 1256 -
1262, The module status check in AbstractModuleRecord.cpp currently treats
Status::Evaluating (and the related relaxations) as allowed for all builds;
restrict these Bun-only relaxations by guarding them with the build flag
USE(BUN_JSC_ADDITIONS). Update the if-condition around module->status() (the
occurrence that checks Status::Linking, Status::Linked, Status::Evaluating,
Status::EvaluatingAsync, Status::Evaluated) to only include Status::Evaluating
when compiled with USE(BUN_JSC_ADDITIONS), and apply the same conditional guard
to the second similar block later (the one around the
cyclic->status(Status::Linked) linearization). Ensure you use the preprocessor
guard pattern so non-Bun builds keep the original strict set (without
Status::Evaluating).
Preview Builds
|
Background
Bun's
require(esm)is a synchronous CommonJS-style entry point into the ESM loader. When called from inside a module that is currently mid-evaluation (Status::Evaluating), it kicks off a freshloadModuleSyncpipeline — i.e. a new DFS — for the requested module. Because the registry is shared, that nested DFS can transitively reach the outer module that is currently evaluating.The ECMA-262 module record state machine implicitly assumes a single DFS for both Link and Evaluate. Two invariants in
AbstractModuleRecord.cppencode that assumption:innerModuleLinking: the early-return list{Linking, Linked, EvaluatingAsync, Evaluated}does not includeEvaluating. AStatus::Evaluatingreached during link falls through toASSERT(status == Unlinked). In release builds the assert is dropped and the linearization loop ends up callingcyclic->status(Status::Linked), downgrading the still-evaluating outer module toLinkedand wiping its in-flight bindings.innerModuleEvaluationstep 11.c.ii:ASSERT(stack.contains(requiredModule) == (cyclic->status() == Status::Evaluating)). In nested DFS the outer-SCC module isEvaluatingbut is on the outer stack, not the local one — so the equivalence breaks.These two failure modes have the same root cause:
require(esm)legitimately produces a(status == Evaluating) && !stack.contains(self)configuration that the spec language never anticipates.Repro
Pre-patch (debug):
ASSERTION FAILED: module->status() == Status::UnlinkedininnerModuleLinking. Pre-patch (release):Object.keys(b).length === 0andtypeof b.foo === 'undefined'becausea.tswas downgraded toLinkedmid-evaluation.Fix
Three small,
USE(BUN_JSC_ADDITIONS)-guarded relaxations:innerModuleLinkingstep 2 — addStatus::Evaluatingto the early-return predicate. AnEvaluatingmodule is already linked; re-linking is a no-op and re-linking is unsafe (the linearization loop would downgradestatustoLinked).innerModuleLinkingstep 9.c.i — extend the post-recursionASSERT_UNUSEDsymmetrically.innerModuleEvaluationstep 11.c — wrap the entire(11.c.i .. 11.c.v)body inif (!depInOuterSCC), wheredepInOuterSCC = (status == Evaluating && !stack.contains(requiredModule)). The outer DFS owns the module's lifecycle; the inner DFS treats it as a satisfied dependency (nodfsAncestorIndexmerge, nocycleRoot()access —cycleRootisn't set until the outer DFS pops it from its stack — and no async-parent registration).Both relaxations preserve the
⟸direction of the spec invariant ("on stack ⇒ Evaluating"); they only relax the⟹direction ("Evaluating ⇒ on stack"), which is the half synchronous re-entry breaks.Behavioural guarantees
(status == Evaluating) && !stack.contains(self)is unreachable, so the new code paths are dead and behaviour is bit-identical.EvaluatingAsyncrecords are unaffected (handled by the existing!cyclic->hasTLA()gate inZig::GlobalObject's sync-load path).const/letfrom an inner-DFS module body still throwsReferenceErrorper spec, exactly as on the pre-rewrite loader.Verification
[a] b namespace keys: 1 foo: function/[b] foo called.test/js/bun/resolve/require-esm*(4 files): 7 pass / 0 fail.test/cli/run/run-cjs.test.ts,esm-defineProperty.test.ts,node/module/require-extensions.test.ts,regression/issue/22929,regression/issue/12910: 17 pass / 0 fail.test/js/bun/resolve/directory full pass aside from a pre-existing debug-build timeout flake (load-same-js-file-a-lot.test.ts, two cases) reproducible without this patch.Notes for review
#if USE(BUN_JSC_ADDITIONS)guarded.get-module-namespace-bfswork — that touchesresolveExportImpl/getModuleNamespace, which are disjoint functions.