fix(napi): ensure tokio runtime is initialized for dlopen#2850
Merged
Brooooooklyn merged 6 commits intomainfrom Aug 8, 2025
Merged
fix(napi): ensure tokio runtime is initialized for dlopen#2850Brooooooklyn merged 6 commits intomainfrom
Brooooooklyn merged 6 commits intomainfrom
Conversation
How to use the Graphite Merge QueueAdd the label ready-to-merge to this PR to add it to the merge queue. You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. |
Member
Author
|
cursor review |
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a runtime panic that occurs when using async functions in NAPI modules loaded via dlopen() instead of require(). The issue was that the tokio runtime wasn't being initialized in dlopen scenarios, causing async functions to panic when trying to access an uninitialized runtime.
- Adds
start_async_runtime()calls to all runtime access functions to ensure proper initialization - Fixes panics in
spawn(),block_on(),spawn_blocking(), andwithin_runtime_if_available()functions - Includes explanatory comments documenting the dlopen scenario requirement
Fixes async functions panic when using nodejs dlopen by ensuring the tokio runtime is properly initialized before accessing it. When modules are loaded via dlopen instead of require, the runtime initialization might not happen through the normal module registration path. This fix adds start_async_runtime() calls to all runtime access functions to handle this scenario. Fixes #2847 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
ebb4cff to
bd33b46
Compare
Merged
This was referenced Nov 21, 2025
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Problem
When modules are loaded via
dlopen()instead ofrequire(), async functions would panic with:In the repro, the
exportsobject was GC after the file is imported:This causes the tokio runtime to shut down incorrectly. So we need to add cleanup hook via
napi_add_env_cleanup_hookwhen available.Fixes #2847
🤖 Generated with Claude Code