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

Panic in NAPI when using pl.format() with expressions in Polars Node.js #23493

Closed
drewbitt opened this issue Apr 22, 2024 · 4 comments · Fixed by #24203
Closed

Panic in NAPI when using pl.format() with expressions in Polars Node.js #23493

drewbitt opened this issue Apr 22, 2024 · 4 comments · Fixed by #24203
Assignees
Labels
bug Something isn't working node compat node native extension related to the node-api (.node)

Comments

@drewbitt
Copy link

Version: Deno 1.42.4

The Node.js version of the Polars library panics when using pl.format() with expressions inside a select operation. This issue occurs even in a minimal example and prevents the execution of the script. It should just error.

Reproduction

Minimial reproduction with documentation:

import pl from "npm:nodejs-polars";

/**
 * Create a sample DataFrame with a single column "ds" containing date values.
 */
const df = pl.DataFrame({
  ds: [
    new Date("2023-01-01"),
    new Date("2023-01-02"),
    new Date("2023-01-03"),
    new Date("2023-01-04"),
  ],
});

/**
 * Perform a simple operation on the DataFrame that causes a panic.
 *
 * The operation:
 * 1. Selects the "ds" column and aliases it as "ds1" and "ds2".
 * 2. Filters the rows where "ds1" is less than "ds2".
 * 3. Selects a new column "cadence" using pl.format() to format a string.
 *    The string includes placeholders {} and expressions that calculate the
 *    difference between "ds2" and "ds1" in days and adds a plural "s" if the
 *    difference is greater than 1.
 */
const cadences = df
  .select(pl.col("ds").alias("ds1"), pl.col("ds").alias("ds2"))
  .filter(pl.col("ds1").lessThan(pl.col("ds2")))
  .select(
    pl
      .format(
        "{} day{}",
        pl.col("ds2").cast(pl.Int64).sub(pl.col("ds1").cast(pl.Int64)),
        pl
          .when(
            pl
              .col("ds2")
              .cast(pl.Int64)
              .sub(pl.col("ds1").cast(pl.Int64))
              .greaterThan(1)
          )
          .then("s")
          .otherwise("")
      )
      .alias("cadence")
  );

console.log(cadences);

Panics with:

❯ deno run -A find-cadence-3.ts

============================================================
Deno has panicked. This is a bug in Deno. Please report this
at https://github.com/denoland/deno/issues/new.
If you can reliably reproduce this panic, include the
reproduction steps and re-run with the RUST_BACKTRACE=1 env
var set and include the backtrace in your report.

Platform: macos aarch64
Version: 1.42.4
Args: ["deno", "run", "-A", "find-cadence-3.ts"]

thread 'main' panicked at cli/napi/js_native_api.rs:2749:47:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Issue that is not being handled

The issue in the code is that

          .then("s")
          .otherwise("")

should be something like:

          .then(pl.lit("s"))
          .otherwise(pl.lit(""))

and I would expect the error to be handled.

Other runtimes

Node

 tsx find-cadence-3.ts    
/Users/drewbitt/Repos/x/scripts/find-cadences-imports/node_modules/nodejs-polars/bin/lazy/whenthen.js:33
        then: ({ _expr }) => Then(_when.then(_expr)),
                                        ^

Error: Failed to recover `JsExpr` type from napi value
    at Object.then (/Users/drewbitt/Repos/x/scripts/find-cadences-imports/node_modules/nodejs-polars/bin/lazy/whenthen.js:33:41)
    at pl (/Users/drewbitt/Repos/x/scripts/find-cadences-imports/find-cadence-3.ts:42:12)
    at Object.<anonymous> (/Users/drewbitt/Repos/x/scripts/find-cadences-imports/find-cadence-3.ts:54:21)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Object.S (/Users/drewbitt/.local/share/mise/installs/npm-tsx/4.7.1/lib/node_modules/tsx/dist/cjs/index.cjs:1:1292)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:202:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:336:24) {
  code: 'InvalidArg'
}

bun

 bun find-cadence-3.ts
28 |  * Utility function.
29 |  * @see {@link when}
30 |  */
31 | function When(_when) {
32 |     return {
33 |         then: ({ _expr }) => Then(_when.then(_expr)),
                                                  ^
error: Failed to recover `JsExpr` type from napi value
 code: "ObjectExpected"

      at then (/Users/drewbitt/Repos/x/scripts/find-cadences-imports/node_modules/.deno/nodejs-polars@0.11.0/node_modules/nodejs-polars/bin/lazy/whenthen.js:33:46)
      at /Users/drewbitt/Repos/x/scripts/find-cadences-imports/find-cadence-3.ts:34:9
@ry ry added the node compat label Apr 22, 2024
@bartlomieju bartlomieju added bug Something isn't working node native extension related to the node-api (.node) labels Apr 22, 2024
@bartlomieju bartlomieju assigned nathanwhit and unassigned littledivy Apr 23, 2024
@devsnek
Copy link
Member

devsnek commented Jun 10, 2024

This should be fixed in #24101, let us know if you encounter more issues.

@devsnek devsnek closed this as completed Jun 10, 2024
@drewbitt
Copy link
Author

drewbitt commented Jun 13, 2024

You still get the Deno has panicked error running the example in 1.44.1.

============================================================
Deno has panicked. This is a bug in Deno. Please report this
at https://github.com/denoland/deno/issues/new.
If you can reliably reproduce this panic, include the
reproduction steps and re-run with the RUST_BACKTRACE=1 env
var set and include the backtrace in your report.

Platform: macos aarch64
Version: 1.44.1
Args: ["deno"]

thread 'main' panicked at cli/napi/js_native_api.rs:2767:47:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

In 1.44.2, it just gives zsh: segmentation fault deno with no other error details. Upon re-running, it can also return zsh: bus error deno. It is unaffected by DENO_FUTURE=1

@nathanwhit nathanwhit reopened this Jun 13, 2024
@littledivy
Copy link
Member

littledivy commented Jun 13, 2024

On Linux x64:

deno run -A test.mjs
error: Uncaught (in promise) Error: Failed to recover `JsExpr` type from napi value
    at Object.then (file:///home/divy/.cache/deno/npm/registry.npmjs.org/nodejs-polars/0.13.0/bin/lazy/whenthen.js:33:41)
    at file:///home/divy/polars/test.mjs:42:12

@littledivy
Copy link
Member

@devsnek backtrace on macOS arm64:

Current executable set to '/Users/divy/gh/deno/target/debug/deno' (arm64).
(lldb) settings set -- target.run-args  "run" "-A" "test.mjs"
(lldb) r
Process 21462 launched: '/Users/divy/gh/deno/target/debug/deno' (arm64)
Process 21462 stopped
* thread #1, name = 'main', queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x62)
    frame #0: 0x0000000107957918 deno`_$LT$alloc..sync..Arc$LT$T$C$A$GT$$u20$as$u20$core..ops..drop..Drop$GT$::drop::h82c7829f6a862a1f at atomic.rs:3345:24
(lldb) bt
* thread #1, name = 'main', queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x62)
  * frame #0: 0x0000000107957918 deno`_$LT$alloc..sync..Arc$LT$T$C$A$GT$$u20$as$u20$core..ops..drop..Drop$GT$::drop::h82c7829f6a862a1f at atomic.rs:3345:24
    frame #1: 0x0000000107957914 deno`_$LT$alloc..sync..Arc$LT$T$C$A$GT$$u20$as$u20$core..ops..drop..Drop$GT$::drop::h82c7829f6a862a1f at atomic.rs:2683:26
    frame #2: 0x0000000107957904 deno`_$LT$alloc..sync..Arc$LT$T$C$A$GT$$u20$as$u20$core..ops..drop..Drop$GT$::drop::h82c7829f6a862a1f(self=0x0000600001c8e988) at sync.rs:2376:32
    frame #3: 0x00000001078f36ec deno`core::ptr::drop_in_place$LT$alloc..sync..Arc$LT$v8..isolate..IsolateAnnex$GT$$GT$::he1aaca765812698a((null)=0x0000600001c8e988) at mod.rs:507:1
    frame #4: 0x00000001078ee6c4 deno`core::ptr::drop_in_place$LT$v8..isolate..IsolateHandle$GT$::h2cb2292f91597c3b((null)=0x0000600001c8e988) at mod.rs:507:1
    frame #5: 0x00000001007c0ba0 deno`core::ptr::drop_in_place$LT$v8..handle..Weak$LT$v8..data..Value$GT$$GT$::h709014b697feaa6a((null)=0x0000600001c8e988) at mod.rs:507:1
    frame #6: 0x00000001007c3708 deno`core::ptr::drop_in_place$LT$deno..napi..js_native_api..ReferenceState$GT$::h89adb6946c3e255f((null)=0x0000600001c8e980) at mod.rs:507:1
    frame #7: 0x00000001007b9cf0 deno`core::ptr::drop_in_place$LT$deno..napi..js_native_api..Reference$GT$::h4e3bb75dd288fc86((null)=0x0000600001c8e980) at mod.rs:507:1
    frame #8: 0x00000001007e4918 deno`core::ptr::drop_in_place$LT$alloc..boxed..Box$LT$deno..napi..js_native_api..Reference$GT$$GT$::hb70addefede44491((null)=0x000000016fdc2fd8) at mod.rs:507:1
    frame #9: 0x00000001001b690c deno`core::mem::drop::h09ef545ddf5c9140(_x=0x0000600001c8e980) at mod.rs:992:24
    frame #10: 0x0000000100551100 deno`deno::napi::js_native_api::Reference::weak_callback::h3a5bb954afa79c33(reference=0x0000600001c8e980) at js_native_api.rs:137:16
    frame #11: 0x0000000101591b20 deno`deno::napi::js_native_api::Reference::set_weak::_$u7b$$u7b$closure$u7d$$u7d$::hb26bf6374272d63f((null)=0x0000000150018000) at js_native_api.rs:114:9
    frame #12: 0x00000001006b7e3c deno`core::ops::function::FnOnce::call_once::h2de6631cc56b75e2((null)={closure_env#0} @ 0x000000016fdc3070, (null)=(&mut v8::isolate::Isolate) @ 0x000000016fdc3078) at function.rs:250:5
    frame #13: 0x00000001006aae84 deno`core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::h4954da2402e60edd((null)=0x0000600000b94390, (null)=(&mut v8::isolate::Isolate) @ 0x000000016fdc30a0) at function.rs:250:5
    frame #14: 0x0000000107bde320 deno`_$LT$alloc..boxed..Box$LT$F$C$A$GT$$u20$as$u20$core..ops..function..FnOnce$LT$Args$GT$$GT$::call_once::hd518edc6d8431261(self=alloc::boxed::Box<dyn core::ops::function::FnOnce<(&mut v8::isolate::Isolate), Output=()>, alloc::alloc::Global> @ 0x000000016fdc30e0, args=(&mut v8::isolate::Isolate) @ 0x000000016fdc30c8) at boxed.rs:2015:9
    frame #15: 0x000000010158d7dc deno`v8::handle::Weak$LT$T$GT$::second_pass_callback::hdafd3e319fa32ee3(wci=0x000000016fdc3270) at handle.rs:841:54
    frame #16: 0x00000001080b538c deno`::InvokeSecondPassPhantomCallbacks() at global-handles.cc:767:18 [opt]
    frame #17: 0x000000010811ec94 deno`::CollectGarbage() - 18446744069279191915 [opt]
    frame #18: 0x00000001080e9a14 deno`::AllocateRawWithLightRetrySlowPath() at heap-allocator.cc:125:14 [opt]
    frame #19: 0x00000001080ea2c0 deno`::AllocateRawWithRetryOrFailSlowPath() at heap-allocator.cc:140:7 [opt]
    frame #20: 0x00000001080d030c deno`::AllocateRaw() at factory.cc:284:23 [opt]
    frame #21: 0x00000001080cab08 deno`::NewDescriptorArray() at factory-base.cc:1171:28 [opt]
    frame #22: 0x0000000108412974 deno`::CopyUpToAddAttributes() at objects.cc:3654:7 [opt]
    frame #23: 0x00000001083ee3cc deno`::CopyAddDescriptor() at map.cc:2152:7 [opt]
    frame #24: 0x00000001083ee1b8 deno`::CopyWithField() at map.cc:479:25 [opt]
    frame #25: 0x00000001083f34bc deno`::TransitionToDataProperty() at map.cc:1975:17 [opt]
    frame #26: 0x00000001083e51fc deno`::PrepareTransitionToDataProperty() at lookup.cc:635:7 [opt]
    frame #27: 0x0000000108410460 deno`::TransitionAndWriteDataProperty() at objects.cc:2694:7 [opt]
    frame #28: 0x000000010838e380 deno`::DefineOwnPropertyIgnoreAttributes() at js-objects.cc:3707:16 [opt]
    frame #29: 0x0000000108518f78 deno`::Runtime_DefineKeyedOwnPropertyInLiteral() at runtime-object.cc:944:1 [opt]
    frame #30: 0x00000001091b1834 deno`Builtins_CEntry_Return1_ArgvOnStack_NoBuiltinExit + 84
    frame #31: 0x00000001092aca10 deno`Builtins_DefineKeyedOwnPropertyInLiteralWideHandler + 144
    frame #32: 0x0000000109115a90 deno`Builtins_InterpreterEntryTrampoline + 304
    frame #33: 0x0000000109115a90 deno`Builtins_InterpreterEntryTrampoline + 304
    frame #34: 0x0000000109115a90 deno`Builtins_InterpreterEntryTrampoline + 304
    frame #35: 0x0000000109156e88 deno`Builtins_GeneratorPrototypeNext + 136
    frame #36: 0x0000000109113648 deno`Builtins_JSEntryTrampoline + 168
    frame #37: 0x0000000109113294 deno`Builtins_JSEntry + 180
    frame #38: 0x0000000108060584 deno`::Invoke() at execution.cc:419:22 [opt]
    frame #39: 0x0000000108060e04 deno`::InvokeWithTryCatch() at execution.cc:476:18 [opt]
    frame #40: 0x0000000108060ee4 deno`::TryCall() at execution.cc:568:10 [opt]
    frame #41: 0x000000010842b504 deno`::ExecuteModule() at source-text-module.cc:1016:8 [opt]
    frame #42: 0x000000010842ad5c deno`::InnerModuleEvaluation() at source-text-module.cc:1190:10 [opt]
    frame #43: 0x000000010842a6fc deno`::Evaluate() at source-text-module.cc:734:7 [opt]
    frame #44: 0x00000001083f5610 deno`::Evaluate() at module.cc:281:12 [opt]
    frame #45: 0x0000000107f62158 deno`::Evaluate() at api.cc:2432:28 [opt]
    frame #46: 0x00000001078daa78 deno`v8::module::_$LT$impl$u20$v8..data..Module$GT$::evaluate::_$u7b$$u7b$closure$u7d$$u7d$::heb1f406a36fff2ff((null)={closure_env#0} @ 0x000000016fdc4368, sd=0x000060000228ce70) at module.rs:348:26
    frame #47: 0x0000000107b138b8 deno`deno_core::modules::map::ModuleMap::mod_evaluate::hde432b9bad56ea3b [inlined] v8::scope::HandleScope$LT$$LP$$RP$$GT$::cast_local::h467bb0ec6202c1a1(self=0x000000016fdc4d10, f={closure_env#0} @ 0x000000016fdc56a8) at scope.rs:239:21
    frame #48: 0x0000000107b135c8 deno`deno_core::modules::map::ModuleMap::mod_evaluate::hde432b9bad56ea3b [inlined] v8::module::_$LT$impl$u20$v8..data..Module$GT$::evaluate::hc4621c440a3a5d57(self=0x000000015c861c08, scope=0x000000016fdc4d10) at module.rs:347:7
    frame #49: 0x0000000107b1359c deno`deno_core::modules::map::ModuleMap::mod_evaluate::hde432b9bad56ea3b(self=0x000000015000e670, scope=0x000000016fdc6548, id=297) at map.rs:1005:23
    frame #50: 0x000000010792e5d0 deno`deno_core::runtime::jsruntime::JsRuntime::mod_evaluate::hcfa7713a5d45cc23(self=0x000000015000e648, id=297) at jsruntime.rs:2177:5
    frame #51: 0x000000010108bd5c deno`deno_runtime::worker::MainWorker::evaluate_module::_$u7b$$u7b$closure$u7d$$u7d$::hce8591d59f8bad93((null)=0x000000016fdfb9c0) at worker.rs:736:24
    frame #52: 0x000000010087301c deno`deno::worker::CliMainWorker::evaluate_module_possibly_with_npm::_$u7b$$u7b$closure$u7d$$u7d$::h39ed9cb79208628f((null)=0x000000016fdfb9c0) at worker.rs:339:37
    frame #53: 0x0000000100872a98 deno`deno::worker::CliMainWorker::execute_main_module_possibly_with_npm::_$u7b$$u7b$closure$u7d$$u7d$::h079ef1725f386e53((null)=0x000000016fdfb9c0) at worker.rs:325:48
    frame #54: 0x0000000100870d8c deno`deno::worker::CliMainWorker::run::_$u7b$$u7b$closure$u7d$$u7d$::hd3ee6e417e8fa7d4((null)=0x000000016fdfb9c0) at worker.rs:190:52
    frame #55: 0x000000010021a9b4 deno`deno::tools::run::run_script::_$u7b$$u7b$closure$u7d$$u7d$::h03c277908c8abea8((null)=0x000000016fdfb9c0) at mod.rs:75:32
    frame #56: 0x00000001017b7b8c deno`deno::run_subcommand::_$u7b$$u7b$closure$u7d$$u7d$::_$u7b$$u7b$closure$u7d$$u7d$::hdb8854b97844e3af((null)=0x000000016fdfb9c0) at main.rs:180:82
    frame #57: 0x0000000100aab430 deno`_$LT$futures_util..future..future..map..Map$LT$Fut$C$F$GT$$u20$as$u20$core..future..future..Future$GT$::poll::hef2194146ddbf4fd(self=(__pointer = 0x000000015000d388), cx=0x000000016fdfb9c0) at map.rs:55:37
    frame #58: 0x000000010068338c deno`_$LT$futures_util..future..future..Map$LT$Fut$C$F$GT$$u20$as$u20$core..future..future..Future$GT$::poll::h681cbb07710ee771(self=Pin<&mut futures_util::future::future::Map<deno::run_subcommand::{async_fn#0}::{async_block_env#18}, deno::spawn_subcommand::{async_block#0}::{closure_env#0}<deno::run_subcommand::{async_fn#0}::{async_block_env#18}, core::result::Result<i32, anyhow::Error>>>> @ 0x000000016fdebc90, cx=0x000000016fdfb9c0) at lib.rs:91:13
    frame #59: 0x00000001017ad5dc deno`deno::spawn_subcommand::_$u7b$$u7b$closure$u7d$$u7d$::ha4e8b210c8069a9d((null)=0x000000016fdfb9c0) at main.rs:88:40
    frame #60: 0x00000001014024dc deno`_$LT$core..pin..Pin$LT$P$GT$$u20$as$u20$core..future..future..Future$GT$::poll::h561ad21625b36462(self=Pin<&mut core::pin::Pin<alloc::boxed::Box<dyn core::future::future::Future<Output=core::result::Result<i32, anyhow::Error>>, alloc::alloc::Global>>> @ 0x000000016fdfb818, cx=0x000000016fdfb9c0) at future.rs:124:9
    frame #61: 0x00000001006904b8 deno`_$LT$deno_unsync..task..MaskFutureAsSend$LT$F$GT$$u20$as$u20$core..future..future..Future$GT$::poll::h43c7a304a9c6018d(self=Pin<&mut deno_unsync::task::MaskFutureAsSend<core::pin::Pin<alloc::boxed::Box<dyn core::future::future::Future<Output=core::result::Result<i32, anyhow::Error>>, alloc::alloc::Global>>>> @ 0x000000016fdfb898, cx=0x000000016fdfb9c0) at task.rs:134:13
    frame #62: 0x0000000101661af8 deno`tokio::runtime::task::core::Core$LT$T$C$S$GT$::poll::_$u7b$$u7b$closure$u7d$$u7d$::h15f7873529f40105(ptr=0x000000015d804930) at core.rs:328:17
    frame #63: 0x0000000101645490 deno`tokio::runtime::task::core::Core$LT$T$C$S$GT$::poll::h204e787221eed80d [inlined] tokio::loom::std::unsafe_cell::UnsafeCell$LT$T$GT$::with_mut::h868d5c86938b6736(self=0x000000015d804930, f={closure_env#0}<deno_unsync::task::MaskFutureAsSend<core::pin::Pin<alloc::boxed::Box<dyn core::future::future::Future<Output=core::result::Result<i32, anyhow::Error>>, alloc::alloc::Global>>>, alloc::sync::Arc<tokio::runtime::scheduler::current_thread::Handle, alloc::alloc::Global>> @ 0x000000016fdfba18) at unsafe_cell.rs:16:9
    frame #64: 0x0000000101645468 deno`tokio::runtime::task::core::Core$LT$T$C$S$GT$::poll::h204e787221eed80d(self=0x000000015d804920, cx=Context @ 0x000000016fdfb9c0) at core.rs:317:13
    frame #65: 0x00000001003cc338 deno`tokio::runtime::task::harness::poll_future::_$u7b$$u7b$closure$u7d$$u7d$::hc3ef6af643177f12 at harness.rs:485:19
    frame #66: 0x00000001005fb174 deno`_$LT$core..panic..unwind_safe..AssertUnwindSafe$LT$F$GT$$u20$as$u20$core..ops..function..FnOnce$LT$$LP$$RP$$GT$$GT$::call_once::h603d4ca839cf41f0(self=AssertUnwindSafe<tokio::runtime::task::harness::poll_future::{closure_env#0}<deno_unsync::task::MaskFutureAsSend<core::pin::Pin<alloc::boxed::Box<dyn core::future::future::Future<Output=core::result::Result<i32, anyhow::Error>>, alloc::alloc::Global>>>, alloc::sync::Arc<tokio::runtime::scheduler::current_thread::Handle, alloc::alloc::Global>>> @ 0x000000016fdfbac0, (null)=<unavailable>) at unwind_safe.rs:272:9
    frame #67: 0x00000001011c2c18 deno`std::panicking::try::do_call::hc877c9eb3c6ed89d(data=" I\x80]\U00000001") at panicking.rs:554:40
    frame #68: 0x00000001012117ac deno`__rust_try + 32
    frame #69: 0x00000001011975e4 deno`std::panicking::try::heab88d541c8bb47e(f=AssertUnwindSafe<tokio::runtime::task::harness::poll_future::{closure_env#0}<deno_unsync::task::MaskFutureAsSend<core::pin::Pin<alloc::boxed::Box<dyn core::future::future::Future<Output=core::result::Result<i32, anyhow::Error>>, alloc::alloc::Global>>>, alloc::sync::Arc<tokio::runtime::scheduler::current_thread::Handle, alloc::alloc::Global>>> @ 0x000000016fdfbbc0) at panicking.rs:518:19
    frame #70: 0x0000000100d4d830 deno`std::panic::catch_unwind::h3ef2b76b43ae05df(f=AssertUnwindSafe<tokio::runtime::task::harness::poll_future::{closure_env#0}<deno_unsync::task::MaskFutureAsSend<core::pin::Pin<alloc::boxed::Box<dyn core::future::future::Future<Output=core::result::Result<i32, anyhow::Error>>, alloc::alloc::Global>>>, alloc::sync::Arc<tokio::runtime::scheduler::current_thread::Handle, alloc::alloc::Global>>> @ 0x000000016fdfbc00) at panic.rs:142:14
    frame #71: 0x00000001003898a8 deno`tokio::runtime::task::harness::poll_future::h3e32af9c5a2952c8(core=0x000000015d804920, cx=Context @ 0x000000016fdfbd38) at harness.rs:473:18
    frame #72: 0x00000001003e05b4 deno`tokio::runtime::task::harness::Harness$LT$T$C$S$GT$::poll_inner::h48c0f7a2b79c3e4e(self=0x000000016fdfbdf8) at harness.rs:208:27
    frame #73: 0x000000010043aa34 deno`tokio::runtime::task::harness::Harness$LT$T$C$S$GT$::poll::h6a385b1dbf8559bd(self=Harness<deno_unsync::task::MaskFutureAsSend<core::pin::Pin<alloc::boxed::Box<dyn core::future::future::Future<Output=core::result::Result<i32, anyhow::Error>>, alloc::alloc::Global>>>, alloc::sync::Arc<tokio::runtime::scheduler::current_thread::Handle, alloc::alloc::Global>> @ 0x000000016fdfbdf8) at harness.rs:153:15
    frame #74: 0x00000001004c53f4 deno`tokio::runtime::task::raw::poll::h9e267484501aa195(ptr=NonNull<tokio::runtime::task::core::Header> @ 0x000000016fdfbe20) at raw.rs:271:5
    frame #75: 0x0000000107db1064 deno`tokio::runtime::task::raw::RawTask::poll::h0a7ad026d65fc83b(self=RawTask @ 0x000000016fdfbe40) at raw.rs:201:18
    frame #76: 0x00000001051b1e84 deno`tokio::runtime::task::LocalNotified$LT$S$GT$::run::ha7f2be130fcebf3a(self=LocalNotified<alloc::sync::Arc<tokio::runtime::scheduler::current_thread::Handle, alloc::alloc::Global>> @ 0x000000016fdfbe70) at mod.rs:427:9
    frame #77: 0x00000001014ad590 deno`tokio::runtime::scheduler::current_thread::CoreGuard::block_on::_$u7b$$u7b$closure$u7d$$u7d$::_$u7b$$u7b$closure$u7d$$u7d$::h04a12d67abfcb10e at mod.rs:700:25
    frame #78: 0x00000001014a96bc deno`tokio::runtime::scheduler::current_thread::Context::run_task::_$u7b$$u7b$closure$u7d$$u7d$::hb8631102ad9911d1 at coop.rs:107:5
    frame #79: 0x00000001014a964c deno`tokio::runtime::scheduler::current_thread::Context::run_task::_$u7b$$u7b$closure$u7d$$u7d$::hb8631102ad9911d1 [inlined] tokio::runtime::coop::budget::h05a98b4fed45559b(f={closure_env#1}<core::pin::Pin<&mut tokio::runtime::task::join::JoinHandle<deno_unsync::task::MaskResultAsSend<core::result::Result<i32, anyhow::Error>>>>> @ 0x000000016fdfbed8) at coop.rs:73:5
    frame #80: 0x00000001014a95f4 deno`tokio::runtime::scheduler::current_thread::Context::run_task::_$u7b$$u7b$closure$u7d$$u7d$::hb8631102ad9911d1 at mod.rs:343:43
    frame #81: 0x00000001014a85f8 deno`tokio::runtime::scheduler::current_thread::Context::enter::hc13aca7d6bc31095(self=0x000000016fdfc800, core=0x000000015be07440, f={closure_env#0}<(), tokio::runtime::scheduler::current_thread::{impl#8}::block_on::{closure#0}::{closure_env#1}<core::pin::Pin<&mut tokio::runtime::task::join::JoinHandle<deno_unsync::task::MaskResultAsSend<core::result::Result<i32, anyhow::Error>>>>>> @ 0x000000016fdfbf90) at mod.rs:404:19
    frame #82: 0x00000001014a8c98 deno`tokio::runtime::scheduler::current_thread::Context::run_task::h04127ca1afdff377(self=0x000000016fdfc800, core=0x000000015be07440, f={closure_env#1}<core::pin::Pin<&mut tokio::runtime::task::join::JoinHandle<deno_unsync::task::MaskResultAsSend<core::result::Result<i32, anyhow::Error>>>>> @ 0x000000016fdfc028) at mod.rs:343:23
    frame #83: 0x00000001014abc54 deno`tokio::runtime::scheduler::current_thread::CoreGuard::block_on::_$u7b$$u7b$closure$u7d$$u7d$::h35a986e03df88f40(core=0x000000015be07440, context=0x000000016fdfc800) at mod.rs:699:35
    frame #84: 0x00000001014aabd8 deno`tokio::runtime::scheduler::current_thread::CoreGuard::enter::_$u7b$$u7b$closure$u7d$$u7d$::h098a3cc62d526db9 at mod.rs:737:68
    frame #85: 0x00000001010df564 deno`tokio::runtime::context::scoped::Scoped$LT$T$GT$::set::h841d071e11e02a64(self=0x000000015d009280, t=0x000000016fdfc7f8, f={closure_env#0}<tokio::runtime::scheduler::current_thread::{impl#8}::block_on::{closure_env#0}<core::pin::Pin<&mut tokio::runtime::task::join::JoinHandle<deno_unsync::task::MaskResultAsSend<core::result::Result<i32, anyhow::Error>>>>>, core::option::Option<core::result::Result<deno_unsync::task::MaskResultAsSend<core::result::Result<i32, anyhow::Error>>, tokio::runtime::task::error::JoinError>>> @ 0x000000016fdfc3d0) at scoped.rs:40:9
    frame #86: 0x00000001010de49c deno`tokio::runtime::context::set_scheduler::_$u7b$$u7b$closure$u7d$$u7d$::hb97b9b5652eaf121(c=0x000000015d009248) at context.rs:176:26
    frame #87: 0x000000010154b5ec deno`std::thread::local::LocalKey$LT$T$GT$::try_with::h2d2601a36815528a(self=0x000000010e872418, f={closure_env#0}<(alloc::boxed::Box<tokio::runtime::scheduler::current_thread::Core, alloc::alloc::Global>, core::option::Option<core::result::Result<deno_unsync::task::MaskResultAsSend<core::result::Result<i32, anyhow::Error>>, tokio::runtime::task::error::JoinError>>), tokio::runtime::scheduler::current_thread::{impl#8}::enter::{closure_env#0}<tokio::runtime::scheduler::current_thread::{impl#8}::block_on::{closure_env#0}<core::pin::Pin<&mut tokio::runtime::task::join::JoinHandle<deno_unsync::task::MaskResultAsSend<core::result::Result<i32, anyhow::Error>>>>>, core::option::Option<core::result::Result<deno_unsync::task::MaskResultAsSend<core::result::Result<i32, anyhow::Error>>, tokio::runtime::task::error::JoinError>>>> @ 0x000000016fdfc538) at local.rs:286:16
    frame #88: 0x0000000101548270 deno`std::thread::local::LocalKey$LT$T$GT$::with::h4f2e0277ad16e998(self=0x000000010e872418, f=<unavailable>) at local.rs:262:9
    frame #89: 0x00000001010de220 deno`tokio::runtime::context::set_scheduler::h38d1e8b524071a30(v=0x000000016fdfc7f8, f=<unavailable>) at context.rs:176:9
    frame #90: 0x00000001014a9e40 deno`tokio::runtime::scheduler::current_thread::CoreGuard::enter::h15396d123d3a885f(self=CoreGuard @ 0x000000016fdfc7f8, f={closure_env#0}<core::pin::Pin<&mut tokio::runtime::task::join::JoinHandle<deno_unsync::task::MaskResultAsSend<core::result::Result<i32, anyhow::Error>>>>> @ 0x000000016fdfc670) at mod.rs:737:27
    frame #91: 0x00000001014aad4c deno`tokio::runtime::scheduler::current_thread::CoreGuard::block_on::h29c1af1ae641ad37(self=<unavailable>, future=Pin<&mut tokio::runtime::task::join::JoinHandle<deno_unsync::task::MaskResultAsSend<core::result::Result<i32, anyhow::Error>>>> @ 0x000000016fdfc728) at mod.rs:646:19
    frame #92: 0x0000000101493aec deno`tokio::runtime::scheduler::current_thread::CurrentThread::block_on::_$u7b$$u7b$closure$u7d$$u7d$::h2c95117a1ea8de3d(blocking=0x000000016fdfca30) at mod.rs:175:28
    frame #93: 0x0000000100a1d7e4 deno`tokio::runtime::context::runtime::enter_runtime::ha0aa3d000e191964(handle=0x000000016fdfdfd0, allow_block_in_place=false, f={closure_env#0}<tokio::runtime::task::join::JoinHandle<deno_unsync::task::MaskResultAsSend<core::result::Result<i32, anyhow::Error>>>> @ 0x000000016fdfcaf8) at runtime.rs:65:16
    frame #94: 0x0000000101493a08 deno`tokio::runtime::scheduler::current_thread::CurrentThread::block_on::hfafb29dd3dc333ff(self=0x000000016fdfdfa8, handle=0x000000016fdfdfd0, future=JoinHandle<deno_unsync::task::MaskResultAsSend<core::result::Result<i32, anyhow::Error>>> @ 0x000000016fdfcb20) at mod.rs:167:9
    frame #95: 0x0000000100534dbc deno`tokio::runtime::runtime::Runtime::block_on::h2f1efe4686946460(self=0x000000016fdfdfa0, future=JoinHandle<deno_unsync::task::MaskResultAsSend<core::result::Result<i32, anyhow::Error>>> @ 0x000000016fdfcb88) at runtime.rs:349:47
    frame #96: 0x0000000100bd55d4 deno`deno::main::hc46c1d1b3670edf9 at tokio_util.rs:103:11
    frame #97: 0x0000000100bd53ec deno`deno::main::hc46c1d1b3670edf9 at tokio_util.rs:128:3
    frame #98: 0x0000000100bd5318 deno`deno::main::hc46c1d1b3670edf9 at main.rs:335:9
    frame #99: 0x00000001006b2668 deno`core::ops::function::FnOnce::call_once::h10af53be884e392b((null)=(deno`deno::main::hc46c1d1b3670edf9 at main.rs:314), (null)=<unavailable>) at function.rs:250:5
    frame #100: 0x0000000100975f64 deno`std::sys_common::backtrace::__rust_begin_short_backtrace::h230304b65c85f103(f=(deno`deno::main::hc46c1d1b3670edf9 at main.rs:314)) at backtrace.rs:155:18
    frame #101: 0x00000001009d218c deno`std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h527ec44e7d96643f at rt.rs:166:18
    frame #102: 0x0000000109340750 deno`std::rt::lang_start_internal::h0e09503d2b7f298e [inlined] core::ops::function::impls::_$LT$impl$u20$core..ops..function..FnOnce$LT$A$GT$$u20$for$u20$$RF$F$GT$::call_once::h4ad1988d4a160680 at function.rs:284:13 [opt]
    frame #103: 0x0000000109340748 deno`std::rt::lang_start_internal::h0e09503d2b7f298e [inlined] std::panicking::try::do_call::h2331bfde8ef4cc3d at panicking.rs:554:40 [opt]
    frame #104: 0x0000000109340748 deno`std::rt::lang_start_internal::h0e09503d2b7f298e [inlined] std::panicking::try::h8b8bd8b27e4c66f6 at panicking.rs:518:19 [opt]
    frame #105: 0x0000000109340748 deno`std::rt::lang_start_internal::h0e09503d2b7f298e [inlined] std::panic::catch_unwind::hae6cb7ed67951dea at panic.rs:142:14 [opt]
    frame #106: 0x0000000109340748 deno`std::rt::lang_start_internal::h0e09503d2b7f298e [inlined] std::rt::lang_start_internal::_$u7b$$u7b$closure$u7d$$u7d$::hd4e8129122b055e2 at rt.rs:148:48 [opt]
    frame #107: 0x0000000109340748 deno`std::rt::lang_start_internal::h0e09503d2b7f298e [inlined] std::panicking::try::do_call::h6feb7fafe6c6f753 at panicking.rs:554:40 [opt]
    frame #108: 0x0000000109340744 deno`std::rt::lang_start_internal::h0e09503d2b7f298e [inlined] std::panicking::try::h2fa734b49f8484d4 at panicking.rs:518:19 [opt]
    frame #109: 0x0000000109340744 deno`std::rt::lang_start_internal::h0e09503d2b7f298e [inlined] std::panic::catch_unwind::h487a42f5cf6b259f at panic.rs:142:14 [opt]
    frame #110: 0x0000000109340744 deno`std::rt::lang_start_internal::h0e09503d2b7f298e at rt.rs:148:20 [opt]
    frame #111: 0x00000001009d2158 deno`std::rt::lang_start::hf1df37447c11ca11(main=(deno`deno::main::hc46c1d1b3670edf9 at main.rs:314), argc=4, argv=0x000000016fdff7b0, sigpipe='\0') at rt.rs:165:17
    frame #112: 0x0000000100bd5ff8 deno`main + 36
    frame #113: 0x00000001842590e0 dyld`start + 2360
(lldb) exit

nathanwhit added a commit that referenced this issue Jun 13, 2024
… crash (#24203)

Fixes #23493.

What was happening here was that napi-rs was freeing the napi reference
([here](https://github.com/napi-rs/napi-rs/blob/19e3488efcbc601afa1f11a979372eb6c5ea6130/crates/napi/src/bindgen_runtime/mod.rs#L62))
during its finalize callback (which we call
[here](https://github.com/denoland/deno/blob/fb31eaa9ca59f6daaee0210d5cd206185c7041b9/cli/napi/js_native_api.rs#L132)).
We then were [reading the `ownership`
field](https://github.com/denoland/deno/blob/fb31eaa9ca59f6daaee0210d5cd206185c7041b9/cli/napi/js_native_api.rs#L136)
of that freed reference.

For some reason on arm macs the freed memory gets zeroed, so the value
of `ownership` was `0` when we read it (i.e. it was
`ReferenceOwnership::Runtime`). We then freed it again (since we thought
we owned it), causing the segfault.
bartlomieju pushed a commit that referenced this issue Jun 18, 2024
… crash (#24203)

Fixes #23493.

What was happening here was that napi-rs was freeing the napi reference
([here](https://github.com/napi-rs/napi-rs/blob/19e3488efcbc601afa1f11a979372eb6c5ea6130/crates/napi/src/bindgen_runtime/mod.rs#L62))
during its finalize callback (which we call
[here](https://github.com/denoland/deno/blob/fb31eaa9ca59f6daaee0210d5cd206185c7041b9/cli/napi/js_native_api.rs#L132)).
We then were [reading the `ownership`
field](https://github.com/denoland/deno/blob/fb31eaa9ca59f6daaee0210d5cd206185c7041b9/cli/napi/js_native_api.rs#L136)
of that freed reference.

For some reason on arm macs the freed memory gets zeroed, so the value
of `ownership` was `0` when we read it (i.e. it was
`ReferenceOwnership::Runtime`). We then freed it again (since we thought
we owned it), causing the segfault.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working node compat node native extension related to the node-api (.node)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants