Skip to content
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

fix(realms): Make realm creation not hang with async ops running #99

Merged

Conversation

andreubotella
Copy link
Contributor

The way JsRuntime::init_extension_js initialized ESM extensions without a snapshot was by running the event loop and blocking the thread ntil there were no more tasks. This works fine for the main realm, because it is created before any user code can start any async ops. However, other realms may be created while async ops are running, which would make realm creation hang until those ops are resolved.

Since extensions are not expected to use top-level await, there would be no need to run the entire event loop for modules to resolve; calling JsRealm::evaluate_pending_module would be enough. If the module is not resolved after calling that functions, then it or one of its dependencies has TLA, in which case we panic.

Since a panic like "TLA is not allowed" would be hard to debug, especially on codebases with many modules, we use ModuleMap::find_stalled_top_level_await to find at least one of the TLA locations, and we display it in the panic message.

The way `JsRuntime::init_extension_js` initialized ESM extensions
without a snapshot was by running the event loop and blocking the
thread ntil there were no more tasks. This works fine for the main
realm, because it is created before any user code can start any async
ops. However, other realms may be created while async ops are running,
which would make realm creation hang until those ops are resolved.

Since extensions are not expected to use top-level await, there would
be no need to run the entire event loop for modules to resolve;
calling `JsRealm::evaluate_pending_module` would be enough. If the
module is not resolved after calling that functions, then it or one of
its dependencies has TLA, in which case we panic.

Since a panic like "TLA is not allowed" would be hard to debug,
especially on codebases with many modules, we use
`ModuleMap::find_stalled_top_level_await` to find at least one of the
TLA locations, and we display it in the panic message.
Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

Lovely solution, LGTM!

@bartlomieju bartlomieju merged commit 58a8f6d into denoland:main Jul 31, 2023
4 checks passed
@andreubotella andreubotella deleted the fix-realm-initialization-hang branch August 4, 2023 14:26
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.

None yet

2 participants