-
Notifications
You must be signed in to change notification settings - Fork 4.7k
mock.module: validate callback before running resolver #28946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟣 Pre-existing bug in the
getJSValuelambda (same function this PR modifies): thewhileloop that unwraps Promise chains has no escape path for a Pending promise —default: { break; }exits only theswitch, not thewhile, so whenexecuteOncereturns a pending Promise the loop busy-spins forever, permanently hanging the JS event loop thread. This is triggered by callingmock.module('already-imported-module', async () => ({ default: 42 }))— the async factory returns a pending Promise synchronously, and the loop never exits.Extended reasoning...
The bug: In the
getJSValuelambda (lines 603–620 ofBunPlugin.cpp), awhileloop unwraps Promise chains returned byexecuteOnce. The loop condition is:The three cases are:
resultwith the resolved value (loop exits on next iteration once result is no longer a Promise)default) →break;which in C++ exits only the enclosingswitchstatement, not thewhileloopWhy the loop spins forever: Because
resultis never updated in the Pending branch,jsDynamicCast<JSC::JSPromise*>(result)evaluates to the same non-null pointer on the very next iteration. The loop has no sleep, no yield, and no way to drain microtasks between iterations — so a Pending promise can never transition to Fulfilled during iteration. The thread spins at 100% CPU until the process is killed.Concrete trigger sequence:
import 'my-module'— module is now in the ESM registrymock.module('my-module', async () => ({ default: 42 }))JSMock__jsModuleMock,esm->getreturns the existing entry, sogetJSValue()is calledexecuteOncecalls the async factory; anasyncfunction always returns a Promise in Pending state synchronously (the microtask that resolves it hasn't run yet)whileloop receives a PendingJSPromise, entersdefault: { break; }, exits the switch, immediately re-evaluates the while condition with the same unchangedresult— Pending againWhy existing code doesn't prevent it: The TODO comment
// TODO: blocking wait for promiseexplicitly acknowledges that blocking on the promise is unimplemented. But the implementation is actively incorrect: instead of returning an error or the pending promise to the caller (asrunVirtualModulecorrectly does at line ~930), it re-spins endlessly.Contrast with
runVirtualModule: That function handles Pending byreturn promise;— returning the pending promise to the caller for proper async resolution.getJSValuehas no equivalent escape.Fix: In the
default/Pending case, eithergoto/breakout of thewhileloop (e.g., using a labeled break or a flag variable), throw a descriptive error, or return the pending promise to the caller and handle it asynchronously asrunVirtualModuledoes. The minimal safe fix is to addgoto done;or restructure tobreakout of the outer loop when the status is Pending.