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

Usage of Passing Wrapped With NodeJs' Worker Threads #725

Closed
necil opened this issue May 12, 2020 · 3 comments
Closed

Usage of Passing Wrapped With NodeJs' Worker Threads #725

necil opened this issue May 12, 2020 · 3 comments

Comments

@necil
Copy link

necil commented May 12, 2020

Hi,

Currently I am having problem in my project.
Briefly what my project does: reads mqtt output from c code and passes it inside a struct to js side. I have created a minimal example with the n-api examples using nodejs/node-addon-examples repository.

my first object contains method:

napi_value MyObject::CreateObject(napi_env env, napi_callback_info info) {
  napi_status status;

  size_t argc = 1;
  napi_value args[1];
  status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
  assert(status == napi_ok);

  napi_value instance;
  status = MyObject2::NewInstance(env, args[0], &instance);
  assert(status == napi_ok);
  return instance;
}

this method creates instance of MyObject2 and returns to js.

NewInstance for MyObject2 is:

napi_status MyObject2::NewInstance(napi_env env,
                                  napi_value arg,
                                  napi_value* instance) {
  napi_status status;

  const int argc = 1;
  napi_value argv[argc] = {arg};
  napi_value cons;
  status = napi_get_reference_value(env, constructor, &cons);
  if (status != napi_ok) return status;

  status = napi_new_instance(env, cons, argc, argv, instance);
  if (status != napi_ok) return status;

  return napi_ok;
}

finally I create and call these objects from js using following code:

function createWorker(count) {
    return new Promise((resolve, reject) => {
        const worker = new Worker(__filename, {
            workerData: count
        });
        worker.on('message', resolve);
        worker.on('error', reject);
        worker.on('exit', (code) => {
            if (code !== 0)
                reject(new Error(`Worker stopped with exit code ${code}`));
        });
    });
};

if (isMainThread) {
    createWorker(1);
    createWorker(2);
} else {
    obj1 = addon.MyObject(1);
    while(true)
        obj1.createObject(workerData);
}

behavior running this js code causes errors everytime. However when and where it causes the error and error type itself changes in each run.
sometimes I get segmentation fault. Sometimes it aborts without an error and sometimes it is the error below.

node: ../myobject.cc:212: static napi_value__* MyObject::CreateObject(napi_env, napi_callback_info): Assertion `status == napi_ok' failed.

I am suspecting this is related worker threads since I do not get any error when I run program without worker threads.
I have looked into napi_thread_safe_function however I couldn't find a way out of this.
You can find full version of example code here: https://github.com/necil/n-api-example

@necil
Copy link
Author

necil commented May 13, 2020

I believe it is related to #711 . Please correct if I am wrong.

@mhdawson
Copy link
Member

There is a PR to update the example: nodejs/node-addon-examples#137. It is the same example as you mentioned?

@necil
Copy link
Author

necil commented May 20, 2020

This PR points the same issue I have had. However solution on c addon side Environment Life Cycle APIs marked as experimental. I guess I have to wait for it to be stable. Thank you for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants