Skip to content

Calling n-api methods from own separate thread #17819

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

Closed
gryor opened this issue Dec 22, 2017 · 1 comment
Closed

Calling n-api methods from own separate thread #17819

gryor opened this issue Dec 22, 2017 · 1 comment
Labels
question Issues that look for answers. wrong repo Issues that should be opened in another repository.

Comments

@gryor
Copy link

gryor commented Dec 22, 2017

  • Version: v9.3.0
  • Platform: Arch Linux 64-bit, Kernel 4.14.6

I'm trying to create a module that has an application compiled within.
The application itself needs own thread to allow node keep running simultaneously.
When the application calls n-api functions from its own thread, node will crash.
Probably a related issue is that the documentation states the environment variable must not be cached for later use. Then how could I create/get an environment for calls, which do not originate from node?

#
# Fatal error in ../deps/v8/src/isolate.h, line 536
# Debug check failed: (isolate) != nullptr.
#
(node:8606) Warning: N-API is an experimental feature and could change at any time.

Thread 7 "node" received signal SIGILL, Illegal instruction.
[Switching to Thread 0x7f1decf11700 (LWP 8615)]
v8::base::OS::Abort () at ../deps/v8/src/base/platform/platform-posix.cc:176
176         V8_IMMEDIATE_CRASH();
(gdb) bt full
#0  v8::base::OS::Abort () at ../deps/v8/src/base/platform/platform-posix.cc:176
No locals.
#1  0x00005627fd91b9a3 in V8_Fatal (file=0x5627fda7aee7 "../deps/v8/src/isolate.h", line=536, format=0x5627fdf08395 "Debug check failed: %s.")
    at ../deps/v8/src/base/logging.cc:138
        arguments = {{gp_offset = 32, fp_offset = 48, overflow_arg_area = 0x7f1decf10ab0, reg_save_area = 0x7f1decf109f0}}
#2  0x00005627fd91a938 in v8::base::(anonymous namespace)::DefaultDcheckHandler (file=0x5627fda7aee7 "../deps/v8/src/isolate.h", line=536,
    message=0x5627fda7af00 "(isolate) != nullptr") at ../deps/v8/src/base/logging.cc:56
No locals.
#3  0x00005627fd91b9cd in V8_Dcheck (file=0x5627fda7aee7 "../deps/v8/src/isolate.h", line=536, message=0x5627fda7af00 "(isolate) != nullptr")
    at ../deps/v8/src/base/logging.cc:142
No locals.
#4  0x00005627fc99ce41 in v8::internal::Isolate::Current () at ../deps/v8/src/isolate.h:536
        isolate = 0x0
#5  0x00005627fc9ae8f9 in v8::Utils::ReportApiFailure (location=0x5627fda7e63c "HandleScope::HandleScope",
    message=0x5627fda7e608 "Entering the V8 API without proper locking in place") at ../deps/v8/src/api.cc:405
        isolate = 0x7f1decf10b80
        callback = 0x5627fc9acb25 <v8::internal::ThreadManager::IsLockedByCurrentThread()+59>
#6  0x00005627fc99d769 in v8::Utils::ApiCheck (condition=false, location=0x5627fda7e63c "HandleScope::HandleScope",
    message=0x5627fda7e608 "Entering the V8 API without proper locking in place") at ../deps/v8/src/api.h:124
No locals.
#7  0x00005627fc9b10a8 in v8::HandleScope::Initialize (this=0x7f1de40923f0, isolate=0x5627ffb92350) at ../deps/v8/src/api.cc:1048
        internal_isolate = 0x5627ffb92350
        current = 0xffffffffffffff88
#8  0x00005627fc9b1035 in v8::HandleScope::HandleScope (this=0x7f1de40923f0, isolate=0x5627ffb92350) at ../deps/v8/src/api.cc:1037
No locals.
#9  0x00005627fd67138f in (anonymous namespace)::v8impl::HandleScopeWrapper::HandleScopeWrapper (this=0x7f1de40923f0, isolate=0x5627ffb92350)
    at ../src/node_api.cc:176
No locals.
#10 0x00005627fd67b6bd in napi_open_handle_scope (env=0x5627ffc2ce30, result=0x7f1decf10c68) at ../src/node_api.cc:2602
No locals.
int my_func(int id)
{
  int ret;
  napi_handle_scope scope;
  napi_value result;
  napi_value this;
  napi_value func;
  const int argc = 1;
  napi_value argv[argc];

  printf("id start %i\n", id);
  puts("trying to open scope");

  if (napi_ok != napi_open_handle_scope(env, &scope))
    napi_throw_error(env, 0, "Could not open a scope");

  puts("scope open");

  if (napi_ok != napi_get_reference_value(env, functions.this, &this))
    napi_throw_error(env, 0, "Could not get this");

  if (napi_ok != napi_get_reference_value(env, functions.id, &func))
    napi_throw_error(env, 0, "Could not get func");

  if (napi_ok != napi_create_int32(env, id, &argv[0]))
    napi_throw_error(env, 0, "Argument conversion error");

  if (napi_ok != napi_call_function(env, this, func, argc, argv, &result))
    napi_throw_error(env, 0, "Could not call javascript function");

  if (napi_ok != napi_get_value_int32(env, result, &ret))
    napi_throw_error(env, 0, "Could not retrieve the result");

  if (napi_ok != napi_close_handle_scope(env, scope))
    napi_throw_error(env, 0, "Could not close a scope");

  printf("id end %i %i\n", id, ret);

  return ret;
}
@bnoordhuis bnoordhuis added question Issues that look for answers. wrong repo Issues that should be opened in another repository. labels Dec 22, 2017
@bnoordhuis
Copy link
Member

Duplicate of nodejs/help#1035. nodejs/help is the right place for questions/support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers. wrong repo Issues that should be opened in another repository.
Projects
None yet
Development

No branches or pull requests

2 participants