Skip to content

Comments

fix(napi): ensure tokio runtime is initialized for dlopen#2850

Merged
Brooooooklyn merged 6 commits intomainfrom
fix-dlopen-async-panic
Aug 8, 2025
Merged

fix(napi): ensure tokio runtime is initialized for dlopen#2850
Brooooooklyn merged 6 commits intomainfrom
fix-dlopen-async-panic

Conversation

@Brooooooklyn
Copy link
Member

@Brooooooklyn Brooooooklyn commented Aug 8, 2025

Summary

Problem

When modules are loaded via dlopen() instead of require(), async functions would panic with:

thread '<unnamed>' panicked at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/napi-3.2.0/src/tokio_runtime.rs:113:6:
Access tokio runtime failed in spawn

In the repro, the exports object was GC after the file is imported:

// loader.mjs

import { join } from "node:path";
import { dlopen } from "node:process";
import { fileURLToPath } from "node:url";

const nativeBinding = { exports: {} };
dlopen(nativeBinding, join(fileURLToPath(import.meta.url), "..", `module.linux-x64-gnu.node`));

globalThis.hello = nativeBinding.exports.hello;

This causes the tokio runtime to shut down incorrectly. So we need to add cleanup hook via napi_add_env_cleanup_hook when available.

Fixes #2847

🤖 Generated with Claude Code

@graphite-app
Copy link

graphite-app bot commented Aug 8, 2025

How to use the Graphite Merge Queue

Add 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.

@Brooooooklyn Brooooooklyn requested a review from Copilot August 8, 2025 07:35
@Brooooooklyn
Copy link
Member Author

cursor review

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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(), and within_runtime_if_available() functions
  • Includes explanatory comments documenting the dlopen scenario requirement

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no bugs!


Brooooooklyn and others added 3 commits August 8, 2025 16:30
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]>
@Brooooooklyn Brooooooklyn force-pushed the fix-dlopen-async-panic branch from ebb4cff to bd33b46 Compare August 8, 2025 08:30
@Brooooooklyn Brooooooklyn merged commit 913e42f into main Aug 8, 2025
64 checks passed
@Brooooooklyn Brooooooklyn deleted the fix-dlopen-async-panic branch August 8, 2025 12:12
@github-actions github-actions bot mentioned this pull request Aug 8, 2025
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.

Async functions panic when using nodejs dlopen.

1 participant