Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 322
test Avatar asked Nov 06 '22 07:11

test


1 Answers

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();
}
like image 179
Michael K Avatar answered Nov 09 '22 23:11

Michael K