Skip to content

Async functions panic when using nodejs dlopen. #2847

@omasakun

Description

@omasakun

When loading a native module created with napi-rs v3 using dlopen, asynchronous function calls panic.
This problem did not occur with napi v2.

This issue can be avoided by using require instead of dlopen, so this issue does not need to be resolved, but is this the expected behavior?

Reproduction

  1. Create a napi project with napi new and cargo add napi -F async
  2. Add an async function
    // lib.rs
    
    use napi_derive::napi;
    
    #[napi]
    pub async fn hello() -> u32 {
      42
    }
  3. Create a loader file:
    // 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;
  4. Build & Start REPL: npm build && node --import ./loader.mjs
  5. Call hello()
Result
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
stack backtrace:
   0:     0x768fb7f16c1a - std::backtrace_rs::backtrace::libunwind::trace::h88deb10bd0145eb8
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:     0x768fb7f16c1a - std::backtrace_rs::backtrace::trace_unsynchronized::he1036f5481c14dff
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x768fb7f16c1a - std::sys::backtrace::_print_fmt::hecc345b6e70c4b20
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/sys/backtrace.rs:66:9
   3:     0x768fb7f16c1a - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::he089f96442833f67
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/sys/backtrace.rs:39:26
   4:     0x768fb7f34803 - core::fmt::rt::Argument::fmt::h1f77cded99c71a14
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/core/src/fmt/rt.rs:177:76
   5:     0x768fb7f34803 - core::fmt::write::h2f210ed4c94745cb
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/core/src/fmt/mod.rs:1440:21
   6:     0x768fb7f14473 - std::io::Write::write_fmt::h7de08171ab770fb2
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/io/mod.rs:1887:15
   7:     0x768fb7f16a62 - std::sys::backtrace::BacktraceLock::print::h810fbd31421329e6
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/sys/backtrace.rs:42:9
   8:     0x768fb7f17c00 - std::panicking::default_hook::{{closure}}::hbaad47ed9dc6356d
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/panicking.rs:295:22
   9:     0x768fb7f179e0 - std::panicking::default_hook::h24e207139139d40a
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/panicking.rs:322:9
  10:     0x768fb7f18272 - std::panicking::rust_panic_with_hook::ha9131beeb2ddc506
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/panicking.rs:828:13
  11:     0x768fb7f1810a - std::panicking::begin_panic_handler::{{closure}}::h1bba0eaeb6da506f
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/panicking.rs:701:13
  12:     0x768fb7f17119 - std::sys::backtrace::__rust_end_short_backtrace::h1d1ca3eade483f4c
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/sys/backtrace.rs:168:18
  13:     0x768fb7f17d9d - rust_begin_unwind
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/panicking.rs:692:5
  14:     0x768fb7e6e5b0 - core::panicking::panic_fmt::h896a0727a1a943f9
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/core/src/panicking.rs:75:14
  15:     0x768fb7e6e58b - core::panicking::panic_display::he48e31e939a730f1
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/core/src/panicking.rs:261:5
  16:     0x768fb7e6e58b - core::option::expect_failed::h8c40d2d654ba3611
                               at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/core/src/option.rs:2024:5
  17:     0x768fb7e72112 - core::option::Option<T>::expect::h095772936ac6a98b
                               at /home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:933:21
  18:     0x768fb7e855cb - napi::tokio_runtime::spawn::h5d8213034b010342
                               at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/napi-3.2.0/src/tokio_runtime.rs:110:3
  19:     0x768fb7e84ada - napi::tokio_runtime::execute_tokio_future::h53947a2a9579e980
                               at /home/user/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/napi-3.2.0/src/tokio_runtime.rs:257:12
  20:     0x768fb7e89aaa - foo::hello_c_callback::{{closure}}::h72b605f92deef390
                               at /repos/foo/src/lib.rs:3:1
  21:     0x768fb7e861a3 - core::result::Result<T,E>::and_then::h7d5772899b08722a
                               at /home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:1353:22
  22:     0x768fb7e6f024 - foo::hello_c_callback::h99730f486e375a65
                               at /repos/foo/src/lib.rs:3:1
  23:           0xee9da5 - _ZN6v8impl12_GLOBAL__N_123FunctionCallbackWrapper6InvokeERKN2v820FunctionCallbackInfoINS2_5ValueEEE
  24:     0x768fafe0f5e2 - <unknown>
fatal runtime error: failed to initiate panic, error 5
Aborted (core dumped)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions