5

I am writing a .cc file so that I can read functions from it in a .js file.

The structure of code is as follows:

napi_value createResult(napi_env env, string resultType, int64_t handlevalue) {
    napi_status status;
    napi_value ObjectRef, returnObject, errorObject;

    printf("INSIDE FUCNTION: PART1\n");

    // Creating NAPI Object's
    status = napi_create_object(env, &ObjectRef);

    std::cout<<"status="<<status<<std::endl;

    assert(status == napi_ok);


    printf("INSIDE FUCNTION: PART2\n");

    status = napi_create_object(env, &errorObject);
    assert(status == napi_ok);

    printf("INSIDE FUCNTION: PART3\n");

    status = napi_create_object(env, &returnObject);
    assert(status == napi_ok);

    printf("INSIDE FUCNTION: PART4\n");

    const char* resultTypeChar = resultType.c_str();
    status = napi_set_named_property(env, returnObject, &resultTypeChar[0], ObjectRef);
    assert(status == napi_ok);

    printf("INSIDE FUCNTION: PART5\n");

    return returnObject;
}

void ABC(napi_env env, void* data){

    // some code....
    size_t handlevalue = access._handle;

    obj->result = createResult(env,"access",handlevalue);

    obj->async_action_status = 0;
  }
}

napi_value f1(napi_env env,
  napi_callback_info info) {
  //
  napi_value promise;
  napi_status status;
  // some code....

  napi_value resource_name;
  napi_create_string_utf8(env, "f1", NAPI_AUTO_LENGTH, &resource_name);

  napi_create_async_work(env, NULL, resource_name, ABC, DEF, obj, &obj->work);

  napi_queue_async_work(env, obj->work);

  return promise;
}

On compiling this & then running a .js file, the following error is displayed:

INSIDE FUCNTION: PART1

#
# Fatal error in v8::HandleScope::CreateHandle()
# Cannot create a handle without a HandleScope
#

Illegal instruction (core dumped)

I have not used any v8 or HandleScope or CreateHandle function anywhere in my code.

Since I am new to all this stuff of promise and async, so I am clueless about how to resolve this.

Kindly help

ashuvssut
  • 1,725
  • 9
  • 17
test
  • 105
  • 2
  • 8
  • Sorry Test, don't know the answer but find "cannot create a handle without a HandleScope" strangely amusing. Apreciate that you will see it differently. – Roamer-1888 Jun 17 '20 at 16:21
  • I also struggle with this issue. When I find a solution I'll tell you. Please also do when you find something. – Michael K Jun 23 '20 at 14:00
  • So beside the fact that I use node-addon-api (which is the C++ wrapper around NAPI) it might be helpful for you to follow this discussion I started there: https://github.com/nodejs/node-addon-api/issues/750 – Michael K Jun 23 '20 at 14:33
  • Also this might help you: https://stackoverflow.com/questions/54187588/understanding-node-addon-api-n-api-handlescope – Michael K Jun 23 '20 at 14:38

2 Answers2

1

From my learnings I created and just released napi-threadsafe-deferred, which you could use in that situation. It is based on the C++ wrapper node-addon-api though, but you might port it to plain NAPI if you like.

It executes the actual promise resolution/rejection asynchronously on the main js thread, which is the only thread from which it is allowed to be called.

You could rewrite your code (left out noise to focus on the important stuff):

napi_value createResult(napi_env env, string resultType, int64_t handlevalue) {

    // ...

    return returnObject;
}

void ABC(napi_env env, void* data){

    // ...

    myDeferred.Reslove([&handlevalue]{
        // this is executed on the correct thread!
        // BEWARE, that references passed in might get
        // invalid as this code is executed after the
        // ABC has returned!
        return createResult(env,"access",handlevalue);
    });

    // ...
  }
}

napi_value f1(napi_env env,
  napi_callback_info info) {

    ThreadSafeDeferred myDeferred = new ThreadSafeDeferred(Env());
  
    // pass myDeferred to your async task (calling ABC)

    return ThreadSafeDeferred.Promise();
}
Michael K
  • 1,070
  • 1
  • 10
  • 25
0

In my case the problem was we were using the last Node version, but NOT the last Node LTS version.

So just rollback the node version to the last Node LTS version fixed the issue.

Also, be sure you are using the NPM version that came by default with that Node version.

A couple of interesting points:

  • We were usign NextJS (the build failed with this "v8::HandleScope::CreateHandle()" error)

  • Locally everything was working fine, the problem was appearing just in production (deployed with docker)

Juanma Menendez
  • 17,253
  • 7
  • 59
  • 56
  • Which version did you rollback to? I have the same issue and can't figure out which version to go back to. LTS version doesn't work for me. – Mats Jun 29 '21 at 04:49
  • Sorry, I don't remember, try to check the last LTS version around my comment date Mar/2021 – Juanma Menendez Jun 29 '21 at 13:51